DEV Community

Cover image for Resolving the Vercel 404 "Page Not Found" Error After Page Refresh.
Stanlisberg
Stanlisberg

Posted on

Resolving the Vercel 404 "Page Not Found" Error After Page Refresh.

I recently completed a project(cinemaxtv.vercel.app), which is a movie application. After deploying the application on Vercel, I noticed an unusual issue. Whenever I navigate to a different page and refresh that specific route, I encounter a 404 Not Found Error, indicating that the specified route cannot be found. However, I achieve the desired result when I perform the same action on my local host. Consequently, I deduced that the error is likely due to a deployment glitch on Vercel.

404 image
A pictorial display of the 404 not found error above

After conducting some research, I discovered a solution for resolving this issue. To rectify the 404 Not Found Error, it is necessary to establish redirect and rewrite rules for your React Router application. These rules will assist your application in circumventing the "Page Not Found" error.

To begin, generate a vercel.json file in the root directory of your project. This file will enable Vercel to configure the routing accurately, and you should insert the following code snippet in the image below.

vercel image
This image represents the structure of the vercel.json file and the code snippet.

{
    "rewrites": [
        {"source": "/(.*)", "destination": "/"}
    ]
}

Enter fullscreen mode Exit fullscreen mode

The provided code snippet instructs Vercel to rewrite any incoming URL path to the root path. This configuration ensures that when we initiate a redirect with a child path, Vercel will redirect it to the root path, effectively preventing the occurrence of the 404 Not Found Error.

If your project has already been deployed, push the changes to your GitHub repository and refresh the page, you will obtain your desired result seamlessly, without encountering any issues.

working image

voila! It now works.

Conclusion
If you find this article interesting, please consider liking and sharing it. Additionally, I welcome your thoughts and feedback in the comment section below. Thank you for your engagement.

Open to work🙂
Did you enjoy this article or need an experienced Technical Writer / React Developer for a remote, full-time, or contract-based role? Feel free to contact me.
Github || Linkedin || Twitter

Top comments (40)

Collapse
 
hassanabuya82 profile image
Hassan

I don't know why my error still persists

Collapse
 
stanlisberg profile image
Stanlisberg

What error are you getting?

Collapse
 
kingjulien profile image
Abusomwan Santos

my links arent working, like when i try to navigate to /about. only the navbar works

Thread Thread
 
stanlisberg profile image
Stanlisberg • Edited

I meant, what is the content of the error message? Kindly send a screenshot of the error and your routing set up to help assist you better.

Thread Thread
 
kingjulien profile image
Abusomwan Santos

Its strange cause it works well in dev mode, i can navigate between links but it fails on prod mode
Image description

Thread Thread
 
kingjulien profile image
Abusomwan Santos

Image description

Thread Thread
 
stanlisberg profile image
Stanlisberg

Since it works fine in dev mode, the issue must be linked to the deployment.

Did you deploy with vercel?

Thread Thread
 
kingjulien profile image
Abusomwan Santos

yes im using vercel

Thread Thread
 
stanlisberg profile image
Stanlisberg • Edited

Did you try to set it up like i did on this article?

If you followed all the steps I outlined in the article judiciously, I believe the routing problem will be rectified.

Thread Thread
 
kingjulien profile image
Abusomwan Santos

yes I did, but it redirects all routes to the home page

Thread Thread
 
stanlisberg profile image
Stanlisberg

For the fact that it works in Dev mode, the issue has to do with deployment. I had similar issue after implementing all the steps in my article and forgot to push changes to my GitHub repo. So you have to consider pushing your changes to your repo if you forgot to do that earlier.

...anyways, i'd implore you to create another json.vercel file, input the rewrites code snippet, restart your server and push changes to your repo. Let's see the outcome.

Thread Thread
 
kingjulien profile image
Abusomwan Santos

strange cause im getting same error like your post but its not working even with the vercel.json

Thread Thread
 
mohamedaliyarar profile image
A.R. Mohamed Aliyar

If you use react-router it may happen because react-router redirects pages in the client-side itself if any reload happens it will send a request to the server side and if the request isn't handled it will persist. you may refer
to this here. I have the same issue. If someone knows kindly let me know.

Collapse
 
sunywebdevs profile image
Md Suny Shaikh

Same error page not found

Collapse
 
hamzagorcevic profile image
Hamza Gorcevic

try adding and rebuild, module.exports = {
trailingSlash: true,

Collapse
 
dbkooper51 profile image
CodeKooper

Thanks for this.
For flutter apps or Single page apps, the following helped me :

{
  "rewrites": [
    {"source": "/:a*", "destination": "/"}
  ]
}

Enter fullscreen mode Exit fullscreen mode
Collapse
 
ebubeevan profile image
Evangeline Mmayie • Edited

Thanks a lot! was wondering why the child paths of my deployed app couldn't refresh. God bless

Collapse
 
stanlisberg profile image
Stanlisberg

You are welcome @ebubeevan. I am glad to help.🤗

Collapse
 
israelmitolu profile image
IsraelMitolu

Thank you!
It worked well

Collapse
 
gitinitpriyanshu profile image
Priyanshu Bartwal

It worked ! Thanks.

Collapse
 
stanlisberg profile image
Stanlisberg

🙏

Collapse
 
alvarnydev profile image
alvarny.dev

Thank you so much, that made it really easy to get it to work :)

Collapse
 
stanlisberg profile image
Stanlisberg

I'm glad I could help @alvarnydev

Collapse
 
aasim782 profile image
Aasim

Thank you!, I faced same problem. I fixed now

Collapse
 
stanlisberg profile image
Stanlisberg

I am glad I could help @aasim782 🙏

Collapse
 
imiebogodson profile image
Imiebo Godson

Thank you, man. Your article just helped me fix the same issue.👍

Collapse
 
stanlisberg profile image
Stanlisberg

Glad it helped @imiebogodson 🙏

Collapse
 
cpwaldow profile image
Carlos Eduardo

Dude, thank you, i got this problem right now and the vercel.json solution worked to me.

Collapse
 
mrhawking profile image
mrhawking • Edited

Thank you a lot, I solved that problem in 3 sec and it works now!

Collapse
 
simaomeyrer profile image
Simão Meyrer

You helped me a lot man, thanks!

Collapse
 
stanlisberg profile image
Stanlisberg

You are welcome @simaomeyrer .

Collapse
 
avkravchenko profile image
Alexander

Thank you so much!

Collapse
 
stanlisberg profile image
Stanlisberg

You are welcome. 👍

Collapse
 
dravidjones28 profile image
dravidjones28 • Edited

Thank you Dude! God bless you!
I Fixed it.

Collapse
 
stanlisberg profile image
Stanlisberg

You are welcome @dravidjones28 . I'm happy to help.

Collapse
 
tlanettepollard profile image
T. Lanette

Thank you it worked!

Collapse
 
neerajghatage profile image
Neeraj Ghatage

Would u plz help me?.....i can't even load my website on Google when I refresh it 5-6 times then it loads the website