next.js: 404 page triggers on every page
Link to the code that reproduces this issue
https://github.com/itsLaserr/min-repro
To Reproduce
Clone my repo https://github.com/itsLaserr/min-repro
npm run dev
or
npm run build and npm run start
Current vs. Expected behavior
Current behavior On page load or switch “404 page” console logs, this console log is located in the notfound page and this is not supposed to happen as nothing on the current page resulted in 404. This happens on ALL pages
Expected behavior its Expected that on page load or switch where nothing is 404 the notfound page shouldnt get triggered in any way as it has nothing to do with the current page.
Provide environment information
Operating System:
Platform: win32
Arch: x64
Version: Windows 11 Pro
Available memory (MB): 16252
Available CPU cores: 12
Binaries:
Node: 20.11.1
npm: N/A
Yarn: N/A
pnpm: N/A
Relevant Packages:
next: 14.2.4 // Latest available version is detected (14.2.4).
eslint-config-next: N/A
react: 18.2.0
react-dom: 18.2.0
typescript: N/A
Next.js Config:
output: N/A
Which area(s) are affected? (Select all that apply)
Runtime
Which stage(s) are affected? (Select all that apply)
next dev (local), next start (local)
Additional context
No response
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 8
- Comments: 20
I fix that removing the folder App who is out of the of src.
I think if you drop your App to src folder it will work.
@BatuhanAktan that doesn’t fix the issue and I also have the issue you’re talking about @itsLaserr.
I actually created a new app this morning with create-next-app. Then added a not-found.js file. Then added console logs to the home page and not-found page. Go to the home page, home page renders, but console logs for both components run.
I can reproduce the issue with your code above as well @BatuhanAktan
My only assumption is nextjs might do this in order to have the code ready for the 404 page in case the notFound() function is called at any point? But still seems dumb if that’s the case…
Yes but that doesn’t really fix the bug it just ignores it, i added the console logs to find the issue. So i will remove the console logs but this isn’t solved as the 404 page should’t trigger on every page.
Hello itsLaserr,
I was just looking through your repo and was not able to reproduce On page load 404 page not found, however, when navigated to other pages using the navbar I did notice the 404 page not found.
I believe that your navbar points to /dashboard, which at the moment does not have any content associated with it which results in the 404 page. I fixed this issue by creating a folder for your dashboard page and adding some filler content which currently eliminates the problem you have. Please check your pull requests to access the contribution.
Let me know if this fixes your problem!
Yeah the hope is people really aren’t ever hitting your 404 page, and if they do, I’m also willing to just sacrifice running that during runtime. If the performance takes a hit I don’t really care because its the 404 page haha
😭😭😭
I would much rather just have it load the RSC at runtime of the client-side 404. Rather than sacrifice compute on every non-edge case like standard page loads.
“Behavior by design” is always an interesting way to describe something abnormal like this. When in reality it is behavior due to a technical constraint that was circumnavigated rather than overcome to speed up delivery most likely. AKA, a technical compromise.
Oh well, at least that is enough context to set aside any hope I had here and move on to my own solutions, like ensuring the 404 page is a dynamically loaded client-side component.
If I could rewind the clock, I would have either stuck with the pages directory and just embraced a custom loader pattern, or moved frameworks entirely. So many struggles like this plague an app dir migration.
I got a message back from Vercel:
+1
Would it be possible to fix it?
In our case this issue makes impossible to attach a Sentry 404 errors handler, because it sends logs all the time, even if the current page exists.
Hey @timneutkens @leerob i also see this behaviour for every trace the not-found page appears on all dynamic routes traces is this intended behaviour in nextjs ?
I encountered the same issue. I identified it by placing a
sentry.captureException. After deploying it to production, we discovered that the issue was occurring frequently, with numerous instances within just three hours. As a result, we had to disable it urgently. @richardtaylordawson @itsLaserr @cyaxaressThis issue also exists in a completely new project.
I don’t have a src folder, so i should make one and put the app in it? don’t understand how having a src folder changes anything as its just for organization, I tried it and nothing changed, still the same thing happens
I tried adding content to the dashboard, but still nothing. It logs to the console on every page load. Here you can see what was logged to the console when I navigated from the dashboard to the home page, as I added a console log in the 404 page to also show the current path:
Also, I tried removing the navbar completely, and the issue was still there, so this can’t be related to the navbar.