| import { |
| abortAndThrowOnSynchronousRequestDataAccess, |
| postponeWithTracking, |
| } from '../../app-render/dynamic-rendering' |
| import { isDynamicRoute } from '../../../shared/lib/router/utils' |
| import { |
| NEXT_CACHE_IMPLICIT_TAG_ID, |
| NEXT_CACHE_SOFT_TAG_MAX_LENGTH, |
| } from '../../../lib/constants' |
| import { workAsyncStorage } from '../../app-render/work-async-storage.external' |
| import { workUnitAsyncStorage } from '../../app-render/work-unit-async-storage.external' |
| import { DynamicServerError } from '../../../client/components/hooks-server-context' |
| import { InvariantError } from '../../../shared/lib/invariant-error' |
|
|
| |
| |
| |
| |
| |
| export function revalidateTag(tag: string) { |
| return revalidate([tag], `revalidateTag ${tag}`) |
| } |
|
|
| |
| |
| |
| |
| |
| export function unstable_expirePath( |
| originalPath: string, |
| type?: 'layout' | 'page' |
| ) { |
| if (originalPath.length > NEXT_CACHE_SOFT_TAG_MAX_LENGTH) { |
| console.warn( |
| `Warning: expirePath received "${originalPath}" which exceeded max length of ${NEXT_CACHE_SOFT_TAG_MAX_LENGTH}. See more info here https://nextjs.org/docs/app/api-reference/functions/unstable_expirePath` |
| ) |
| return |
| } |
|
|
| let normalizedPath = `${NEXT_CACHE_IMPLICIT_TAG_ID}${originalPath}` |
|
|
| if (type) { |
| normalizedPath += `${normalizedPath.endsWith('/') ? '' : '/'}${type}` |
| } else if (isDynamicRoute(originalPath)) { |
| console.warn( |
| `Warning: a dynamic page path "${originalPath}" was passed to "expirePath", but the "type" parameter is missing. This has no effect by default, see more info here https://nextjs.org/docs/app/api-reference/functions/unstable_expirePath` |
| ) |
| } |
| return revalidate([normalizedPath], `unstable_expirePath ${originalPath}`) |
| } |
|
|
| |
| |
| |
| |
| |
| export function unstable_expireTag(...tags: string[]) { |
| return revalidate(tags, `unstable_expireTag ${tags.join(', ')}`) |
| } |
|
|
| |
| |
| |
| |
| |
| export function revalidatePath(originalPath: string, type?: 'layout' | 'page') { |
| if (originalPath.length > NEXT_CACHE_SOFT_TAG_MAX_LENGTH) { |
| console.warn( |
| `Warning: revalidatePath received "${originalPath}" which exceeded max length of ${NEXT_CACHE_SOFT_TAG_MAX_LENGTH}. See more info here https://nextjs.org/docs/app/api-reference/functions/revalidatePath` |
| ) |
| return |
| } |
|
|
| let normalizedPath = `${NEXT_CACHE_IMPLICIT_TAG_ID}${originalPath}` |
|
|
| if (type) { |
| normalizedPath += `${normalizedPath.endsWith('/') ? '' : '/'}${type}` |
| } else if (isDynamicRoute(originalPath)) { |
| console.warn( |
| `Warning: a dynamic page path "${originalPath}" was passed to "revalidatePath", but the "type" parameter is missing. This has no effect by default, see more info here https://nextjs.org/docs/app/api-reference/functions/revalidatePath` |
| ) |
| } |
| return revalidate([normalizedPath], `revalidatePath ${originalPath}`) |
| } |
|
|
| function revalidate(tags: string[], expression: string) { |
| const store = workAsyncStorage.getStore() |
| if (!store || !store.incrementalCache) { |
| throw new Error( |
| `Invariant: static generation store missing in ${expression}` |
| ) |
| } |
|
|
| const workUnitStore = workUnitAsyncStorage.getStore() |
| if (workUnitStore) { |
| if (workUnitStore.phase === 'render') { |
| throw new Error( |
| `Route ${store.route} used "${expression}" during render which is unsupported. To ensure revalidation is performed consistently it must always happen outside of renders and cached functions. See more info here: https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering` |
| ) |
| } |
|
|
| switch (workUnitStore.type) { |
| case 'cache': |
| case 'private-cache': |
| throw new Error( |
| `Route ${store.route} used "${expression}" inside a "use cache" which is unsupported. To ensure revalidation is performed consistently it must always happen outside of renders and cached functions. See more info here: https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering` |
| ) |
| case 'unstable-cache': |
| throw new Error( |
| `Route ${store.route} used "${expression}" inside a function cached with "unstable_cache(...)" which is unsupported. To ensure revalidation is performed consistently it must always happen outside of renders and cached functions. See more info here: https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering` |
| ) |
| case 'prerender': |
| |
| const error = new Error( |
| `Route ${store.route} used ${expression} without first calling \`await connection()\`.` |
| ) |
| return abortAndThrowOnSynchronousRequestDataAccess( |
| store.route, |
| expression, |
| error, |
| workUnitStore |
| ) |
| case 'prerender-client': |
| throw new InvariantError( |
| `${expression} must not be used within a client component. Next.js should be preventing ${expression} from being included in client components statically, but did not in this case.` |
| ) |
| case 'prerender-ppr': |
| return postponeWithTracking( |
| store.route, |
| expression, |
| workUnitStore.dynamicTracking |
| ) |
| case 'prerender-legacy': |
| workUnitStore.revalidate = 0 |
|
|
| const err = new DynamicServerError( |
| `Route ${store.route} couldn't be rendered statically because it used \`${expression}\`. See more info here: https://nextjs.org/docs/messages/dynamic-server-error` |
| ) |
| store.dynamicUsageDescription = expression |
| store.dynamicUsageStack = err.stack |
|
|
| throw err |
| case 'request': |
| if (process.env.NODE_ENV !== 'production') { |
| |
| |
| |
| workUnitStore.usedDynamic = true |
| } |
| break |
| default: |
| workUnitStore satisfies never |
| } |
| } |
|
|
| if (!store.pendingRevalidatedTags) { |
| store.pendingRevalidatedTags = [] |
| } |
|
|
| for (const tag of tags) { |
| if (!store.pendingRevalidatedTags.includes(tag)) { |
| store.pendingRevalidatedTags.push(tag) |
| } |
| } |
|
|
| |
| store.pathWasRevalidated = true |
| } |
|
|