Unverified Commit bcb20100 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Fix error on 404 page when middleware exists (#46303)

Closes #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)
parent cd557c96
...@@ -1456,6 +1456,8 @@ export default class Router implements BaseRouter { ...@@ -1456,6 +1456,8 @@ export default class Router implements BaseRouter {
Router.events.emit('routeChangeStart', as, routeProps) Router.events.emit('routeChangeStart', as, routeProps)
} }
const isErrorRoute = this.pathname === '/404' || this.pathname === '/_error'
try { try {
let routeInfo = await this.getRouteInfo({ let routeInfo = await this.getRouteInfo({
route, route,
...@@ -1645,10 +1647,7 @@ export default class Router implements BaseRouter { ...@@ -1645,10 +1647,7 @@ export default class Router implements BaseRouter {
// wasn't originally present. This is also why this block is before the // wasn't originally present. This is also why this block is before the
// below `changeState` call which updates the browser's history (changing // below `changeState` call which updates the browser's history (changing
// the URL). // the URL).
if ( if (isQueryUpdating && isErrorRoute) {
isQueryUpdating &&
(this.pathname === '/404' || this.pathname === '/_error')
) {
routeInfo = await this.getRouteInfo({ routeInfo = await this.getRouteInfo({
route: this.pathname, route: this.pathname,
pathname: this.pathname, pathname: this.pathname,
...@@ -1658,6 +1657,7 @@ export default class Router implements BaseRouter { ...@@ -1658,6 +1657,7 @@ export default class Router implements BaseRouter {
routeProps: { shallow: false }, routeProps: { shallow: false },
locale: nextState.locale, locale: nextState.locale,
isPreview: nextState.isPreview, isPreview: nextState.isPreview,
isQueryUpdating: isQueryUpdating && !this.isFallback,
}) })
if ('type' in routeInfo) { if ('type' in routeInfo) {
......
...@@ -118,5 +118,16 @@ describe.each(table)( ...@@ -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')
})
} }
) )
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment