Unverified Commit 4b603cfa authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

Fix interception routes with trailing slash configuration (#86787)

### What?

This PR fixes interception routes to work correctly with the
`trailingSlash` configuration option.

### Why?

Interception routes were not working when users had `trailingSlash:
true` in their Next.js config. This was because interception route
rewrites were marked as `internal: true`, which caused them to bypass
the `modifyRouteRegex` processing that handles trailing slash
normalization.

When a route is marked as internal and provides its own regex, the code
would skip the regex modification step that ensures routes work
correctly with both `trailingSlash: true` and `trailingSlash: false`
configurations. This meant interception routes would fail to match when
trailing slashes were enabled.

### How?

1. **Removed `internal: true` flag** from interception route rewrites in
`generate-interception-routes-rewrites.ts`. This ensures they go through
the same regex modification process as regular routes, properly handling
trailing slash normalization.

2. **Added comprehensive test coverage** by wrapping the parallel routes
and interception test suite in `describe.each([true, false])` to test
both trailing slash configurations. Updated all URL assertions to
account for the trailing slash setting.

3. **Refactored `build-custom-route.ts`** to scope the `source` variable
closer to where it's actually used, preventing unnecessary computation
for internal routes that already provide a regex.

4. **Moved `next.config.js` inline** to the test file for better
organization and to support the parameterized test structure.
parent 77144703
...@@ -37,14 +37,6 @@ export function buildCustomRoute( ...@@ -37,14 +37,6 @@ export function buildCustomRoute(
delimiter: '/', // default is `/#?`, but Next does not pass query info delimiter: '/', // default is `/#?`, but Next does not pass query info
}) })
let source = compiled.source
if (!route.internal) {
source = modifyRouteRegex(
source,
type === 'redirect' ? restrictedRedirectPaths : undefined
)
}
// If this is an internal rewrite and it already provides a regex, use it // If this is an internal rewrite and it already provides a regex, use it
// otherwise, normalize the source to a regex. // otherwise, normalize the source to a regex.
let regex: string let regex: string
...@@ -54,6 +46,14 @@ export function buildCustomRoute( ...@@ -54,6 +46,14 @@ export function buildCustomRoute(
!('regex' in route) || !('regex' in route) ||
typeof route.regex !== 'string' typeof route.regex !== 'string'
) { ) {
let source = compiled.source
if (!route.internal) {
source = modifyRouteRegex(
source,
type === 'redirect' ? restrictedRedirectPaths : undefined
)
}
regex = normalizeRouteRegex(source) regex = normalizeRouteRegex(source)
} else { } else {
regex = route.regex regex = route.regex
......
...@@ -52,7 +52,6 @@ export function generateInterceptionRoutesRewrites( ...@@ -52,7 +52,6 @@ export function generateInterceptionRoutesRewrites(
value: headerRegex, value: headerRegex,
}, },
], ],
internal: true,
regex: source.namedRegex, regex: source.namedRegex,
}) })
} }
......
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
async rewrites() {
return {
beforeFiles: [
{
source: '/foo',
destination: '/en/foo',
},
{
source: '/photos',
destination: '/en/photos',
},
],
}
},
}
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