From 3b68e9e0d4cabe71f56c249816b1a5d5d0d09ad9 Mon Sep 17 00:00:00 2001 From: JJ Kasper Date: Thu, 28 Jul 2022 23:06:24 -0500 Subject: [PATCH] Fix incorrect component cache for middleware cases --- packages/next/shared/lib/router/router.ts | 6 ----- .../middleware-rewrites/test/index.test.ts | 22 +++++++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/packages/next/shared/lib/router/router.ts b/packages/next/shared/lib/router/router.ts index 331870a5e1e0..ea7941b00932 100644 --- a/packages/next/shared/lib/router/router.ts +++ b/packages/next/shared/lib/router/router.ts @@ -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 } } } @@ -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( diff --git a/test/e2e/middleware-rewrites/test/index.test.ts b/test/e2e/middleware-rewrites/test/index.test.ts index d3b33f437ee0..8af533562049 100644 --- a/test/e2e/middleware-rewrites/test/index.test.ts +++ b/test/e2e/middleware-rewrites/test/index.test.ts @@ -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")`)