-
Notifications
You must be signed in to change notification settings - Fork 27.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[NEXT-435] Minimal middleware (only NextResponse.next()), generates error on error pages in dev mode #44293
Comments
same here |
same here, the 404 hard navigate problem still persists in the next 13.1.1 |
Same issue here from |
I also experience issues on the production build, it is not just on dev. It seems to have to do with i18n and/or getStaticPaths in that situation but have not been able to get a clear view on what’s going on. Definitely something that happens just by having a middleware file. |
In my case, I excluded |
@p-chan would you please provide an example of how you did so? |
@mohux I'm sorry. |
Adding the 404 page to exclude from the matcher does not work indeed. As I commented before, it is not just on dev. A build is broken as well. If you have a dynamic page with If you remove the Reason for mentioning it is that I think some things around middleware and navigating are not going well. Not just on dev but also in a production environment. |
Same issue on next 13.1.2. So I will follow the ticket and hope for a fix |
I potentially found a workaround I am using getStaticProps and also using dynamic revalidation but also using Since I am doing a check based on
Then if I am building my app I am returning notFound: true. Once I am running it will prefetch the path and then return my page to a my 404. I don't want it to be permanent since I am using dynamic revalidation. With this check I can then avoid the following error
|
Can confirm this is happening as well. Had to downgrade to 13.0.6 to fix. |
It seems like that when you have a middleware file, it triggers internal routing handling more than once. I noticed when running nextjs from source the first run is "good" but the second triggers this behaviour. This will also happen in production builds obviously. I am not good enough into the nextjs repo to know how to fix or say something more educated about it, but that is what I noticed. It might help 🙈 . |
I observe that, for a 404 page, any middleware (just a In 13.0.6, the middleware only gets called once for the 404 page, and there's no error. Thanks to OP for identifying in which version the issue was introduced. |
@damien can you look at this issue? Seems more apps have encountered the issue |
Seems to have been introduced at #43836 Namely, this code was removed. // We don't update for 404 requests as this can modify
// the asPath unexpectedly e.g. adding basePath when
// it wasn't originally present
initialData.page !== '/404' &&
initialData.page !== '/_error' && |
It's broken for me too. In my case I'm using the locale example from the docs (to always send to a locale subpath either detected or chosen by the user). The only way I can get a custom 404 to show without any errors is to remove the middleware, which breaks the whole thing for me. The problem (at least in my case) seems to be that next.js will always think that the route is a middleware redirection (and thus requesting chunks/page/whatever.json then breaking). This is because since I'm always under a locale subpath, having a middleware will make a matcher match because of the optional locale group in the regex. For example, with locales enabled: export const config = {
matcher: ["/", "/((?!_next/static|assets|favicon.ico).*)"],
}; Turns into the following in
// Without middleware - thus why it works
[] // With middleware & i18n in config
[
"^(?:\\/(_next\\/data\\/[^/]{1,}))?(?:\\/([^/.]{1,}))(|\\.json|\\/?index|\\/?index\\.json)?[\\/#\\?]?$",
"^(?:\\/(_next\\/data\\/[^/]{1,}))?(?:\\/([^/.]{1,}))(?:\\/((?!_next\\/static|assets|favicon.ico).*))(.json)?[\\/#\\?]?$"
] and // With middleware & without i18n in config
[
"^(?:\\/(_next\\/data\\/[^/]{1,}))?(?:\\/(\\/?index|\\/?index\\.json))?[\\/#\\?]?$",
"^(?:\\/(_next\\/data\\/[^/]{1,}))?(?:\\/((?!_next\\/static|assets|favicon.ico).*))(.json)?[\\/#\\?]?$"
] I thought and tried to work around it by adding the "known locales" to the matcher (and also how I found the above), but it seems that whenever you have locale support enable, the matcher won't even match locales at all because it's already on that regex group. Whatever you add to the matcher will end up being something like I know it's not purely related to locales, but it's how I managed to get the error page to show in my case. I hope this at least helps debugging it further - I'm completely stuck without an error page right now because of this since I had to completely disable ours just to avoid switching the whole application to client-side locale redirection and keep the behavior working. |
A poor workaround that worked for me is to just fallback rewrite to a custom error page and remove 404.tsx. module.exports = {
// ...
rewrites() {
return {
fallback: [
{
source: "/:path*",
destination: "/not-found",
locale: false,
},
],
};
}
}; EDIT: To make it respond with 404, add getServerSideProps() to the custom error page and make it return Complete workaroundnext.config.js module.exports = {
// ...
rewrites() {
return {
fallback: [
// last entry
{
source: "/:path*",
destination: "/404bug?path=:path*",
locale: false,
},
],
};
},
}; middleware.js // The contents of this file actually doesn't matter.
// It just needs to exist and be a valid middleware to trigger the bug.
import { NextResponse } from 'next/server';
export function middleware(request) {
return NextResponse.next();
} pages/404.jsx export default () => <div>404 works</div>; pages/404bug.jsx export default () => null;
export const getServerSideProps = () => ({ notFound: true }); |
If this helps resolve the issue, i'm using next version 12.2.0. What worked for me was downgrading to the version 12.1.6, as described in: |
Any progress on a fix for this? Solution by @WoLfulus did not work for me. |
I got this issue when using middleware and The temporary solution is downgrade to |
* Fix link in 404 * Downgrade Next to 13.0.6 vercel/next.js#44293 --------- Co-authored-by: Marek Bodinger <marek.bodinger@gmail.com>
I concur with @RioChndr the issue doesn't arise in |
Closes vercel#44293. ## Bug - [ ] Related issues linked using `fixes #number` - [x] Integration tests added - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md) ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
still facing the same issue.. |
:( |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Verify canary release
Provide environment information
Operating System:
Platform: win32
Arch: x64
Version: Windows 10 Pro
Binaries:
Node: 16.17.0
npm: N/A
Yarn: N/A
pnpm: N/A
Relevant packages:
next: 13.1.0
eslint-config-next: N/A
react: 18.2.0
react-dom: 18.2.0
Which area(s) of Next.js are affected? (leave empty if unsure)
Routing (next/router, next/navigation, next/link)
Link to the code that reproduces this issue
https://github.com/w4zZz4p/issue-nextjs-error-page
To Reproduce
open http://localhost:3000/page-does-not-exist
Describe the Bug
When there is empty global middleware created:
and when you visit any 404 page, error occurs:
The issue appeared in version
v13.0.7-canary.5
.Version
v13.0.7-canary.4
works as expected.My assumption its related to #43836
Expected Behavior
There should be no error as in any existing page for example http://localhost:3000/
Which browser are you using? (if relevant)
Chrome 108.0.5359.125 (Official Build) (64-bit)
How are you deploying your application? (if relevant)
No response
NEXT-435
The text was updated successfully, but these errors were encountered: