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(
appPageKeys,
nonInternalRedirects
config.experimental.clientRouterFilterRedirects
? nonInternalRedirects
: []
)
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 { tryToParsePath } from './try-to-parse-path'
const POTENTIAL_ERROR_RATE = 0.02
const POTENTIAL_ERROR_RATE = 0.01
export function createClientRouterFilter(
paths: string[],

View file

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

View file

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

View file

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

View file

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

View file

@ -2,5 +2,6 @@ module.exports = {
experimental: {
appDir: 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) {
it('should not have duplicate config warnings', async () => {
await next.fetch('/')

View file

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

View file

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