Unverified Commit 72858d71 authored by Josh Story's avatar Josh Story Committed by GitHub

Split revalidation errors by execution context (#98993)

## Summary

- Narrow build-time generator names to a literal type that can be
extended as new generators are added.
- Split the combined revalidation diagnostic into render,
cached-function, and build-time-generator errors so each failure reports
only its actual context and never needs a fallback generator name.
- Cover all three contexts in the existing development and production
test suites.

Follow-up to the review feedback on #98891.

## Verification

- `HEADLESS=true pnpm test-dev-webpack
test/e2e/app-dir/revalidatetag-rsc/revalidatetag-rsc.test.ts` (3 tests)
- `HEADLESS=true pnpm test-start-webpack
test/e2e/app-dir/revalidatetag-rsc/revalidatetag-rsc.test.ts` (3 tests)
- Production Webpack `generate-static-params-errors` suite (6 tests)
- `pnpm --filter=next types`

<!-- NEXT_JS_LLM -->
parent 26bf404d
......@@ -436,10 +436,12 @@ export interface UnstableCacheStore extends CommonCacheStore {
*/
export type CacheStore = UseCacheStore | UnstableCacheStore
export type BuildTimeGeneratorName = 'generateStaticParams'
export interface BuildTimeGeneratorStore extends CommonWorkUnitStore {
readonly type: 'build-time-generator'
/** The public export name to use in diagnostics, regardless of bundling. */
readonly functionName: string
readonly functionName: BuildTimeGeneratorName
readonly rootParams: Params
}
......
......@@ -4,6 +4,8 @@
* immediate fix, then link to the relevant docs.
*/
import type { BuildTimeGeneratorName } from '../app-render/work-unit-async-storage.external'
const NEXT_REQUEST_IN_USE_CACHE =
'https://nextjs.org/docs/messages/next-request-in-use-cache'
......@@ -122,12 +124,30 @@ export function createDraftModeMutationInUnstableCacheError(
}
export function createRevalidateDuringRenderError(
route: string,
expression: string
): Error {
return new Error(
`Route "${route}": \`${expression}\` can't be called during render. Call it from a Server Action or Route Handler instead.\nLearn more: ${REVALIDATE_IN_USE_CACHE}`
)
}
export function createRevalidateInCachedFunctionError(
route: string,
expression: string
): Error {
return new Error(
`Route "${route}": \`${expression}\` can't be called inside a cached function. Call it from a Server Action or Route Handler instead.\nLearn more: ${REVALIDATE_IN_USE_CACHE}`
)
}
export function createRevalidateInBuildTimeGeneratorError(
route: string,
expression: string,
generatorName?: string
generatorName: BuildTimeGeneratorName
): Error {
return new Error(
`Route "${route}": \`${expression}\` can't be called during render, inside a cached function, or inside \`${generatorName ?? 'generateStaticParams'}\`. Call it from a Server Action or Route Handler instead.\nLearn more: ${REVALIDATE_IN_USE_CACHE}`
`Route "${route}": \`${expression}\` can't be called inside \`${generatorName}\`. Call it from a Server Action or Route Handler instead.\nLearn more: ${REVALIDATE_IN_USE_CACHE}`
)
}
......
......@@ -14,7 +14,11 @@ import {
} from '../../../shared/lib/action-revalidation-kind'
import { removeTrailingSlash } from '../../../shared/lib/router/utils/remove-trailing-slash'
import { encodeHeaderSafe } from '../../lib/encode-header-safe'
import { createRevalidateDuringRenderError } from '../../use-cache/use-cache-messages'
import {
createRevalidateDuringRenderError,
createRevalidateInBuildTimeGeneratorError,
createRevalidateInCachedFunctionError,
} from '../../use-cache/use-cache-messages'
import { validateAndNormalizeCacheLifeProfile } from '../../use-cache/cache-life-profile'
type CacheLifeConfig = {
......@@ -145,10 +149,10 @@ function revalidate(
const workUnitStore = workUnitAsyncStorage.getStore()
if (workUnitStore) {
// Keep the generator's name before applying the general render-phase check.
// Prefer the more specific contexts over the general render-phase error.
switch (workUnitStore.type) {
case 'build-time-generator':
throw createRevalidateDuringRenderError(
throw createRevalidateInBuildTimeGeneratorError(
store.route,
expression,
workUnitStore.functionName
......@@ -156,6 +160,7 @@ function revalidate(
case 'cache':
case 'private-cache':
case 'unstable-cache':
throw createRevalidateInCachedFunctionError(store.route, expression)
case 'prerender':
case 'prerender-runtime':
case 'prerender-client':
......@@ -172,10 +177,6 @@ function revalidate(
}
switch (workUnitStore.type) {
case 'cache':
case 'private-cache':
case 'unstable-cache':
throw createRevalidateDuringRenderError(store.route, expression)
case 'prerender':
case 'prerender-runtime':
// cacheComponents Prerender
......
......@@ -19,6 +19,9 @@ export default async function Page() {
<Link href="/revalidate_via_page?tag=data" id="revalidate-via-page">
Revalidate via page
</Link>
<Link href="/revalidate_via_cache" id="revalidate-via-cache">
Revalidate via cache
</Link>
</div>
)
}
import { revalidateTag, unstable_cache } from 'next/cache'
export const dynamic = 'force-dynamic'
const revalidateInCache = unstable_cache(async () => {
revalidateTag('data', 'max')
})
export default async function Page() {
await revalidateInCache()
return <p>revalidated</p>
}
......@@ -30,7 +30,7 @@ describe('revalidateTag-rsc', () => {
if (isNextDev) {
await waitForRedbox(browser)
await expect(getRedboxHeader(browser)).resolves.toContain(
'Route "/revalidate_via_page": `revalidateTag("data")` can\'t be called during render, inside a cached function, or inside `generateStaticParams`.'
'Route "/revalidate_via_page": `revalidateTag("data")` can\'t be called during render.'
)
} else {
await retry(async () => {
......@@ -41,7 +41,29 @@ describe('revalidateTag-rsc', () => {
}
expect(next.cliOutput).toContain(
'Route "/revalidate_via_page": `revalidateTag("data")` can\'t be called during render, inside a cached function, or inside `generateStaticParams`. Call it from a Server Action or Route Handler instead.\nLearn more: https://nextjs.org/docs/messages/revalidate-in-use-cache'
'Route "/revalidate_via_page": `revalidateTag("data")` can\'t be called during render. Call it from a Server Action or Route Handler instead.\nLearn more: https://nextjs.org/docs/messages/revalidate-in-use-cache'
)
})
it('should error if revalidateTag is called inside a cached function', async () => {
const browser = await next.browser('/')
await browser.elementByCss('#revalidate-via-cache').click()
if (isNextDev) {
await waitForRedbox(browser)
await expect(getRedboxHeader(browser)).resolves.toContain(
'Route "/revalidate_via_cache": `revalidateTag("data")` can\'t be called inside a cached function.'
)
} else {
await retry(async () => {
expect(
await browser.eval('document.documentElement.innerHTML')
).toContain('This page couldn\u2019t load')
})
}
expect(next.cliOutput).toContain(
'Route "/revalidate_via_cache": `revalidateTag("data")` can\'t be called inside a cached function. Call it from a Server Action or Route Handler instead.\nLearn more: https://nextjs.org/docs/messages/revalidate-in-use-cache'
)
})
}
......
import { revalidateTag } from 'next/cache'
export default function Page() {
return <p>revalidate-tag</p>
}
export async function generateStaticParams() {
revalidateTag('data', 'max')
return [{ slug: 'test' }]
}
......@@ -47,6 +47,13 @@ describe('generate-static-params-errors', () => {
)
})
it('should error when revalidateTag() is called inside generateStaticParams', async () => {
await buildRoute('app/[lang]/revalidate-tag/[slug]/page.tsx')
expect(getCliOutput()).toContain(
'Error: Route "/[lang]/revalidate-tag/[slug]": `revalidateTag("data")` can\'t be called inside `generateStaticParams`. Call it from a Server Action or Route Handler instead.\nLearn more: https://nextjs.org/docs/messages/revalidate-in-use-cache'
)
})
it('should allow root params access inside generateStaticParams', async () => {
await buildRoute('app/[lang]/root-params/[slug]/page.tsx')
expect(getCliOutput()).not.toContain('Error')
......
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