-
Notifications
You must be signed in to change notification settings - Fork 27k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SSR-prefetches with middleware break application between deployments #59612
Comments
@thoaltmann Thanks for creating this issue. I am experiencing the same problem. Did you find any temporary solution to the problem? I don't think it is only related to prefetches, since it doesn't work when doing a router.push either (after a re-build). After a bit of investigation, I think that this behaviour was introduces in version 13.4.13-canary.0. It work fine in version 13.4.12. |
The commit that seems to have created the issue (1398de9) unfortunately involves a significant rework/restructure of the routing logic which makes it a bit hard for a newcomer of the repo to understand the full scope of the changes. However, I suspect the cause of the issue is this if-statement:
Removing this if-statement seems to resolve the issue, reverting the behavior to its previous state. After this modification, a 404 error on the _next/data/the-build-id/page.json-request is followed by a 200 on the /page-request. Additionally, running the test suite post-modification shows no issues. Sorry to bother you @ijjk , but I was wondering if you might have any insights into the rationale behind this if-statement's inclusion. Understanding that its removal might impact other functionalities, I'd greatly appreciate any guidance on potential alternative solutions or areas to investigate further. |
Hi @vilindberg, unfortunately we didn't find a solution and had to create our own custom middleware as a cloudfront viewer request lambda. |
@thoaltmann FYI. I think I have found a workaround for this issue. That this workaround works would also explain why there are not a gazillion issues created about this. Workaround: // app/layout.tsx - A default layout
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body>{children}</body>
</html>
);
}
// app/whatever/page.tsx - Dummy app page
export default function Page() {
return null;
} Seems to resolve the issue. On hover you get a 404 and then on click you get a hard navigation resulting in a 200 with the document. |
@vilindberg thanks, it worked for me too! |
Maybe we do not need to remove entire if statement, but just replace status code with 404 instead of 200, for me it fixed a problem during debugging.
|
@ydubinskyi Yeah, maybe it is better to do an early null-return instead. In that case the addition of the "x-nextjs-matched-path"-header should probably be removed as well, since it includes a path with the previous buildId. |
@vilindberg Your fix works. However, for it to work I also need to annotate the layout and pages with |
@karaoak Interesting! Do you have a small repro of it not working without Yeah, all the 404 errors are kind of irritating. When preserving logs, the solutions above (and also the pre v13.4.13 behaviour) causes the following 404's before the hard navigation. First 404 - Request headers: Second 404 - Request headers: Third 404 - Request headers: All 404's are to the In my uninformed opinion, if you get a path miss, but the miss is only due to a buildId-mismatch, the server should instantly return a 404 to the client with a header or whatever to tell the client to do a hard navigation. |
@vilindberg No, I'm sorry, unfortunately I don't have that available in a small repo. |
Unfortunately also not fixed with the release of Next 14.1.0 😢 |
I was able to fix our issue, either by removing our custom src/middleware.ts
And for this to work and be able to grab the buildId from the As well, make the Next BUILD_ID available at runtime, so that I can actually perform the check in above snippet. next.config.js
See: Advanced Middleware Flags for the |
I found this solution to work in our case:
This helps the client to eventually force refresh its JavaScript and aligns the version again for smooth experience and client-server communication
Since this is a version skew problem, there is a few options among:
|
@Lezzio Nice, I see |
Pages with middleware has been borked for a while, here's something related I reported back in October: #57207 and there's a bunch more unresolved issues here: #56222 (comment) Disappointing that these have been open for a while and don't seem to be getting any attention from the devs. |
Closing as this should be resolved by #60968, please upgrade to the latest canary of Next.js and give it a try! |
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Link to the code that reproduces this issue
https://github.com/thoaltmann/next-middleware-prefetch
To Reproduce
Current vs. Expected behavior
Current Behavior:
When a middleware is present, hovering over links to pages with
getServerSideProps
, the client sends_next/data
requests (which is the intended behavior and that's okay). After the first build, the response also includes Headers to signal the client that the request was skipped/bailed (X-Middleware-Skip=1
) so the actual request is then made when the user clicks on the link.However, after the app has been rebuilt/served/deployed, this behavior changes. The _next/data request then also responds with an empty object, but the
X-Middleware-Skip
header is missing. Instead, a resolved path with the previous build-id is in theX-Nextjs-Matched-Path
Header (e.g./_next/data/5NLSryPAF39BoEpy7KklK/test/1.json
). With this response, the client application doesn't know that this was a bailed SSR-request and uses the empty object as the actual data for the page, resulting in an error. When using a cache, this problem persists as long as the cache does, as the rendered HTML contains an old build-id in the__NEXT_DATA__
-scriptExpected Behavior:
After the second build, the
_next/data
-response should include the necessary headers for the client, so it doesn't use the empty object as actual data. After clicking a link and making the actual request to the data, the server will then send a 404, which leads to a hard navigation, similarly to how it behaves without a middleware.Verify canary release
Provide environment information
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 21.1.0: Wed Oct 13 17:33:01 PDT 2021; root:xnu-8019.41.5~1/RELEASE_ARM64_T6000 Binaries: Node: 18.17.1 npm: 9.6.7 Yarn: 1.22.19 pnpm: 8.10.4 Relevant Packages: next: 14.0.5-canary.12 eslint-config-next: N/A react: 18.2.0 react-dom: 18.2.0 typescript: 5.1.3 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
Data fetching (gS(S)P, getInitialProps), Middleware / Edge (API routes, runtime), Routing (next/router, next/navigation, next/link)
Additional context
No response
The text was updated successfully, but these errors were encountered: