Skip to content
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

Fix error on 404 page when middleware exists #46303

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/next/src/shared/lib/router/router.ts
Expand Up @@ -1456,6 +1456,8 @@ export default class Router implements BaseRouter {
Router.events.emit('routeChangeStart', as, routeProps)
}

const isErrorRoute = this.pathname === '/404' || this.pathname === '/_error'

try {
let routeInfo = await this.getRouteInfo({
route,
Expand Down Expand Up @@ -1645,10 +1647,7 @@ export default class Router implements BaseRouter {
// wasn't originally present. This is also why this block is before the
// below `changeState` call which updates the browser's history (changing
// the URL).
if (
isQueryUpdating &&
(this.pathname === '/404' || this.pathname === '/_error')
) {
if (isQueryUpdating && isErrorRoute) {
routeInfo = await this.getRouteInfo({
route: this.pathname,
pathname: this.pathname,
Expand All @@ -1658,6 +1657,7 @@ export default class Router implements BaseRouter {
routeProps: { shallow: false },
locale: nextState.locale,
isPreview: nextState.isPreview,
isQueryUpdating: isQueryUpdating && !this.isFallback,
})

if ('type' in routeInfo) {
Expand Down
11 changes: 11 additions & 0 deletions test/e2e/404-page-router/index.test.ts
Expand Up @@ -118,5 +118,16 @@ describe.each(table)(
}
})
})

// It should not throw any errors when re-fetching the route info:
// https://github.com/vercel/next.js/issues/44293
it('should not throw any errors when re-fetching the route info', async () => {
const browser = await webdriver(next.url, '/?test=1')
await check(
() => browser.eval('next.router.isReady ? "yes" : "no"'),
'yes'
)
expect(await browser.elementById('query').text()).toEqual('test=1')
})
}
)