Commit 4ba1f06
fix: status code to avoid navigation with empty props (#60968)
### What?
Currently, when a middleware is active and the path results in a 404,
the server responds with a 200 status code to data requests. We propose
a change to ensure that a 404 status code is returned in these
situations, as expected, solving production issues for the navigation
router.
### Why?
The client data requests (identified with `_next/data` path &
`x-nextjs-data` header) get answered the status code 200. The code below
is executed when there is a version skew for the data requests (e.g
prefetch in production).
https://github.com/vercel/next.js/blob/4125069840ca98981f0e7796f55265af04f3e903/packages/next/src/server/lib/router-server.ts#L217-L227
The version skew consists in the client side version identified with the
buildId in `__NEXT_DATA__ ` tag to be obsolete compared to the server
version (`BUILD_ID` file).
In the case of prefetching, this leads to the code above being executed,
therefore the `prefetch-reducer.ts` handles the response as valid and
sets it in its cache. Which ultimately triggers a navigation with empty
prop, resulting in erroneous behaviours reported in issues and in our
production websites:
https://github.com/vercel/next.js/blob/4125069840ca98981f0e7796f55265af04f3e903/packages/next/src/client/components/router-reducer/reducers/prefetch-reducer.ts#L54-L74
By switching the response to a 404, we trigger this code in the fetch
(`fetchServerResponse`). This change prompts a hard navigation
(mpaNavigation), effectively refreshing the client version and
resynching it with the server version.
https://github.com/vercel/next.js/blob/4125069840ca98981f0e7796f55265af04f3e903/packages/next/src/client/components/router-reducer/fetch-server-response.ts#L125-L134
### How?
We simply update the status code to 404 here:
https://github.com/vercel/next.js/blob/4125069840ca98981f0e7796f55265af04f3e903/packages/next/src/server/lib/router-server.ts#L223
Closes: #60785
Closes: #59295
Fixes: #47516
---------
Co-authored-by: JJ Kasper <[email protected]>1 parent bdd0dd2 commit 4ba1f06
File tree
3 files changed
+11
-26
lines changed- packages/next/src
- server/lib
- test/e2e/middleware-general/test
3 files changed
+11
-26
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
180 | 180 | | |
181 | 181 | | |
182 | 182 | | |
183 | | - | |
| 183 | + | |
184 | 184 | | |
185 | 185 | | |
186 | 186 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
323 | 323 | | |
324 | 324 | | |
325 | 325 | | |
326 | | - | |
327 | | - | |
| 326 | + | |
328 | 327 | | |
329 | | - | |
330 | | - | |
331 | | - | |
332 | | - | |
333 | | - | |
| 328 | + | |
334 | 329 | | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | | - | |
344 | | - | |
345 | | - | |
346 | | - | |
347 | | - | |
348 | | - | |
349 | | - | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
350 | 337 | | |
351 | 338 | | |
352 | 339 | | |
| |||
2403 | 2390 | | |
2404 | 2391 | | |
2405 | 2392 | | |
2406 | | - | |
| 2393 | + | |
2407 | 2394 | | |
2408 | 2395 | | |
2409 | 2396 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
107 | | - | |
108 | 107 | | |
109 | 108 | | |
110 | 109 | | |
111 | 110 | | |
112 | 111 | | |
113 | 112 | | |
114 | | - | |
115 | 113 | | |
116 | 114 | | |
117 | 115 | | |
| |||
0 commit comments