Middleware page data fetch 404 causing Page component to return undefined data #41113
Closed
1 task done
Labels
bug
Issue was opened via the bug report template.
Middleware
Related to Next.js Middleware.
stale
The issue has not seen recent activity.
Verify canary release
Provide environment information
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
Vercel
Describe the Bug
In 4-5% of our users we are seeing in our google analytics that they are receiving a 'Page Not Found' for valid pages. These pages do work most of the time and so we have quite a bit of trouble replicating this issue.
Our page is a dynamic SSG page that when there is an issue with the data or an error comes back from our Apollo client it renders an Error component. The Error component will set the page to 'Page Not Found'. This issue has come in when upgrading to Next.js 12.2 and we believe it is to do with the update to the middleware. Unfortunately we need it for prefixing the default locale otherwise we could just remove it.
What we think could be the issue is that as there an extra fetch for page data when using middleware then it could be possible that the page is trying to fetch the data from an old deployment folder and then getting a 404 on the fetch. Once it 404's then undefined data is return to the component causing the Page component to render the Error component within it. In our datadog logs we do see 404 errors for the page data e.g /_next/data/TZOMVjjp6O7ETtw-t1234/en/page-name.json?slug=page-name.
Using a network interceptor tool (https://tamper.dev/) we can force a 404 and we do see the behaviour described above but without understanding how all this works under the hood it is hard to know if this is a valid test.
Expected Behavior
404's on the fetch for page data don't cause the page to break.
Link to reproduction
https://codesandbox.io/s/angry-nightingale-dgz4vo?file=/pages/%5B...slug%5D.tsx
To Reproduce
The text was updated successfully, but these errors were encountered: