Unverified Commit 78a2eb0b authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

fix interception routes with dynamic segments (#59273)

### What?
Using an interception marker next to a dynamic segment does not behave
properly when deployed to Vercel

### Why?
The named route regex that gets created is not accounting for the
interception marker, which is causing the non-intercepted route to match
the intercepted serverless function.

### How?
This factors in the interception marker when building the named route
regex so that the non-intercepted route regex properly matches when
loading the non-intercepted page.

Deployment verified here: https://test-intercept-mu.vercel.app/

Closes NEXT-1786
Fixes #54650
parent 2003f5f8
...@@ -24,6 +24,18 @@ describe('getNamedRouteRegex', () => { ...@@ -24,6 +24,18 @@ describe('getNamedRouteRegex', () => {
expect(regex.re.test('/photos/(.)next/123')).toBe(true) expect(regex.re.test('/photos/(.)next/123')).toBe(true)
}) })
it('should match named routes correctly when interception markers are adjacent to dynamic segments', () => {
let regex = getNamedRouteRegex('/(.)[author]/[id]', true)
let namedRegexp = new RegExp(regex.namedRegex)
expect(namedRegexp.test('/[author]/[id]')).toBe(false)
expect(namedRegexp.test('/(.)[author]/[id]')).toBe(true)
regex = getNamedRouteRegex('/(..)(..)[author]/[id]', true)
namedRegexp = new RegExp(regex.namedRegex)
expect(namedRegexp.test('/[author]/[id]')).toBe(false)
expect(namedRegexp.test('/(..)(..)[author]/[id]')).toBe(true)
})
it('should handle multi-level interception markers', () => { it('should handle multi-level interception markers', () => {
const regex = getNamedRouteRegex('/photos/(..)(..)[author]/[id]', true) const regex = getNamedRouteRegex('/photos/(..)(..)[author]/[id]', true)
......
...@@ -97,11 +97,13 @@ function buildGetSafeRouteKey() { ...@@ -97,11 +97,13 @@ function buildGetSafeRouteKey() {
} }
function getSafeKeyFromSegment({ function getSafeKeyFromSegment({
interceptionMarker,
getSafeRouteKey, getSafeRouteKey,
segment, segment,
routeKeys, routeKeys,
keyPrefix, keyPrefix,
}: { }: {
interceptionMarker?: string
getSafeRouteKey: () => string getSafeRouteKey: () => string
segment: string segment: string
routeKeys: Record<string, string> routeKeys: Record<string, string>
...@@ -137,11 +139,18 @@ function getSafeKeyFromSegment({ ...@@ -137,11 +139,18 @@ function getSafeKeyFromSegment({
routeKeys[cleanedKey] = key routeKeys[cleanedKey] = key
} }
// if the segment has an interception marker, make sure that's part of the regex pattern
// this is to ensure that the route with the interception marker doesn't incorrectly match
// the non-intercepted route (ie /app/(.)[username] should not match /app/[username])
const interceptionPrefix = interceptionMarker
? escapeStringRegexp(interceptionMarker)
: ''
return repeat return repeat
? optional ? optional
? `(?:/(?<${cleanedKey}>.+?))?` ? `(?:/${interceptionPrefix}(?<${cleanedKey}>.+?))?`
: `/(?<${cleanedKey}>.+?)` : `/${interceptionPrefix}(?<${cleanedKey}>.+?)`
: `/(?<${cleanedKey}>[^/]+?)` : `/${interceptionPrefix}(?<${cleanedKey}>[^/]+?)`
} }
function getNamedParametrizedRoute(route: string, prefixRouteKeys: boolean) { function getNamedParametrizedRoute(route: string, prefixRouteKeys: boolean) {
...@@ -157,8 +166,11 @@ function getNamedParametrizedRoute(route: string, prefixRouteKeys: boolean) { ...@@ -157,8 +166,11 @@ function getNamedParametrizedRoute(route: string, prefixRouteKeys: boolean) {
const paramMatches = segment.match(/\[((?:\[.*\])|.+)\]/) // Check for parameters const paramMatches = segment.match(/\[((?:\[.*\])|.+)\]/) // Check for parameters
if (hasInterceptionMarker && paramMatches) { if (hasInterceptionMarker && paramMatches) {
const [usedMarker] = segment.split(paramMatches[0])
return getSafeKeyFromSegment({ return getSafeKeyFromSegment({
getSafeRouteKey, getSafeRouteKey,
interceptionMarker: usedMarker,
segment: paramMatches[1], segment: paramMatches[1],
routeKeys, routeKeys,
keyPrefix: prefixRouteKeys keyPrefix: prefixRouteKeys
......
export default function Page() {
return 'not intercepted'
}
export default function Default() {
return null
}
export default function Layout(props: {
children: React.ReactNode
modal: React.ReactNode
}) {
return (
<html>
<body>
<div id="children">{props.children}</div>
<div id="modal">{props.modal}</div>
</body>
</html>
)
}
import Link from 'next/link'
export default function Page() {
return (
<div>
<Link href="/foo/1">Foo</Link> <Link href="/bar/1">Foo</Link>
</div>
)
}
import { createNextDescribe } from 'e2e-utils'
import { check } from 'next-test-utils'
createNextDescribe(
'interception-dynamic-segment',
{
files: __dirname,
},
({ next }) => {
it('should work when interception route is paired with a dynamic segment', async () => {
const browser = await next.browser('/')
await browser.elementByCss('[href="/foo/1"]').click()
await check(() => browser.elementById('modal').text(), /intercepted/)
await browser.refresh()
await check(() => browser.elementById('modal').text(), '')
await check(
() => browser.elementById('children').text(),
/not intercepted/
)
})
}
)
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
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