Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update client router filter to separate redirects handling #46752

Merged
merged 2 commits into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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