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,6 +225,28 @@ function useChangeByServerResponse( ...@@ -225,6 +225,28 @@ 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 navigateWrapper = process.env.__NEXT_NAVIGATION_RAF
? (cb: () => void) => {
let finished = false
// if the frame is hidden or requestAnimationFrame
// is delayed too long add upper bound timeout
setTimeout(() => {
if (!finished) {
finished = true
cb()
}
}, 1000)
requestAnimationFrame(() => {
setTimeout(() => {
finished = true
cb()
}, 1)
})
}
: (cb: () => void) => cb()
navigateWrapper(() => {
const url = new URL(addBasePath(href), location.href) const url = new URL(addBasePath(href), location.href)
return dispatch({ return dispatch({
...@@ -235,6 +257,7 @@ function useNavigate(dispatch: React.Dispatch<ReducerActions>): RouterNavigate { ...@@ -235,6 +257,7 @@ function useNavigate(dispatch: React.Dispatch<ReducerActions>): RouterNavigate {
shouldScroll: shouldScroll ?? true, shouldScroll: shouldScroll ?? true,
navigateType, 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,7 +1498,8 @@ export default class Router implements BaseRouter { ...@@ -1498,7 +1498,8 @@ 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([
this.getRouteInfo({
route, route,
pathname, pathname,
query, query,
...@@ -1511,7 +1512,18 @@ export default class Router implements BaseRouter { ...@@ -1511,7 +1512,18 @@ export default class Router implements BaseRouter {
unstable_skipClientCache: options.unstable_skipClientCache, unstable_skipClientCache: options.unstable_skipClientCache,
isQueryUpdating: isQueryUpdating && !this.isFallback, isQueryUpdating: isQueryUpdating && !this.isFallback,
isMiddlewareRewrite, 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