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

Add client router filter handling #46283

Merged
merged 6 commits into from
Feb 23, 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: 4 additions & 0 deletions packages/next/src/build/build-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Rewrite } from '../lib/load-custom-routes'
import { __ApiPreviewProps } from '../server/api-utils'
import { NextConfigComplete } from '../server/config-shared'
import { Span } from '../trace'
import type getBaseWebpackConfig from './webpack-config'
import { TelemetryPlugin } from './webpack/plugins/telemetry-plugin'

// a global object to store context for the current build
Expand Down Expand Up @@ -48,4 +49,7 @@ export const NextBuildContext: Partial<{
reactProductionProfiling: boolean
noMangling: boolean
appDirOnly: boolean
clientRouterFilters: Parameters<
typeof getBaseWebpackConfig
>[1]['clientRouterFilters']
}> = {}
13 changes: 13 additions & 0 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ import { webpackBuild } from './webpack-build'
import { NextBuildContext } from './build-context'
import { normalizePathSep } from '../shared/lib/page-path/normalize-path-sep'
import { isAppRouteRoute } from '../lib/is-app-route-route'
import { createClientRouterFilter } from '../lib/create-router-client-filter'

export type SsgRoute = {
initialRevalidateSeconds: number | false
Expand Down Expand Up @@ -840,6 +841,18 @@ export default async function build(
...rewrites.fallback,
]

if (config.experimental.clientRouterFilter) {
const nonInternalRedirects = redirects.filter(
(redir) => !(redir as any).internal
)
const clientRouterFilters = createClientRouterFilter(
appPageKeys,
nonInternalRedirects
)

NextBuildContext.clientRouterFilters = clientRouterFilters
}

const distDirCreated = await nextBuildSpan
.traceChild('create-dist-dir')
.traceAsyncFn(async () => {
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/build/webpack-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ async function webpackBuildImpl(): Promise<{
rewrites: NextBuildContext.rewrites!,
reactProductionProfiling: NextBuildContext.reactProductionProfiling!,
noMangling: NextBuildContext.noMangling!,
clientRouterFilters: NextBuildContext.clientRouterFilters!,
}

const configs = await runWebpackSpan
Expand Down
23 changes: 23 additions & 0 deletions packages/next/src/build/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ export function getDefineEnv({
isNodeServer,
isEdgeServer,
middlewareMatchers,
clientRouterFilters,
}: {
dev?: boolean
distDir: string
Expand All @@ -181,6 +182,9 @@ export function getDefineEnv({
isEdgeServer?: boolean
middlewareMatchers?: MiddlewareMatcher[]
config: NextConfigComplete
clientRouterFilters: Parameters<
typeof getBaseWebpackConfig
>[1]['clientRouterFilters']
}) {
return {
// internal field to identify the plugin config
Expand Down Expand Up @@ -229,6 +233,15 @@ export function getDefineEnv({
'process.env.__NEXT_NEW_LINK_BEHAVIOR': JSON.stringify(
config.experimental.newNextLinkBehavior
),
'process.env.__NEXT_CLIENT_ROUTER_FILTER_ENABLED': JSON.stringify(
config.experimental.clientRouterFilter
),
'process.env.__NEXT_CLIENT_ROUTER_S_FILTER': JSON.stringify(
clientRouterFilters?.staticFilter
),
'process.env.__NEXT_CLIENT_ROUTER_D_FILTER': JSON.stringify(
clientRouterFilters?.dynamicFilter
),
'process.env.__NEXT_OPTIMISTIC_CLIENT_CACHE': JSON.stringify(
config.experimental.optimisticClientCache
),
Expand Down Expand Up @@ -610,6 +623,7 @@ export default async function getBaseWebpackConfig(
jsConfig,
resolvedBaseUrl,
supportedBrowsers,
clientRouterFilters,
}: {
buildId: string
config: NextConfigComplete
Expand All @@ -628,6 +642,14 @@ export default async function getBaseWebpackConfig(
jsConfig: any
resolvedBaseUrl: string | undefined
supportedBrowsers: string[] | undefined
clientRouterFilters?: {
staticFilter: ReturnType<
import('../shared/lib/bloom-filter').BloomFilter['export']
>
dynamicFilter: ReturnType<
import('../shared/lib/bloom-filter').BloomFilter['export']
>
}
}
): Promise<webpack.Configuration> {
const isClient = compilerType === COMPILER_NAMES.client
Expand Down Expand Up @@ -2061,6 +2083,7 @@ export default async function getBaseWebpackConfig(
isNodeServer,
isEdgeServer,
middlewareMatchers,
clientRouterFilters,
})
),
isClient &&
Expand Down
71 changes: 71 additions & 0 deletions packages/next/src/lib/create-router-client-filter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { BloomFilter } from '../shared/lib/bloom-filter'
import { isDynamicRoute } from '../shared/lib/router/utils'
import { removeTrailingSlash } from '../shared/lib/router/utils/remove-trailing-slash'
import { Redirect } from './load-custom-routes'

const POTENTIAL_ERROR_RATE = 0.02

export function createClientRouterFilter(
paths: string[],
redirects: Redirect[]
): {
staticFilter: ReturnType<BloomFilter['export']>
dynamicFilter: ReturnType<BloomFilter['export']>
} {
const staticPaths = new Set<string>()
const dynamicPaths = new Set<string>()

for (const path of paths) {
if (isDynamicRoute(path)) {
let subPath = ''
const pathParts = path.split('/')

for (let i = 1; i < pathParts.length + 1; i++) {
ijjk marked this conversation as resolved.
Show resolved Hide resolved
const curPart = pathParts[i]

if (curPart.startsWith('[')) {
break
}
subPath = `${subPath}/${curPart}`
}
dynamicPaths.add(subPath)
} else {
staticPaths.add(path)
}
}

for (const redirect of redirects) {
const { source } = redirect
const path = removeTrailingSlash(source)

if (path.includes(':') || path.includes('(')) {
let subPath = ''
const pathParts = path.split('/')

for (let i = 1; i < pathParts.length + 1; i++) {
const curPart = pathParts[i]

if (curPart.includes(':') || curPart.includes('(')) {
break
}
subPath = `${subPath}/${curPart}`
}

dynamicPaths.add(subPath)
} else {
staticPaths.add(path)
}
}

const staticFilter = BloomFilter.from([...staticPaths], POTENTIAL_ERROR_RATE)

const dynamicFilter = BloomFilter.from(
[...dynamicPaths],
POTENTIAL_ERROR_RATE
)
const data = {
staticFilter: staticFilter.export(),
dynamicFilter: dynamicFilter.export(),
}
return data
}
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 @@ -247,6 +247,9 @@ const configSchema = {
},
type: 'object',
},
clientRouterFilter: {
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 @@ -113,6 +113,7 @@ export interface NextJsWebpackConfig {
}

export interface ExperimentalConfig {
clientRouterFilter?: boolean
externalMiddlewareRewritesResolve?: boolean
extensionAlias?: Record<string, any>
allowedRevalidateHeaderKeys?: string[]
Expand Down Expand Up @@ -619,6 +620,7 @@ export const defaultConfig: NextConfig = {
output: !!process.env.NEXT_PRIVATE_STANDALONE ? 'standalone' : undefined,
modularizeImports: undefined,
experimental: {
clientRouterFilter: false,
preCompiledNextServer: false,
fetchCache: false,
middlewarePrefetch: 'flexible',
Expand Down
22 changes: 14 additions & 8 deletions packages/next/src/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,20 @@ function assignDefaults(
value
) as (keyof ExperimentalConfig)[]) {
const featureValue = value[featureName]
if (
featureName === 'appDir' &&
featureValue === true &&
!isAboveNodejs16
) {
throw new Error(
`experimental.appDir requires Node v${NODE_16_VERSION} or later.`
)
if (featureName === 'appDir' && featureValue === true) {
if (!isAboveNodejs16) {
throw new Error(
`experimental.appDir requires Node v${NODE_16_VERSION} or later.`
)
}
// auto enable clientRouterFilter if not manually set
// when appDir is enabled
if (
typeof userConfig.experimental.clientRouterFilter ===
'undefined'
) {
userConfig.experimental.clientRouterFilter = true
}
}
if (
value[featureName] !== defaultConfig.experimental[featureName]
Expand Down
20 changes: 20 additions & 0 deletions packages/next/src/server/dev/next-dev-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +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 { NextBuildContext } from '../../build/build-context'
import { logAppDirError } from './log-app-dir-error'
import { createClientRouterFilter } from '../../lib/create-router-client-filter'

// Load ReactDevOverlay only when needed
let ReactDevOverlayImpl: FunctionComponent
Expand Down Expand Up @@ -411,6 +412,7 @@ export default class DevServer extends Server {
wp.watch({ directories: [this.dir], startTime: 0 })
const fileWatchTimes = new Map()
let enabledTypeScript = this.usingTypeScript
let previousClientRouterFilters: any

wp.on('aggregated', async () => {
let middlewareMatchers: MiddlewareMatcher[] | undefined
Expand Down Expand Up @@ -578,6 +580,23 @@ export default class DevServer extends Server {
Log.error(` "${pagesPath}" - "${appPath}"`)
}
}
let clientRouterFilters: any

if (this.nextConfig.experimental.clientRouterFilter) {
clientRouterFilters = createClientRouterFilter(
Object.keys(appPaths),
this.customRoutes.redirects.filter((r) => !(r as any).internal)
)

if (
!previousClientRouterFilters ||
JSON.stringify(previousClientRouterFilters) !==
JSON.stringify(clientRouterFilters)
) {
envChange = true
previousClientRouterFilters = clientRouterFilters
}
}

if (!this.usingTypeScript && enabledTypeScript) {
// we tolerate the error here as this is best effort
Expand Down Expand Up @@ -664,6 +683,7 @@ export default class DevServer extends Server {
hasRewrites,
isNodeServer,
isEdgeServer,
clientRouterFilters,
})

Object.keys(plugin.definitions).forEach((key) => {
Expand Down
Loading