Unverified Commit 2bdcaa0f authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Add experimental config for navigation raf test (#62668)

This adds a new experimental config for testing. 

x-ref: [slack thread](https://vercel.slack.com/archives/C06LP23L4NT)

Closes NEXT-2644
parent c262e611
...@@ -194,6 +194,9 @@ export function getDefineEnv({ ...@@ -194,6 +194,9 @@ export function getDefineEnv({
'process.env.__NEXT_NO_MIDDLEWARE_URL_NORMALIZE': JSON.stringify( 'process.env.__NEXT_NO_MIDDLEWARE_URL_NORMALIZE': JSON.stringify(
config.skipMiddlewareUrlNormalize config.skipMiddlewareUrlNormalize
), ),
'process.env.__NEXT_NAVIGATION_RAF': JSON.stringify(
config.experimental.navigationRAF
),
'process.env.__NEXT_EXTERNAL_MIDDLEWARE_REWRITE_RESOLVE': JSON.stringify( 'process.env.__NEXT_EXTERNAL_MIDDLEWARE_REWRITE_RESOLVE': JSON.stringify(
config.experimental.externalMiddlewareRewritesResolve config.experimental.externalMiddlewareRewritesResolve
), ),
......
...@@ -225,15 +225,38 @@ function useChangeByServerResponse( ...@@ -225,15 +225,38 @@ function useChangeByServerResponse(
function useNavigate(dispatch: React.Dispatch<ReducerActions>): RouterNavigate { function useNavigate(dispatch: React.Dispatch<ReducerActions>): RouterNavigate {
return useCallback( return useCallback(
(href, navigateType, shouldScroll) => { (href, navigateType, shouldScroll) => {
const url = new URL(addBasePath(href), location.href) const navigateWrapper = process.env.__NEXT_NAVIGATION_RAF
? (cb: () => void) => {
return dispatch({ let finished = false
type: ACTION_NAVIGATE,
url, // if the frame is hidden or requestAnimationFrame
isExternalUrl: isExternalURL(url), // is delayed too long add upper bound timeout
locationSearch: location.search, setTimeout(() => {
shouldScroll: shouldScroll ?? true, if (!finished) {
navigateType, finished = true
cb()
}
}, 1000)
requestAnimationFrame(() => {
setTimeout(() => {
finished = true
cb()
}, 1)
})
}
: (cb: () => void) => cb()
navigateWrapper(() => {
const url = new URL(addBasePath(href), location.href)
return dispatch({
type: ACTION_NAVIGATE,
url,
isExternalUrl: isExternalURL(url),
locationSearch: location.search,
shouldScroll: shouldScroll ?? true,
navigateType,
})
}) })
}, },
[dispatch] [dispatch]
......
...@@ -276,6 +276,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => ...@@ -276,6 +276,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
linkNoTouchStart: z.boolean().optional(), linkNoTouchStart: z.boolean().optional(),
manualClientBasePath: z.boolean().optional(), manualClientBasePath: z.boolean().optional(),
middlewarePrefetch: z.enum(['strict', 'flexible']).optional(), middlewarePrefetch: z.enum(['strict', 'flexible']).optional(),
navigationRAF: z.boolean().optional(),
nextScriptWorkers: z.boolean().optional(), nextScriptWorkers: z.boolean().optional(),
// The critter option is unknown, use z.any() here // The critter option is unknown, use z.any() here
optimizeCss: z.union([z.boolean(), z.any()]).optional(), optimizeCss: z.union([z.boolean(), z.any()]).optional(),
......
...@@ -167,6 +167,7 @@ export interface NextJsWebpackConfig { ...@@ -167,6 +167,7 @@ export interface NextJsWebpackConfig {
} }
export interface ExperimentalConfig { export interface ExperimentalConfig {
navigationRAF?: boolean
prerenderEarlyExit?: boolean prerenderEarlyExit?: boolean
linkNoTouchStart?: boolean linkNoTouchStart?: boolean
caseSensitiveRoutes?: boolean caseSensitiveRoutes?: boolean
...@@ -838,6 +839,7 @@ export const defaultConfig: NextConfig = { ...@@ -838,6 +839,7 @@ export const defaultConfig: NextConfig = {
output: !!process.env.NEXT_PRIVATE_STANDALONE ? 'standalone' : undefined, output: !!process.env.NEXT_PRIVATE_STANDALONE ? 'standalone' : undefined,
modularizeImports: undefined, modularizeImports: undefined,
experimental: { experimental: {
navigationRAF: false,
prerenderEarlyExit: false, prerenderEarlyExit: false,
serverMinification: true, serverMinification: true,
serverSourceMaps: false, serverSourceMaps: false,
......
...@@ -1498,20 +1498,32 @@ export default class Router implements BaseRouter { ...@@ -1498,20 +1498,32 @@ export default class Router implements BaseRouter {
const isErrorRoute = this.pathname === '/404' || this.pathname === '/_error' const isErrorRoute = this.pathname === '/404' || this.pathname === '/_error'
try { try {
let routeInfo = await this.getRouteInfo({ let [routeInfo] = await Promise.all([
route, this.getRouteInfo({
pathname, route,
query, pathname,
as, query,
resolvedAs, as,
routeProps, resolvedAs,
locale: nextState.locale, routeProps,
isPreview: nextState.isPreview, locale: nextState.locale,
hasMiddleware: isMiddlewareMatch, isPreview: nextState.isPreview,
unstable_skipClientCache: options.unstable_skipClientCache, hasMiddleware: isMiddlewareMatch,
isQueryUpdating: isQueryUpdating && !this.isFallback, unstable_skipClientCache: options.unstable_skipClientCache,
isMiddlewareRewrite, isQueryUpdating: isQueryUpdating && !this.isFallback,
}) isMiddlewareRewrite,
}),
process.env.__NEXT_NAVIGATION_RAF
? new Promise((resolve) => {
// if the frame is hidden or requestAnimationFrame
// is delayed too long add upper bound timeout
setTimeout(resolve, 1000)
requestAnimationFrame(() => {
setTimeout(resolve, 1)
})
})
: Promise.resolve(),
])
if (!isQueryUpdating && !options.shallow) { if (!isQueryUpdating && !options.shallow) {
await this._bfl( await this._bfl(
......
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