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 incorrect component cache for middleware cases #39159

Merged
merged 2 commits into from Jul 29, 2022
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
6 changes: 0 additions & 6 deletions packages/next/shared/lib/router/router.ts
Expand Up @@ -1763,7 +1763,6 @@ export default class Router implements BaseRouter {
// If we have a match with the current route due to rewrite,
// we can copy the existing information to the rewritten one.
// Then, we return the information along with the matched route.
this.components[requestedRoute] = { ...existingInfo, route }
return { ...existingInfo, route }
}
}
Expand Down Expand Up @@ -1908,11 +1907,6 @@ export default class Router implements BaseRouter {
routeInfo.resolvedAs = resolvedAs
this.components[route] = routeInfo

// If the route was rewritten in the process of fetching data,
// we update the cache to allow hitting the same data for shallow requests.
if (route !== requestedRoute) {
this.components[requestedRoute] = { ...routeInfo, route }
}
return routeInfo
} catch (err) {
return this.handleRouteInfoError(
Expand Down
22 changes: 22 additions & 0 deletions test/e2e/middleware-rewrites/test/index.test.ts
Expand Up @@ -27,6 +27,28 @@ describe('Middleware Rewrite', () => {
testsWithLocale('/fr')

function tests() {
it('should not mix component cache when navigating between dynamic routes', async () => {
const browser = await webdriver(next.url, '/param-1')

expect(await browser.eval('next.router.pathname')).toBe('/[param]')
expect(await browser.eval('next.router.query.param')).toBe('param-1')

await browser.eval(`next.router.push("/fallback-true-blog/first")`)
await check(
() => browser.eval('next.router.pathname'),
'/fallback-true-blog/[slug]'
)
expect(await browser.eval('next.router.query.slug')).toBe('first')
expect(await browser.eval('next.router.asPath')).toBe(
'/fallback-true-blog/first'
)

await browser.back()
await check(() => browser.eval('next.router.pathname'), '/[param]')
expect(await browser.eval('next.router.query.param')).toBe('param-1')
expect(await browser.eval('next.router.asPath')).toBe('/param-1')
})

it('should have props for afterFiles rewrite to SSG page', async () => {
let browser = await webdriver(next.url, '/')
await browser.eval(`next.router.push("/afterfiles-rewrite-ssg")`)
Expand Down