Unverified Commit 53e4627c authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Update app router filter handling (#47761)

This ensures we check if a path is `appRouter` during prefetching so
that we can trigger hard navigations quicker when routing from pages ->
app. An additional config is also exposed to allow configuring the
potential false positive rate for the client filter.

x-ref: [slack
thread](https://vercel.slack.com/archives/C017QMYC5FB/p1680225393243459)
parent 0413fea8
......@@ -892,7 +892,8 @@ export default async function build(
appPageKeys,
config.experimental.clientRouterFilterRedirects
? nonInternalRedirects
: []
: [],
config.experimental.clientRouterFilterAllowedRate
)
NextBuildContext.clientRouterFilters = clientRouterFilters
......
......@@ -9,7 +9,8 @@ const POTENTIAL_ERROR_RATE = 0.01
export function createClientRouterFilter(
paths: string[],
redirects: Redirect[]
redirects: Redirect[],
allowedErrorRate: number = POTENTIAL_ERROR_RATE
): {
staticFilter: ReturnType<BloomFilter['export']>
dynamicFilter: ReturnType<BloomFilter['export']>
......@@ -56,12 +57,9 @@ export function createClientRouterFilter(
}
}
const staticFilter = BloomFilter.from([...staticPaths], POTENTIAL_ERROR_RATE)
const staticFilter = BloomFilter.from([...staticPaths], allowedErrorRate)
const dynamicFilter = BloomFilter.from(
[...dynamicPaths],
POTENTIAL_ERROR_RATE
)
const dynamicFilter = BloomFilter.from([...dynamicPaths], allowedErrorRate)
const data = {
staticFilter: staticFilter.export(),
dynamicFilter: dynamicFilter.export(),
......
......@@ -256,6 +256,9 @@ const configSchema = {
clientRouterFilterRedirects: {
type: 'boolean',
},
clientRouterFilterAllowedRate: {
type: 'number',
},
cpus: {
type: 'number',
},
......
......@@ -116,6 +116,10 @@ export interface NextJsWebpackConfig {
export interface ExperimentalConfig {
clientRouterFilter?: boolean
clientRouterFilterRedirects?: boolean
// decimal for percent for possible false positives
// e.g. 0.01 for 10% potential false matches lower
// percent increases size of the filter
clientRouterFilterAllowedRate?: number
externalMiddlewareRewritesResolve?: boolean
extensionAlias?: Record<string, any>
allowedRevalidateHeaderKeys?: string[]
......
......@@ -630,7 +630,8 @@ export default class DevServer extends Server {
? ((this.nextConfig as any)._originalRedirects || []).filter(
(r: any) => !r.internal
)
: []
: [],
this.nextConfig.experimental.clientRouterFilterAllowedRate
)
if (
......
......@@ -1052,7 +1052,12 @@ export default class Router implements BaseRouter {
return this.change('replaceState', url, as, options)
}
async _bfl(as: string, resolvedAs?: string, locale?: string | false) {
async _bfl(
as: string,
resolvedAs?: string,
locale?: string | false,
skipNavigate?: boolean
) {
if (process.env.__NEXT_CLIENT_ROUTER_FILTER_ENABLED) {
let matchesBflStatic = false
let matchesBflDynamic = false
......@@ -1095,6 +1100,9 @@ export default class Router implements BaseRouter {
// if the client router filter is matched then we trigger
// a hard navigation
if (matchesBflStatic || matchesBflDynamic) {
if (skipNavigate) {
return true
}
handleHardNavigation({
url: addBasePath(
addLocale(as, locale || this.locale, this.defaultLocale)
......@@ -1107,6 +1115,7 @@ export default class Router implements BaseRouter {
}
}
}
return false
}
private async change(
......@@ -1311,6 +1320,13 @@ export default class Router implements BaseRouter {
let parsed = parseRelativeUrl(url)
let { pathname, query } = parsed
// if we detected the path as app route during prefetching
// trigger hard navigation
if ((this.components[pathname] as any)?.__appRouter) {
handleHardNavigation({ url, router: this })
return new Promise(() => {})
}
// The build manifest needs to be loaded before auto-static dynamic pages
// get their query parameters to allow ensuring they can be parsed properly
// when rewritten to
......@@ -2276,6 +2292,7 @@ export default class Router implements BaseRouter {
return
}
let parsed = parseRelativeUrl(url)
const urlPathname = parsed.pathname
let { pathname, query } = parsed
const originalPathname = pathname
......@@ -2412,6 +2429,10 @@ export default class Router implements BaseRouter {
const route = removeTrailingSlash(pathname)
if (await this._bfl(asPath, resolvedAs, options.locale, true)) {
this.components[urlPathname] = { __appRouter: true } as any
}
await Promise.all([
this.pageLoader._isSsg(route).then((isSsg) => {
return isSsg
......
......@@ -16,6 +16,24 @@ createNextDescribe(
},
},
({ next, isNextDev: isDev, isNextStart, isNextDeploy }) => {
if (!isDev) {
it('should successfully detect app route during prefetch', async () => {
const browser = await next.browser('/')
await check(async () => {
const found = await browser.eval(
'!!window.next.router.components["/dashboard"]'
)
return found
? 'success'
: await browser.eval('Object.keys(window.next.router.components)')
}, 'success')
await browser.elementByCss('a').click()
await browser.waitForElementByCss('#from-dashboard')
})
}
it('should encode chunk path correctly', async () => {
await next.fetch('/dynamic-client/first/second')
const browser = await next.browser('/')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment