Update client router filter to separate redirects handling (#46752)

x-ref: [slack
thread](https://vercel.slack.com/archives/C017QMYC5FB/p1677875647422339)
x-ref: [slack
thread](https://vercel.slack.com/archives/C049YV4U2F6/p1677875992732789)

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)
This commit is contained in:
JJ Kasper 2023-03-03 16:02:02 -08:00 committed by GitHub
parent 35d8e8379e
commit 33827ede54
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 32 additions and 7 deletions

View file

@ -850,7 +850,9 @@ export default async function build(
) )
const clientRouterFilters = createClientRouterFilter( const clientRouterFilters = createClientRouterFilter(
appPageKeys, appPageKeys,
nonInternalRedirects config.experimental.clientRouterFilterRedirects
? nonInternalRedirects
: []
) )
NextBuildContext.clientRouterFilters = clientRouterFilters NextBuildContext.clientRouterFilters = clientRouterFilters

View file

@ -5,7 +5,7 @@ import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-
import { Redirect } from './load-custom-routes' import { Redirect } from './load-custom-routes'
import { tryToParsePath } from './try-to-parse-path' import { tryToParsePath } from './try-to-parse-path'
const POTENTIAL_ERROR_RATE = 0.02 const POTENTIAL_ERROR_RATE = 0.01
export function createClientRouterFilter( export function createClientRouterFilter(
paths: string[], paths: string[],

View file

@ -253,6 +253,9 @@ const configSchema = {
clientRouterFilter: { clientRouterFilter: {
type: 'boolean', type: 'boolean',
}, },
clientRouterFilterRedirects: {
type: 'boolean',
},
cpus: { cpus: {
type: 'number', type: 'number',
}, },

View file

@ -115,6 +115,7 @@ export interface NextJsWebpackConfig {
export interface ExperimentalConfig { export interface ExperimentalConfig {
clientRouterFilter?: boolean clientRouterFilter?: boolean
clientRouterFilterRedirects?: boolean
externalMiddlewareRewritesResolve?: boolean externalMiddlewareRewritesResolve?: boolean
extensionAlias?: Record<string, any> extensionAlias?: Record<string, any>
allowedRevalidateHeaderKeys?: string[] allowedRevalidateHeaderKeys?: string[]
@ -621,6 +622,7 @@ export const defaultConfig: NextConfig = {
modularizeImports: undefined, modularizeImports: undefined,
experimental: { experimental: {
clientRouterFilter: false, clientRouterFilter: false,
clientRouterFilterRedirects: false,
preCompiledNextServer: false, preCompiledNextServer: false,
fetchCacheKeyPrefix: '', fetchCacheKeyPrefix: '',
middlewarePrefetch: 'flexible', middlewarePrefetch: 'flexible',

View file

@ -585,9 +585,11 @@ export default class DevServer extends Server {
if (this.nextConfig.experimental.clientRouterFilter) { if (this.nextConfig.experimental.clientRouterFilter) {
clientRouterFilters = createClientRouterFilter( clientRouterFilters = createClientRouterFilter(
Object.keys(appPaths), Object.keys(appPaths),
((this.nextConfig as any)._originalRedirects || []).filter( this.nextConfig.experimental.clientRouterFilterRedirects
(r: any) => !r.internal ? ((this.nextConfig as any)._originalRedirects || []).filter(
) (r: any) => !r.internal
)
: []
) )
if ( if (

View file

@ -1123,7 +1123,7 @@ export default class Router implements BaseRouter {
// any time without notice. // any time without notice.
const isQueryUpdating = (options as any)._h === 1 const isQueryUpdating = (options as any)._h === 1
if (!isQueryUpdating) { if (!isQueryUpdating && !options.shallow) {
await this._bfl(as, undefined, options.locale) await this._bfl(as, undefined, options.locale)
} }
@ -1509,7 +1509,7 @@ export default class Router implements BaseRouter {
isMiddlewareRewrite, isMiddlewareRewrite,
}) })
if (!isQueryUpdating) { if (!isQueryUpdating && !options.shallow) {
await this._bfl( await this._bfl(
as, as,
'resolvedAs' in routeInfo ? routeInfo.resolvedAs : undefined, 'resolvedAs' in routeInfo ? routeInfo.resolvedAs : undefined,

View file

@ -2,5 +2,6 @@ module.exports = {
experimental: { experimental: {
appDir: true, appDir: true,
clientRouterFilter: true, clientRouterFilter: true,
clientRouterFilterRedirects: true,
}, },
} }

View file

@ -70,6 +70,19 @@ createNextDescribe(
} }
) )
it('should not apply client router filter on shallow', async () => {
const browser = await next.browser('/')
await browser.eval('window.beforeNav = 1')
await check(async () => {
await browser.eval(
`window.next.router.push('/', '/redirect-1', { shallow: true })`
)
return await browser.eval('window.location.pathname')
}, '/redirect-1')
expect(await browser.eval('window.beforeNav')).toBe(1)
})
if (isDev) { if (isDev) {
it('should not have duplicate config warnings', async () => { it('should not have duplicate config warnings', async () => {
await next.fetch('/') await next.fetch('/')

View file

@ -1,6 +1,7 @@
module.exports = { module.exports = {
experimental: { experimental: {
appDir: true, appDir: true,
clientRouterFilterRedirects: true,
sri: { sri: {
algorithm: 'sha256', algorithm: 'sha256',
}, },

View file

@ -5,6 +5,7 @@ module.exports = {
}, },
experimental: { experimental: {
clientRouterFilter: true, clientRouterFilter: true,
clientRouterFilterRedirects: true,
}, },
redirects() { redirects() {
return [ return [