Unverified Commit 82f4fd3e authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update router filter to be less sensitive (#46515)

This skips including dynamic redirects in the client router filter as it
causes extra unexpected hard navigations.

x-ref: [slack
thread](https://vercel.slack.com/archives/C04MEB9L9RQ/p1677521159911179?thread_ts=1677255545.574209&cid=C04MEB9L9RQ)

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)
parent 9ad1f321
...@@ -132,7 +132,7 @@ import { webpackBuild } from './webpack-build' ...@@ -132,7 +132,7 @@ import { webpackBuild } from './webpack-build'
import { NextBuildContext } from './build-context' import { NextBuildContext } from './build-context'
import { normalizePathSep } from '../shared/lib/page-path/normalize-path-sep' import { normalizePathSep } from '../shared/lib/page-path/normalize-path-sep'
import { isAppRouteRoute } from '../lib/is-app-route-route' import { isAppRouteRoute } from '../lib/is-app-route-route'
import { createClientRouterFilter } from '../lib/create-router-client-filter' import { createClientRouterFilter } from '../lib/create-client-router-filter'
export type SsgRoute = { export type SsgRoute = {
initialRevalidateSeconds: number | false initialRevalidateSeconds: number | false
......
import { Token } from 'next/dist/compiled/path-to-regexp'
import { BloomFilter } from '../shared/lib/bloom-filter' import { BloomFilter } from '../shared/lib/bloom-filter'
import { isDynamicRoute } from '../shared/lib/router/utils' import { isDynamicRoute } from '../shared/lib/router/utils'
import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-slash' import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-slash'
import { Redirect } from './load-custom-routes' import { Redirect } from './load-custom-routes'
import { tryToParsePath } from './try-to-parse-path'
const POTENTIAL_ERROR_RATE = 0.02 const POTENTIAL_ERROR_RATE = 0.02
...@@ -42,26 +44,14 @@ export function createClientRouterFilter( ...@@ -42,26 +44,14 @@ export function createClientRouterFilter(
for (const redirect of redirects) { for (const redirect of redirects) {
const { source } = redirect const { source } = redirect
const path = removeTrailingSlash(source) const path = removeTrailingSlash(source)
let tokens: Token[] = []
if (path.includes(':') || path.includes('(')) { try {
let subPath = '' tokens = tryToParsePath(source).tokens || []
const pathParts = path.split('/') } catch (_) {}
for (let i = 1; i < pathParts.length + 1; i++) {
const curPart = pathParts[i]
if (curPart.includes(':') || curPart.includes('(')) {
break
}
subPath = `${subPath}/${curPart}`
}
// if redirect has matcher at top-level we don't include this if (tokens.every((token) => typeof token === 'string')) {
// as it would match everything // only include static redirects initially
if (subPath) {
dynamicPaths.add(subPath)
}
} else {
staticPaths.add(path) staticPaths.add(path)
} }
} }
......
...@@ -96,7 +96,7 @@ import { CachedFileReader } from '../future/route-matcher-providers/dev/helpers/ ...@@ -96,7 +96,7 @@ import { CachedFileReader } from '../future/route-matcher-providers/dev/helpers/
import { DefaultFileReader } from '../future/route-matcher-providers/dev/helpers/file-reader/default-file-reader' import { DefaultFileReader } from '../future/route-matcher-providers/dev/helpers/file-reader/default-file-reader'
import { NextBuildContext } from '../../build/build-context' import { NextBuildContext } from '../../build/build-context'
import { logAppDirError } from './log-app-dir-error' import { logAppDirError } from './log-app-dir-error'
import { createClientRouterFilter } from '../../lib/create-router-client-filter' import { createClientRouterFilter } from '../../lib/create-client-router-filter'
// Load ReactDevOverlay only when needed // Load ReactDevOverlay only when needed
let ReactDevOverlayImpl: FunctionComponent let ReactDevOverlayImpl: FunctionComponent
......
...@@ -1063,7 +1063,7 @@ export default class Router implements BaseRouter { ...@@ -1063,7 +1063,7 @@ export default class Router implements BaseRouter {
new URL(curAs, 'http://n').pathname new URL(curAs, 'http://n').pathname
) )
const asNoSlashLocale = addBasePath( const asNoSlashLocale = addBasePath(
addLocale(curAs, locale || this.locale) addLocale(asNoSlash, locale || this.locale)
) )
if ( if (
......
...@@ -20,10 +20,8 @@ createNextDescribe( ...@@ -20,10 +20,8 @@ createNextDescribe(
{ pathname: '/redirect-1' }, { pathname: '/redirect-1' },
{ pathname: '/redirect-2' }, { pathname: '/redirect-2' },
{ pathname: '/blog/old-post' }, { pathname: '/blog/old-post' },
{ pathname: '/redirect-3/some/value' },
{ pathname: '/redirect-3/some' }, { pathname: '/redirect-3/some' },
{ pathname: '/redirect-4' }, { pathname: '/redirect-4' },
{ pathname: '/redirect-4/another' },
])( ])(
'should match redirects in pages correctly $path', 'should match redirects in pages correctly $path',
async ({ pathname }) => { async ({ pathname }) => {
......
...@@ -45,12 +45,12 @@ module.exports = { ...@@ -45,12 +45,12 @@ module.exports = {
permanent: false, permanent: false,
}, },
{ {
source: '/redirect-3/some/:path*', source: '/redirect-3/some',
destination: 'https://example.vercel.sh', destination: 'https://example.vercel.sh',
permanent: false, permanent: false,
}, },
{ {
source: '/redirect-4/:path*', source: '/redirect-4',
destination: 'https://example.vercel.sh', destination: 'https://example.vercel.sh',
permanent: false, permanent: false,
}, },
......
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