Skip to content

Commit

Permalink
Update client router filter to separate redirects handling (#46752)
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
ijjk authored Mar 4, 2023
1 parent 35d8e83 commit 33827ed
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 7 deletions.
4 changes: 3 additions & 1 deletion packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,9 @@ export default async function build(
)
const clientRouterFilters = createClientRouterFilter(
appPageKeys,
nonInternalRedirects
config.experimental.clientRouterFilterRedirects
? nonInternalRedirects
: []
)

NextBuildContext.clientRouterFilters = clientRouterFilters
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/lib/create-client-router-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[],
Expand Down
3 changes: 3 additions & 0 deletions packages/next/src/server/config-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,9 @@ const configSchema = {
clientRouterFilter: {
type: 'boolean',
},
clientRouterFilterRedirects: {
type: 'boolean',
},
cpus: {
type: 'number',
},
Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/server/config-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ export interface NextJsWebpackConfig {

export interface ExperimentalConfig {
clientRouterFilter?: boolean
clientRouterFilterRedirects?: boolean
externalMiddlewareRewritesResolve?: boolean
extensionAlias?: Record<string, any>
allowedRevalidateHeaderKeys?: string[]
Expand Down Expand Up @@ -621,6 +622,7 @@ export const defaultConfig: NextConfig = {
modularizeImports: undefined,
experimental: {
clientRouterFilter: false,
clientRouterFilterRedirects: false,
preCompiledNextServer: false,
fetchCacheKeyPrefix: '',
middlewarePrefetch: 'flexible',
Expand Down
8 changes: 5 additions & 3 deletions packages/next/src/server/dev/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
(r: any) => !r.internal
)
this.nextConfig.experimental.clientRouterFilterRedirects
? ((this.nextConfig as any)._originalRedirects || []).filter(
(r: any) => !r.internal
)
: []
)

if (
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/shared/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions test/e2e/app-dir/app-middleware/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ module.exports = {
experimental: {
appDir: true,
clientRouterFilter: true,
clientRouterFilterRedirects: true,
},
}
13 changes: 13 additions & 0 deletions test/e2e/app-dir/app/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('/')
Expand Down
1 change: 1 addition & 0 deletions test/e2e/app-dir/app/next.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
experimental: {
appDir: true,
clientRouterFilterRedirects: true,
sri: {
algorithm: 'sha256',
},
Expand Down
1 change: 1 addition & 0 deletions test/e2e/middleware-redirects/app/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module.exports = {
},
experimental: {
clientRouterFilter: true,
clientRouterFilterRedirects: true,
},
redirects() {
return [
Expand Down

0 comments on commit 33827ed

Please sign in to comment.