From 2f9ff160aa5fa2d0521685119bb108f075cbad92 Mon Sep 17 00:00:00 2001 From: Jimmy Lai Date: Wed, 10 May 2023 17:30:14 +0200 Subject: [PATCH] interception route: fix route groups breaking the referrer computation (#49602) This PR fixes a bug during path normalisation where `/(foo)/` would break because it would return `` per the function logic. A simple fix is to just ensure we always return a `/` as the default if the normalisation returns an empty string. link NEXT-1118 --- .../compute-changed-path.test.ts | 60 +++++++++++++++++++ .../router-reducer/compute-changed-path.ts | 17 ++++-- 2 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 packages/next/src/client/components/router-reducer/compute-changed-path.test.ts diff --git a/packages/next/src/client/components/router-reducer/compute-changed-path.test.ts b/packages/next/src/client/components/router-reducer/compute-changed-path.test.ts new file mode 100644 index 000000000000..835cf9933b5d --- /dev/null +++ b/packages/next/src/client/components/router-reducer/compute-changed-path.test.ts @@ -0,0 +1,60 @@ +import { computeChangedPath } from './compute-changed-path' + +describe('computeChangedPath', () => { + it('should return the correct path', () => { + expect( + computeChangedPath( + [ + '', + { + children: [ + '(marketing)', + { + children: ['__PAGE__', {}], + modal: [ + '(...)stats', + { + children: [ + ['key', 'github', 'd'], + { + children: ['__PAGE__', {}], + }, + ], + }, + ], + }, + ], + }, + undefined, + undefined, + true, + ], + [ + '', + { + children: [ + '(marketing)', + { + children: ['__PAGE__', {}], + modal: [ + '(...)stats', + { + children: [ + ['key', 'github', 'd'], + { + children: ['__PAGE__', {}], + }, + ], + }, + ], + }, + ], + }, + undefined, + undefined, + true, + ] + ) + ).toBe('/') + }) +}) diff --git a/packages/next/src/client/components/router-reducer/compute-changed-path.ts b/packages/next/src/client/components/router-reducer/compute-changed-path.ts index 29bbc7f7e87c..763b689083aa 100644 --- a/packages/next/src/client/components/router-reducer/compute-changed-path.ts +++ b/packages/next/src/client/components/router-reducer/compute-changed-path.ts @@ -11,13 +11,18 @@ const segmentToPathname = (segment: Segment): string => { } function normalizePathname(pathname: string): string { - return pathname.split('/').reduce((acc, segment) => { - if (segment === '' || (segment.startsWith('(') && segment.endsWith(')'))) { - return acc - } + return ( + pathname.split('/').reduce((acc, segment) => { + if ( + segment === '' || + (segment.startsWith('(') && segment.endsWith(')')) + ) { + return acc + } - return `${acc}/${segment}` - }, '') + return `${acc}/${segment}` + }, '') || '/' + ) } export function extractPathFromFlightRouterState(