Unverified Commit e3a78fd3 authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

refactor: move useDynamic{Route,Search}Params to reduce snapshot churn (#97360)

Relocates `useDynamicRouteParams` and `useDynamicSearchParams` to avoid
having to update `client-hook-abort-reasons.test.ts` every time there's
changes in `dynamic-rendering.ts`. Webpack leaked the source location of
the `React.use` calls inside, so any code change that moved the source
of the hook would require a snapshot update.

For reasons i don't quite understand, moving the hooks to a different
file improved the ignore-listing, i.e. we no longer point to the
internals of the hooks. This is surprising but not unwelcome.
parent 1be0ab80
...@@ -8,4 +8,4 @@ ...@@ -8,4 +8,4 @@
export { export {
useDynamicRouteParams, useDynamicRouteParams,
useDynamicSearchParams, useDynamicSearchParams,
} from '../../server/app-render/dynamic-rendering' } from '../../server/app-render/dynamic-rendering-client'
import React from 'react'
import {
throwForMissingRequestStore,
workUnitAsyncStorage,
} from './work-unit-async-storage.external'
import { workAsyncStorage } from '../app-render/work-async-storage.external'
import {
ClientHookDynamicError,
makeClientHookHangingPromise,
} from '../dynamic-rendering-utils'
import { InvariantError } from '../../shared/lib/invariant-error'
import { BailoutToCSRError } from '../../shared/lib/lazy-dynamic/bailout-to-csr'
// TODO(veil): This module is separated from `dynamic-rendering.ts` as a workaround.
// When these hooks were part of `dynamic-rendering.ts, the source location of these
// `React.use()` calls would show up in Webpack snapshots in `client-hook-abort-reasons.test.ts`,
// which makes changes to the surrounding file needlessly churn the snapshots.
// Until the ignore-listing is fixed, we can make this less annoying by keeping these hooks in a separate file.
export function useDynamicRouteParams(expression: string) {
const workStore = workAsyncStorage.getStore()
const workUnitStore = workUnitAsyncStorage.getStore()
if (workStore && workUnitStore) {
switch (workUnitStore.type) {
case 'prerender-client': {
const fallbackParams = workUnitStore.fallbackRouteParams
if (fallbackParams && fallbackParams.size > 0) {
// We are in a prerender with cacheComponents semantics. We are going to
// hang here and never resolve. This will cause the currently
// rendering component to effectively be a dynamic hole.
React.use(
makeClientHookHangingPromise(
workUnitStore.renderSignal,
new ClientHookDynamicError(workStore.route, expression)
)
)
}
break
}
case 'prerender':
throw new InvariantError(
`\`${expression}\` was called from a Server Component. Next.js should be preventing ${expression} from being included in server components statically, but did not in this case.`
)
case 'validation-client': {
// Don't check fallbackRouteParams here. We handle params that weren't
// provided in the samples using a proxy that throws when accessed.
break
}
case 'prerender-runtime':
throw new InvariantError(
`\`${expression}\` was called during a runtime prerender. Next.js should be preventing ${expression} from being included in server components statically, but did not in this case.`
)
case 'cache':
case 'private-cache':
throw new InvariantError(
`\`${expression}\` was called inside a cache scope. Next.js should be preventing ${expression} from being included in server components statically, but did not in this case.`
)
case 'generate-static-params':
throw new InvariantError(
`\`${expression}\` was called in \`generateStaticParams\`. Next.js should be preventing ${expression} from being included in server component files statically, but did not in this case.`
)
case 'prerender-legacy':
case 'request':
case 'unstable-cache':
break
default:
workUnitStore satisfies never
}
}
}
export function useDynamicSearchParams(expression: string) {
const workStore = workAsyncStorage.getStore()
const workUnitStore = workUnitAsyncStorage.getStore()
if (!workStore) {
// We assume pages router context and just return
return
}
if (!workUnitStore) {
throwForMissingRequestStore(expression)
}
switch (workUnitStore.type) {
case 'validation-client':
// During instant validation we try to behave as close to client as possible,
// so this shouldn't hang during SSR.
return
case 'prerender-client': {
React.use(
makeClientHookHangingPromise(
workUnitStore.renderSignal,
new ClientHookDynamicError(workStore.route, expression)
)
)
break
}
case 'prerender-legacy': {
if (workStore.forceStatic) {
return
}
throw new BailoutToCSRError(expression)
}
case 'prerender':
case 'prerender-runtime':
throw new InvariantError(
`\`${expression}\` was called from a Server Component. Next.js should be preventing ${expression} from being included in server components statically, but did not in this case.`
)
case 'cache':
case 'unstable-cache':
case 'private-cache':
throw new InvariantError(
`\`${expression}\` was called inside a cache scope. Next.js should be preventing ${expression} from being included in server components statically, but did not in this case.`
)
case 'generate-static-params':
throw new InvariantError(
`\`${expression}\` was called in \`generateStaticParams\`. Next.js should be preventing ${expression} from being included in server component files statically, but did not in this case.`
)
case 'request':
return
default:
workUnitStore satisfies never
}
}
...@@ -20,30 +20,23 @@ ...@@ -20,30 +20,23 @@
* read that data outside the cache and pass it in as an argument to the cached function. * read that data outside the cache and pass it in as an argument to the cached function.
*/ */
import type { WorkStore } from '../app-render/work-async-storage.external' import type { WorkStore } from './work-async-storage.external'
import type { import type {
WorkUnitStore, WorkUnitStore,
PrerenderStoreLegacy, PrerenderStoreLegacy,
PrerenderStoreModern, PrerenderStoreModern,
ValidationStoreClient, ValidationStoreClient,
PrerenderStoreModernServer, PrerenderStoreModernServer,
} from '../app-render/work-unit-async-storage.external' } from './work-unit-async-storage.external'
// Once postpone is in stable we should switch to importing the postpone export directly // Once postpone is in stable we should switch to importing the postpone export directly
import React from 'react' import React from 'react'
import { DynamicServerError } from '../../client/components/hooks-server-context' import { DynamicServerError } from '../../client/components/hooks-server-context'
import { StaticGenBailoutError } from '../../client/components/static-generation-bailout' import { StaticGenBailoutError } from '../../client/components/static-generation-bailout'
import { getStagedRenderingController } from './work-unit-async-storage.external'
import { import {
getStagedRenderingController,
throwForMissingRequestStore,
workUnitAsyncStorage,
} from './work-unit-async-storage.external'
import { workAsyncStorage } from '../app-render/work-async-storage.external'
import {
ClientHookDynamicError,
isClientHookDynamicError, isClientHookDynamicError,
makeClientHookHangingPromise,
trackRuntimeDataAccessed, trackRuntimeDataAccessed,
} from '../dynamic-rendering-utils' } from '../dynamic-rendering-utils'
import { import {
...@@ -549,114 +542,6 @@ export function annotateDynamicAccess( ...@@ -549,114 +542,6 @@ export function annotateDynamicAccess(
} }
} }
export function useDynamicRouteParams(expression: string) {
const workStore = workAsyncStorage.getStore()
const workUnitStore = workUnitAsyncStorage.getStore()
if (workStore && workUnitStore) {
switch (workUnitStore.type) {
case 'prerender-client': {
const fallbackParams = workUnitStore.fallbackRouteParams
if (fallbackParams && fallbackParams.size > 0) {
// We are in a prerender with cacheComponents semantics. We are going to
// hang here and never resolve. This will cause the currently
// rendering component to effectively be a dynamic hole.
React.use(
makeClientHookHangingPromise(
workUnitStore.renderSignal,
new ClientHookDynamicError(workStore.route, expression)
)
)
}
break
}
case 'prerender':
throw new InvariantError(
`\`${expression}\` was called from a Server Component. Next.js should be preventing ${expression} from being included in server components statically, but did not in this case.`
)
case 'validation-client': {
// Don't check fallbackRouteParams here. We handle params that weren't
// provided in the samples using a proxy that throws when accessed.
break
}
case 'prerender-runtime':
throw new InvariantError(
`\`${expression}\` was called during a runtime prerender. Next.js should be preventing ${expression} from being included in server components statically, but did not in this case.`
)
case 'cache':
case 'private-cache':
throw new InvariantError(
`\`${expression}\` was called inside a cache scope. Next.js should be preventing ${expression} from being included in server components statically, but did not in this case.`
)
case 'generate-static-params':
throw new InvariantError(
`\`${expression}\` was called in \`generateStaticParams\`. Next.js should be preventing ${expression} from being included in server component files statically, but did not in this case.`
)
case 'prerender-legacy':
case 'request':
case 'unstable-cache':
break
default:
workUnitStore satisfies never
}
}
}
export function useDynamicSearchParams(expression: string) {
const workStore = workAsyncStorage.getStore()
const workUnitStore = workUnitAsyncStorage.getStore()
if (!workStore) {
// We assume pages router context and just return
return
}
if (!workUnitStore) {
throwForMissingRequestStore(expression)
}
switch (workUnitStore.type) {
case 'validation-client':
// During instant validation we try to behave as close to client as possible,
// so this shouldn't hang during SSR.
return
case 'prerender-client': {
React.use(
makeClientHookHangingPromise(
workUnitStore.renderSignal,
new ClientHookDynamicError(workStore.route, expression)
)
)
break
}
case 'prerender-legacy': {
if (workStore.forceStatic) {
return
}
throw new BailoutToCSRError(expression)
}
case 'prerender':
case 'prerender-runtime':
throw new InvariantError(
`\`${expression}\` was called from a Server Component. Next.js should be preventing ${expression} from being included in server components statically, but did not in this case.`
)
case 'cache':
case 'unstable-cache':
case 'private-cache':
throw new InvariantError(
`\`${expression}\` was called inside a cache scope. Next.js should be preventing ${expression} from being included in server components statically, but did not in this case.`
)
case 'generate-static-params':
throw new InvariantError(
`\`${expression}\` was called in \`generateStaticParams\`. Next.js should be preventing ${expression} from being included in server component files statically, but did not in this case.`
)
case 'request':
return
default:
workUnitStore satisfies never
}
}
const hasSuspenseRegex = /\n\s+at Suspense \(<anonymous>\)/ const hasSuspenseRegex = /\n\s+at Suspense \(<anonymous>\)/
// Common implicit body tags that React will treat as body when placed directly in html // Common implicit body tags that React will treat as body when placed directly in html
......
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