Commit 01c6e0b3 authored by Will Binns-Smith's avatar Will Binns-Smith

Merge remote-tracking branch 'origin/canary' into wbinnssmith/unify-node-runtime

parents 0fcc1d27 9e71bba0
......@@ -42,6 +42,7 @@ jobs:
if: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.fork }}
outputs:
value: ${{ steps.deploy-target.outputs.value }}
release_environment: ${{ steps.deploy-target.outputs.release_environment }}
steps:
- uses: actions/checkout@v6
with:
......@@ -62,9 +63,31 @@ jobs:
# 'staging' for canary branch since that will eventually be published i.e. become the production build.
id: deploy-target
run: |
if [[ $(node ./scripts/check-is-release.js 2> /dev/null || :) == v* ]];
RELEASE_VERSION="$(node ./scripts/check-is-release.js 2> /dev/null || :)"
RELEASE_ENVIRONMENT=""
if [[ "$RELEASE_VERSION" == v* ]];
then
echo "value=production" >> $GITHUB_OUTPUT
if [[ "$RELEASE_VERSION" == *-canary.* ]];
then
RELEASE_ENVIRONMENT="release-canary"
elif [[ "$RELEASE_VERSION" == *-beta.* ]];
then
RELEASE_ENVIRONMENT="release-beta"
elif [[ "$RELEASE_VERSION" == *-rc.* ]];
then
RELEASE_ENVIRONMENT="release-release-candidate"
elif [[ "$RELEASE_VERSION" != *-* ]];
then
RELEASE_ENVIRONMENT="release-stable"
fi
if [[ -z "$RELEASE_ENVIRONMENT" ]];
then
echo "::error::Missing release environment for $RELEASE_VERSION"
exit 1
fi
echo "release_environment=$RELEASE_ENVIRONMENT" >> $GITHUB_OUTPUT
elif [ ''"${GIT_REF}"'' == 'refs/heads/canary' ]
then
echo "value=staging" >> $GITHUB_OUTPUT
......@@ -472,6 +495,7 @@ jobs:
if: ${{ needs.deploy-target.outputs.value == 'production' }}
name: Potentially publish release
runs-on: ubuntu-latest
environment: ${{ needs.deploy-target.outputs.release_environment }}
needs:
- deploy-target
- build
......
......@@ -45,7 +45,6 @@ jobs:
# canary next-swc binaries in the monorepo
NEXT_SKIP_NATIVE_POSTINSTALL: 1
environment: release-${{ github.event.inputs.releaseType || 'canary' }}
steps:
- name: Setup node
uses: actions/setup-node@v4
......
......@@ -9900,9 +9900,7 @@ dependencies = [
"futures",
"indexmap 2.13.0",
"inventory",
"once_cell",
"parking_lot",
"phf",
"pin-project-lite",
"rayon",
"regex",
......@@ -9942,7 +9940,6 @@ dependencies = [
"indexmap 2.13.0",
"indoc",
"lzzzz",
"once_cell",
"parking_lot",
"rand 0.10.0",
"regex",
......@@ -9975,7 +9972,6 @@ dependencies = [
"arbitrary",
"bincode 2.0.1",
"libfuzzer-sys",
"once_cell",
"serde",
"tokio",
"turbo-tasks",
......
......@@ -35,10 +35,11 @@
"@types/react": "^18",
"@types/react-dom": "^18",
"cross-env": "10.1.0",
"eslint": "catalog:",
"inliner": "1.13.1",
"postcss": "^8.5",
"tailwindcss": "^4.1.9",
"tw-animate-css": "1.3.3",
"typescript": "6.0.2"
"typescript": "catalog:"
}
}
......@@ -198,7 +198,7 @@
"dd-trace": "4.12.0",
"es5-ext": "0.10.53",
"escape-string-regexp": "2.0.0",
"eslint": "9.37.0",
"eslint": "catalog:",
"eslint-config-next": "workspace:*",
"eslint-formatter-codeframe": "7.32.1",
"eslint-plugin-eslint-plugin": "5.2.1",
......@@ -230,7 +230,7 @@
"image-size": "1.2.1",
"is-animated": "2.0.2",
"isomorphic-unfetch": "3.0.0",
"jest": "29.7.0",
"jest": "catalog:",
"jest-diff": "29.7.0",
"jest-environment-jsdom": "29.7.0",
"jest-extended": "4.0.2",
......@@ -262,7 +262,7 @@
"postcss-pseudoelements": "5.0.0",
"postcss-short-size": "4.0.0",
"postcss-trolling": "0.1.7",
"prettier": "3.6.2",
"prettier": "catalog:",
"pretty-bytes": "5.3.0",
"pretty-ms": "7.0.0",
"random-seed": "0.3.0",
......@@ -302,7 +302,7 @@
"tsec": "0.2.1",
"tsx": "4.19.2",
"turbo": "2.9.4",
"typescript": "6.0.2",
"typescript": "catalog:",
"typescript-eslint": "8.46.0",
"unfetch": "4.2.0",
"wait-port": "0.2.2",
......
......@@ -41,6 +41,7 @@
"cross-spawn": "7.0.3",
"fast-glob": "3.3.1",
"picocolors": "1.1.1",
"prettier": "catalog:",
"prettier-plugin-tailwindcss": "0.6.2",
"prompts": "2.4.2",
"tar": "7.5.7",
......
......@@ -17,7 +17,8 @@
},
"devDependencies": {
"@types/eslint": "9.6.1",
"eslint": "9.37.0"
"eslint": "9.37.0",
"typescript": "catalog:"
},
"scripts": {
"dev": "pnpm build",
......
......@@ -40,6 +40,7 @@
"@types/jscodeshift": "0.11.0",
"@types/prompts": "2.4.2",
"@types/semver": "7.3.1",
"typescript": "6.0.2"
"jest": "catalog:",
"typescript": "catalog:"
}
}
......@@ -32,6 +32,7 @@
"devDependencies": {
"@vercel/ncc": "0.34.0",
"dotenv": "16.3.1",
"dotenv-expand": "10.0.0"
"dotenv-expand": "10.0.0",
"typescript": "catalog:"
}
}
......@@ -34,6 +34,7 @@
"@types/jest": "^29.5.0",
"@vercel/ncc": "0.34.0",
"jest": "^29.5.0",
"ts-jest": "^29.1.0"
"ts-jest": "^29.1.0",
"typescript": "catalog:"
}
}
......@@ -7,7 +7,7 @@
"resolveJsonModule": true,
"esModuleInterop": true,
"skipLibCheck": false,
"lib": ["es2022", "dom"],
"lib": ["es2022", "dom", "dom.iterable"],
"rootDir": "src",
"types": ["jest"]
},
......
......@@ -2098,6 +2098,8 @@ export default async function build(
configFileName,
cacheComponents: isAppCacheComponentsEnabled,
authInterrupts: isAuthInterruptsEnabled,
useCacheTimeout: config.experimental.useCacheTimeout,
staticPageGenerationTimeout: config.staticPageGenerationTimeout,
httpAgentOptions: config.httpAgentOptions,
locales: config.i18n?.locales,
defaultLocale: config.i18n?.defaultLocale,
......@@ -2325,6 +2327,10 @@ export default async function build(
pageType,
cacheComponents: isAppCacheComponentsEnabled,
authInterrupts: isAuthInterruptsEnabled,
useCacheTimeout:
config.experimental.useCacheTimeout,
staticPageGenerationTimeout:
config.staticPageGenerationTimeout,
cacheHandler: config.cacheHandler,
cacheHandlers: config.cacheHandlers,
isrFlushToDisk: ciEnvironment.hasNextSupport
......
......@@ -782,6 +782,8 @@ export async function buildAppStaticPaths({
distDir,
cacheComponents,
authInterrupts,
useCacheTimeout,
staticPageGenerationTimeout,
segments,
isrFlushToDisk,
cacheHandler,
......@@ -802,6 +804,8 @@ export async function buildAppStaticPaths({
route: NormalizedAppRoute
cacheComponents: boolean
authInterrupts: boolean
useCacheTimeout: number
staticPageGenerationTimeout: number
segments: readonly Readonly<AppSegment>[]
distDir: string
isrFlushToDisk?: boolean
......@@ -859,10 +863,12 @@ export async function buildAppStaticPaths({
renderOpts: {
incrementalCache,
cacheLifeProfiles,
staticPageGenerationTimeout,
supportsDynamicResponse: true,
cacheComponents,
experimental: {
authInterrupts,
useCacheTimeout,
},
waitUntil: afterRunner.context.waitUntil,
onClose: afterRunner.context.onClose,
......
......@@ -860,6 +860,7 @@ export async function handler(
prefetchHints: prefetchHintsManifest,
incrementalCache,
cacheLifeProfiles: nextConfig.cacheLife,
staticPageGenerationTimeout: nextConfig.staticPageGenerationTimeout,
basePath: nextConfig.basePath,
serverActions: nextConfig.experimental.serverActions,
logServerFunctions:
......@@ -888,6 +889,7 @@ export async function handler(
inlineCss: Boolean(nextConfig.experimental.inlineCss),
prefetchInlining: nextConfig.experimental.prefetchInlining ?? false,
authInterrupts: Boolean(nextConfig.experimental.authInterrupts),
useCacheTimeout: nextConfig.experimental.useCacheTimeout,
cachedNavigations: Boolean(
nextConfig.experimental.cachedNavigations
),
......@@ -1791,7 +1793,9 @@ export async function handler(
const instantTestRequestId =
routeModule.isDev === true ? crypto.randomUUID() : null
body.pipeThrough(
createInstantTestScriptInsertionTransformStream(instantTestRequestId)
await createInstantTestScriptInsertionTransformStream(
instantTestRequestId
)
)
return sendRenderResult({
req,
......
......@@ -243,11 +243,13 @@ export async function handler(
renderOpts: {
experimental: {
authInterrupts: Boolean(nextConfig.experimental.authInterrupts),
useCacheTimeout: nextConfig.experimental.useCacheTimeout,
},
cacheComponents: Boolean(nextConfig.cacheComponents),
supportsDynamicResponse,
incrementalCache,
cacheLifeProfiles: nextConfig.cacheLife,
staticPageGenerationTimeout: nextConfig.staticPageGenerationTimeout,
waitUntil: ctx.waitUntil,
onClose: (cb) => {
res.on('close', cb)
......
......@@ -152,6 +152,7 @@ async function requestHandler(
multiZoneDraftMode: false,
cacheLifeProfiles: nextConfig.cacheLife,
staticPageGenerationTimeout: nextConfig.staticPageGenerationTimeout,
basePath: nextConfig.basePath,
serverActions: nextConfig.experimental.serverActions,
logServerFunctions:
......@@ -167,6 +168,7 @@ async function requestHandler(
inlineCss: Boolean(nextConfig.experimental.inlineCss),
prefetchInlining: nextConfig.experimental.prefetchInlining ?? false,
authInterrupts: Boolean(nextConfig.experimental.authInterrupts),
useCacheTimeout: nextConfig.experimental.useCacheTimeout,
cachedNavigations: Boolean(nextConfig.experimental.cachedNavigations),
clientTraceMetadata:
nextConfig.experimental.clientTraceMetadata || ([] as any),
......
......@@ -684,6 +684,8 @@ export async function isPageStatic({
pageType,
cacheComponents,
authInterrupts,
useCacheTimeout,
staticPageGenerationTimeout,
originalAppPath,
isrFlushToDisk,
cacheMaxMemorySize,
......@@ -702,6 +704,8 @@ export async function isPageStatic({
distDir: string
cacheComponents: boolean
authInterrupts: boolean
useCacheTimeout: number
staticPageGenerationTimeout: number
configFileName: string
httpAgentOptions: NextConfigComplete['httpAgentOptions']
locales?: readonly string[]
......@@ -874,6 +878,8 @@ export async function isPageStatic({
route,
cacheComponents,
authInterrupts,
useCacheTimeout,
staticPageGenerationTimeout,
segments,
distDir,
requestHeaders: {},
......
......@@ -532,7 +532,7 @@ export async function createFetch<T>(
// search param to it. This should not leak outside of this function, so we
// track them separately.
let fetchUrl = new URL(url)
setCacheBustingSearchParam(fetchUrl, headers)
await setCacheBustingSearchParam(fetchUrl, headers)
let processed = fetch(fetchUrl, fetchOptions).then(processFetch)
let fetchPromise = processed.then(({ response }) => response)
......@@ -602,7 +602,7 @@ export async function createFetch<T>(
// fetch again.
// TODO: We should abort the previous request.
fetchUrl = new URL(responseUrl)
setCacheBustingSearchParam(fetchUrl, headers)
await setCacheBustingSearchParam(fetchUrl, headers)
processed = fetch(fetchUrl, fetchOptions).then(processFetch)
fetchPromise = processed.then(({ response }) => response)
flightResponsePromise = shouldImmediatelyDecode
......
import {
NEXT_ROUTER_PREFETCH_HEADER,
NEXT_ROUTER_SEGMENT_PREFETCH_HEADER,
NEXT_ROUTER_STATE_TREE_HEADER,
NEXT_RSC_UNION_QUERY,
NEXT_URL,
} from '../app-router-headers'
import { setCacheBustingSearchParam } from './set-cache-busting-search-param'
import { computeLegacyCacheBustingSearchParam } from '../../../shared/lib/router/utils/cache-busting-search-param'
describe('setCacheBustingSearchParam', () => {
const originalCryptoDescriptor = Object.getOwnPropertyDescriptor(
globalThis,
'crypto'
)
afterEach(() => {
if (originalCryptoDescriptor) {
Object.defineProperty(globalThis, 'crypto', originalCryptoDescriptor)
}
})
it('falls back to the legacy hash when Web Crypto is unavailable', async () => {
Object.defineProperty(globalThis, 'crypto', {
configurable: true,
value: {},
})
const headers = {
[NEXT_ROUTER_PREFETCH_HEADER]: '1',
[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER]: '/_tree',
[NEXT_ROUTER_STATE_TREE_HEADER]: '%5B%22%22%2C%7B%7D%5D',
[NEXT_URL]: '/pcsta0',
} as const
const url = new URL('https://example.com/')
await setCacheBustingSearchParam(url, headers)
expect(url.searchParams.get(NEXT_RSC_UNION_QUERY)).toBe(
computeLegacyCacheBustingSearchParam(
headers[NEXT_ROUTER_PREFETCH_HEADER],
headers[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER],
headers[NEXT_ROUTER_STATE_TREE_HEADER],
headers[NEXT_URL]
)
)
})
})
'use client'
import { computeCacheBustingSearchParam } from '../../../shared/lib/router/utils/cache-busting-search-param'
import {
computeCacheBustingSearchParam,
computeLegacyCacheBustingSearchParam,
} from '../../../shared/lib/router/utils/cache-busting-search-param'
import {
NEXT_ROUTER_PREFETCH_HEADER,
NEXT_ROUTER_SEGMENT_PREFETCH_HEADER,
......@@ -10,6 +13,26 @@ import {
} from '../app-router-headers'
import type { RequestHeaders } from './fetch-server-response'
async function computeClientCacheBustingSearchParam(
headers: RequestHeaders
): Promise<string> {
if (typeof globalThis.crypto?.subtle?.digest === 'function') {
return computeCacheBustingSearchParam(
headers[NEXT_ROUTER_PREFETCH_HEADER],
headers[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER],
headers[NEXT_ROUTER_STATE_TREE_HEADER],
headers[NEXT_URL]
)
}
return computeLegacyCacheBustingSearchParam(
headers[NEXT_ROUTER_PREFETCH_HEADER],
headers[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER],
headers[NEXT_ROUTER_STATE_TREE_HEADER],
headers[NEXT_URL]
)
}
/**
* Mutates the provided URL by adding a cache-busting search parameter for CDNs that don't
* support custom headers. This helps avoid caching conflicts by making each request unique.
......@@ -21,21 +44,17 @@ import type { RequestHeaders } from './fetch-server-response'
* URL before: https://example.com/path?query=1
* URL after: https://example.com/path?query=1&_rsc=abc123
*
* Note: This function mutates the input URL directly and does not return anything.
* Note: This function mutates the input URL directly and resolves once the
* cache-busting value has been computed and applied.
*
* TODO: Since we need to use a search param anyway, we could simplify by removing the custom
* headers approach entirely and just use search params.
*/
export const setCacheBustingSearchParam = (
export const setCacheBustingSearchParam = async (
url: URL,
headers: RequestHeaders
): void => {
const uniqueCacheKey = computeCacheBustingSearchParam(
headers[NEXT_ROUTER_PREFETCH_HEADER],
headers[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER],
headers[NEXT_ROUTER_STATE_TREE_HEADER],
headers[NEXT_URL]
)
): Promise<void> => {
const uniqueCacheKey = await computeClientCacheBustingSearchParam(headers)
setCacheBustingSearchParamWithHash(url, uniqueCacheKey)
}
......@@ -50,7 +69,7 @@ export const setCacheBustingSearchParam = (
* hash: "abc123"
* URL after: https://example.com/path?query=1&_rsc=abc123
*
* If the hash is null, we will set `_rsc` search param without a value.
* If the hash is empty, we will set `_rsc` search param without a value.
* Like this: https://example.com/path?query=1&_rsc
*
* Note: This function mutates the input URL directly and does not return anything.
......
......@@ -496,6 +496,7 @@ async function exportAppImpl(
serverActions: nextConfig.experimental.serverActions,
serverComponents: enabledDirectories.app,
cacheLifeProfiles: nextConfig.cacheLife,
staticPageGenerationTimeout: nextConfig.staticPageGenerationTimeout,
nextFontManifest: require(
join(distDir, 'server', `${NEXT_FONT_MANIFEST}.json`)
),
......@@ -512,6 +513,7 @@ async function exportAppImpl(
inlineCss: nextConfig.experimental.inlineCss ?? false,
prefetchInlining: nextConfig.experimental.prefetchInlining ?? false,
authInterrupts: !!nextConfig.experimental.authInterrupts,
useCacheTimeout: nextConfig.experimental.useCacheTimeout,
cachedNavigations: nextConfig.experimental.cachedNavigations ?? false,
maxPostponedStateSizeBytes: parseMaxPostponedStateSize(
nextConfig.experimental.maxPostponedStateSize
......
......@@ -48,7 +48,10 @@ export async function exportAppRoute(
htmlFilepath: string,
fileWriter: MultiFileWriter,
cacheComponents: boolean,
experimental: Required<Pick<ExperimentalConfig, 'authInterrupts'>>,
staticPageGenerationTimeout: number,
experimental: Required<
Pick<ExperimentalConfig, 'authInterrupts' | 'useCacheTimeout'>
>,
buildId: string
): Promise<ExportRouteResult> {
// Ensure that the URL is absolute.
......@@ -81,6 +84,7 @@ export async function exportAppRoute(
onClose: afterRunner.context.onClose,
onAfterTaskError: afterRunner.context.onTaskError,
cacheLifeProfiles,
staticPageGenerationTimeout,
},
sharedContext: {
buildId,
......
......@@ -257,6 +257,7 @@ async function exportPageImpl(
htmlFilepath,
fileWriter,
commonRenderOpts.cacheComponents,
commonRenderOpts.staticPageGenerationTimeout,
commonRenderOpts.experimental,
buildId
)
......
......@@ -68,11 +68,12 @@ import { workUnitAsyncStorage } from '../app-render/work-unit-async-storage.exte
import { InvariantError } from '../../shared/lib/invariant-error'
import { executeRevalidates } from '../revalidation-utils'
import { addRequestMeta, getRequestMeta } from '../request-meta'
import { setCacheBustingSearchParam } from '../../client/components/router-reducer/set-cache-busting-search-param'
import { setCacheBustingSearchParamWithHash } from '../../client/components/router-reducer/set-cache-busting-search-param'
import {
ActionDidNotRevalidate,
ActionDidRevalidateStaticAndDynamic,
} from '../../shared/lib/action-revalidation-kind'
import { computeCacheBustingSearchParam } from '../../shared/lib/router/utils/cache-busting-search-param'
const INLINE_ACTION_PREFIX = '$$RSC_SERVER_ACTION_'
......@@ -401,19 +402,15 @@ async function createRedirectRenderResult(
forwardedHeaders.delete(ACTION_HEADER)
try {
setCacheBustingSearchParam(fetchUrl, {
[NEXT_ROUTER_PREFETCH_HEADER]: forwardedHeaders.get(
NEXT_ROUTER_PREFETCH_HEADER
)
const cacheBustingSearchParam = await computeCacheBustingSearchParam(
forwardedHeaders.get(NEXT_ROUTER_PREFETCH_HEADER)
? ('1' as const)
: undefined,
[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER]:
forwardedHeaders.get(NEXT_ROUTER_SEGMENT_PREFETCH_HEADER) ??
undefined,
[NEXT_ROUTER_STATE_TREE_HEADER]:
forwardedHeaders.get(NEXT_ROUTER_STATE_TREE_HEADER) ?? undefined,
[NEXT_URL]: forwardedHeaders.get(NEXT_URL) ?? undefined,
})
forwardedHeaders.get(NEXT_ROUTER_SEGMENT_PREFETCH_HEADER) ?? undefined,
forwardedHeaders.get(NEXT_ROUTER_STATE_TREE_HEADER) ?? undefined,
forwardedHeaders.get(NEXT_URL) ?? undefined
)
setCacheBustingSearchParamWithHash(fetchUrl, cacheBustingSearchParam)
const response = await fetch(fetchUrl, {
method: 'GET',
......
......@@ -6814,6 +6814,8 @@ async function validateInstantConfigInBuildWithSample(
route: outerWorkStore.route,
incrementalCache: outerWorkStore.incrementalCache,
cacheLifeProfiles: outerWorkStore.cacheLifeProfiles,
useCacheTimeout: outerWorkStore.useCacheTimeout,
staticPageGenerationTimeout: outerWorkStore.staticPageGenerationTimeout,
isBuildTimePrerendering: false,
fetchCache: outerWorkStore.fetchCache,
isOnDemandRevalidate: false,
......
......@@ -111,6 +111,7 @@ export interface RenderOptsPartial {
cacheLifeProfiles?: {
[profile: string]: import('../use-cache/cache-life').CacheLife
}
staticPageGenerationTimeout: number
isOnDemandRevalidate?: boolean
isPossibleServerAction?: boolean
setCacheStatus?: (status: ServerCacheStatus, htmlRequestId: string) => void
......@@ -165,6 +166,7 @@ export interface RenderOptsPartial {
inlineCss: boolean
prefetchInlining: PrefetchInliningConfig
authInterrupts: boolean
useCacheTimeout: number
cachedNavigations: boolean
/**
......
......@@ -29,6 +29,8 @@ export interface WorkStore {
readonly incrementalCache?: IncrementalCache
readonly cacheLifeProfiles?: { [profile: string]: CacheLife }
readonly useCacheTimeout: number
readonly staticPageGenerationTimeout: number
readonly isOnDemandRevalidate?: boolean
readonly isBuildTimePrerendering?: boolean
......
......@@ -23,6 +23,7 @@ export type WorkStoreContext = {
nonce?: string
renderOpts: {
cacheLifeProfiles?: { [profile: string]: CacheLife }
staticPageGenerationTimeout: number
incrementalCache?: IncrementalCache
isOnDemandRevalidate?: boolean
cacheComponents: boolean
......@@ -31,7 +32,7 @@ export type WorkStoreContext = {
pendingWaitUntil?: Promise<any>
experimental: Pick<
RenderOpts['experimental'],
'isRoutePPREnabled' | 'authInterrupts'
'isRoutePPREnabled' | 'authInterrupts' | 'useCacheTimeout'
>
/**
......@@ -121,6 +122,8 @@ export function createWorkStore({
// so that it can access the fs cache without mocks
renderOpts.incrementalCache || (globalThis as any).__incrementalCache,
cacheLifeProfiles: renderOpts.cacheLifeProfiles,
useCacheTimeout: renderOpts.experimental.useCacheTimeout,
staticPageGenerationTimeout: renderOpts.staticPageGenerationTimeout,
isBuildTimePrerendering: renderOpts.isBuildTimePrerendering,
fetchCache: renderOpts.fetchCache,
isOnDemandRevalidate: renderOpts.isOnDemandRevalidate,
......
......@@ -145,7 +145,6 @@ import { shouldServeStreamingMetadata } from './lib/streaming-metadata'
import { decodeQueryPathParameter } from './lib/decode-query-path-parameter'
import { NoFallbackError } from '../shared/lib/no-fallback-error.external'
import { fixMojibake } from './lib/fix-mojibake'
import { computeCacheBustingSearchParam } from '../shared/lib/router/utils/cache-busting-search-param'
import { setCacheBustingSearchParamWithHash } from '../client/components/router-reducer/set-cache-busting-search-param'
import type { CacheControl } from './lib/cache-control'
import type { PrerenderedRoute } from '../build/static-paths/types'
......@@ -157,6 +156,10 @@ import {
getPostponedStateExceededErrorMessage,
readBodyWithSizeLimit,
} from './lib/postponed-request-body'
import {
computeCacheBustingSearchParam,
computeLegacyCacheBustingSearchParam,
} from '../shared/lib/router/utils/cache-busting-search-param'
export type FindComponentsResult<
NextModule extends GenericComponentMod = GenericComponentMod,
......@@ -553,6 +556,7 @@ export default abstract class Server<
distDir: this.distDir,
serverComponents: this.enabledDirectories.app,
cacheLifeProfiles: this.nextConfig.cacheLife,
staticPageGenerationTimeout: this.nextConfig.staticPageGenerationTimeout,
enableTainting: this.nextConfig.experimental.taint,
crossOrigin: this.nextConfig.crossOrigin
? this.nextConfig.crossOrigin
......@@ -576,6 +580,7 @@ export default abstract class Server<
prefetchInlining:
this.nextConfig.experimental.prefetchInlining ?? false,
authInterrupts: !!this.nextConfig.experimental.authInterrupts,
useCacheTimeout: this.nextConfig.experimental.useCacheTimeout,
cachedNavigations:
this.nextConfig.experimental.cachedNavigations ?? false,
maxPostponedStateSizeBytes: parseMaxPostponedStateSize(
......@@ -2078,7 +2083,7 @@ export default abstract class Server<
headers[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER] ||
getRequestMeta(req, 'segmentPrefetchRSCRequest')
const expectedHash = computeCacheBustingSearchParam(
const expectedHash = await computeCacheBustingSearchParam(
routerPrefetch,
segmentPrefetchRSCRequest,
headers[NEXT_ROUTER_STATE_TREE_HEADER],
......@@ -2090,10 +2095,24 @@ export default abstract class Server<
NEXT_RSC_UNION_QUERY
)
if (expectedHash !== actualHash) {
let matchesHash = expectedHash === actualHash
if (!matchesHash && actualHash !== null) {
// We'll fallback to checking the legacy hash format to support clients that do not have a secure context
matchesHash =
computeLegacyCacheBustingSearchParam(
routerPrefetch,
segmentPrefetchRSCRequest,
headers[NEXT_ROUTER_STATE_TREE_HEADER],
headers[NEXT_URL]
) === actualHash
}
if (!matchesHash) {
// The hash sent by the client does not match the expected value.
// Redirect to the URL with the correct cache-busting search param.
// This prevents cache poisoning attacks on CDNs that don't respect Vary headers.
// We continue to accept the legacy short hash for clients that still
// generate the 5-character `_rsc` form.
// Note: When no headers are present, expectedHash is empty string and client
// must send `_rsc` param, otherwise actualHash is null and hash check fails.
const url = new URL(req.url || '', 'http://localhost')
......
......@@ -398,6 +398,7 @@ export const experimentalSchema = {
serverComponentsHmrCache: z.boolean().optional(),
authInterrupts: z.boolean().optional(),
useCache: z.boolean().optional(),
useCacheTimeout: z.number().positive().optional(),
useNodeStreams: z.boolean().optional(),
slowModuleDetection: z
.object({
......
......@@ -35,6 +35,8 @@ export type NextConfigComplete = Required<Omit<NextConfig, 'configFile'>> & {
experimental: ExperimentalConfig & {
// Normalized by config.ts: true and partial objects become resolved objects
prefetchInlining?: PrefetchInliningConfig
// Normalized by config.ts: defaulted to 90% of staticPageGenerationTimeout
useCacheTimeout: number
}
// The root directory of the distDir. In development mode, this is the parent directory of `distDir`
// since development builds use `{distDir}/dev`. This is used to ensure that the bundler doesn't
......@@ -1014,6 +1016,13 @@ export interface ExperimentalConfig {
*/
authInterrupts?: boolean
/**
* Seconds before a `'use cache'` fill is considered stalled. Defaults to
* 90% of `staticPageGenerationTimeout`. In prerender it's clamped to that
* ceiling so errors surface before the build worker kills the page.
*/
useCacheTimeout?: number
/**
* Enables the use of the `"use cache"` directive.
* @deprecated use top-level `cacheComponents` instead
......@@ -1989,6 +1998,7 @@ export interface NextConfigRuntime {
useFileSystemPublicRoutes: NextConfigComplete['useFileSystemPublicRoutes']
logging?: NextConfigComplete['logging']
adapterPath?: NextConfigComplete['adapterPath']
staticPageGenerationTimeout: NextConfigComplete['staticPageGenerationTimeout']
experimental: Pick<
NextConfigComplete['experimental'],
......@@ -2002,6 +2012,7 @@ export interface NextConfigRuntime {
| 'inlineCss'
| 'prefetchInlining'
| 'authInterrupts'
| 'useCacheTimeout'
| 'clientTraceMetadata'
| 'clientParamParsingOrigins'
| 'allowedRevalidateHeaderKeys'
......@@ -2051,64 +2062,63 @@ export function getNextConfigRuntime(
return config
}
let ex = config.experimental
const ex = config.experimental
type Requiredish<T> = {
[K in keyof Required<T>]: T[K]
}
let experimental = ex
? ({
ppr: ex.ppr,
taint: ex.taint,
serverActions: ex.serverActions,
staleTimes: ex.staleTimes,
dynamicOnHover: ex.dynamicOnHover,
useOffline: ex.useOffline,
optimisticRouting: ex.optimisticRouting,
inlineCss: ex.inlineCss,
prefetchInlining: ex.prefetchInlining,
authInterrupts: ex.authInterrupts,
clientTraceMetadata: ex.clientTraceMetadata,
clientParamParsingOrigins: ex.clientParamParsingOrigins,
allowedRevalidateHeaderKeys: ex.allowedRevalidateHeaderKeys,
fetchCacheKeyPrefix: ex.fetchCacheKeyPrefix,
isrFlushToDisk: ex.isrFlushToDisk,
optimizeCss: ex.optimizeCss,
nextScriptWorkers: ex.nextScriptWorkers,
disableOptimizedLoading: ex.disableOptimizedLoading,
largePageDataBytes: ex.largePageDataBytes,
serverComponentsHmrCache: ex.serverComponentsHmrCache,
caseSensitiveRoutes: ex.caseSensitiveRoutes,
validateRSCRequestHeaders: ex.validateRSCRequestHeaders,
sri: ex.sri,
useSkewCookie: ex.useSkewCookie,
preloadEntriesOnStart: ex.preloadEntriesOnStart,
hideLogsAfterAbort: ex.hideLogsAfterAbort,
removeUncaughtErrorAndRejectionListeners:
ex.removeUncaughtErrorAndRejectionListeners,
imgOptConcurrency: ex.imgOptConcurrency,
imgOptMaxInputPixels: ex.imgOptMaxInputPixels,
imgOptSequentialRead: ex.imgOptSequentialRead,
imgOptSkipMetadata: ex.imgOptSkipMetadata,
imgOptTimeoutInSeconds: ex.imgOptTimeoutInSeconds,
proxyClientMaxBodySize: ex.proxyClientMaxBodySize,
proxyTimeout: ex.proxyTimeout,
testProxy: ex.testProxy,
runtimeServerDeploymentId: ex.runtimeServerDeploymentId,
maxPostponedStateSize: ex.maxPostponedStateSize,
cachedNavigations: ex.cachedNavigations,
partialFallbacks: ex.partialFallbacks,
exposeTestingApiInProductionBuild: ex.exposeTestingApiInProductionBuild,
supportsImmutableAssets: ex.supportsImmutableAssets,
useNodeStreams: ex.useNodeStreams,
trustHostHeader: ex.trustHostHeader,
isExperimentalCompile: ex.isExperimentalCompile,
} satisfies Requiredish<NextConfigRuntime['experimental']>)
: {}
let runtimeConfig: Requiredish<NextConfigRuntime> = {
const experimental = {
ppr: ex.ppr,
taint: ex.taint,
serverActions: ex.serverActions,
staleTimes: ex.staleTimes,
dynamicOnHover: ex.dynamicOnHover,
useOffline: ex.useOffline,
optimisticRouting: ex.optimisticRouting,
inlineCss: ex.inlineCss,
prefetchInlining: ex.prefetchInlining,
authInterrupts: ex.authInterrupts,
useCacheTimeout: ex.useCacheTimeout,
clientTraceMetadata: ex.clientTraceMetadata,
clientParamParsingOrigins: ex.clientParamParsingOrigins,
allowedRevalidateHeaderKeys: ex.allowedRevalidateHeaderKeys,
fetchCacheKeyPrefix: ex.fetchCacheKeyPrefix,
isrFlushToDisk: ex.isrFlushToDisk,
optimizeCss: ex.optimizeCss,
nextScriptWorkers: ex.nextScriptWorkers,
disableOptimizedLoading: ex.disableOptimizedLoading,
largePageDataBytes: ex.largePageDataBytes,
serverComponentsHmrCache: ex.serverComponentsHmrCache,
caseSensitiveRoutes: ex.caseSensitiveRoutes,
validateRSCRequestHeaders: ex.validateRSCRequestHeaders,
sri: ex.sri,
useSkewCookie: ex.useSkewCookie,
preloadEntriesOnStart: ex.preloadEntriesOnStart,
hideLogsAfterAbort: ex.hideLogsAfterAbort,
removeUncaughtErrorAndRejectionListeners:
ex.removeUncaughtErrorAndRejectionListeners,
imgOptConcurrency: ex.imgOptConcurrency,
imgOptMaxInputPixels: ex.imgOptMaxInputPixels,
imgOptSequentialRead: ex.imgOptSequentialRead,
imgOptSkipMetadata: ex.imgOptSkipMetadata,
imgOptTimeoutInSeconds: ex.imgOptTimeoutInSeconds,
proxyClientMaxBodySize: ex.proxyClientMaxBodySize,
proxyTimeout: ex.proxyTimeout,
testProxy: ex.testProxy,
runtimeServerDeploymentId: ex.runtimeServerDeploymentId,
maxPostponedStateSize: ex.maxPostponedStateSize,
cachedNavigations: ex.cachedNavigations,
partialFallbacks: ex.partialFallbacks,
exposeTestingApiInProductionBuild: ex.exposeTestingApiInProductionBuild,
supportsImmutableAssets: ex.supportsImmutableAssets,
useNodeStreams: ex.useNodeStreams,
trustHostHeader: ex.trustHostHeader,
isExperimentalCompile: ex.isExperimentalCompile,
} satisfies Requiredish<NextConfigRuntime['experimental']>
const runtimeConfig: Requiredish<NextConfigRuntime> = {
deploymentId: config.experimental.runtimeServerDeploymentId
? ''
: config.deploymentId,
......@@ -2144,6 +2154,7 @@ export function getNextConfigRuntime(
pageExtensions: config.pageExtensions,
useFileSystemPublicRoutes: config.useFileSystemPublicRoutes,
logging: config.logging,
staticPageGenerationTimeout: config.staticPageGenerationTimeout,
experimental,
}
......
......@@ -1526,6 +1526,14 @@ function assignDefaultsAndValidate(
result.distDir = join(result.distDir, 'dev')
}
// Derive the `'use cache'` fill timeout from `staticPageGenerationTimeout`
// if the user didn't set one explicitly. 90% leaves headroom for the
// cache-fill error to surface before the build worker kills the page.
if (result.experimental.useCacheTimeout === undefined) {
result.experimental.useCacheTimeout =
result.staticPageGenerationTimeout * 0.9
}
return result as NextConfigComplete
}
......
......@@ -819,6 +819,9 @@ export default class DevServer extends Server {
nextConfigOutput: this.nextConfig.output,
buildId: this.buildId,
authInterrupts: Boolean(this.nextConfig.experimental.authInterrupts),
useCacheTimeout: this.nextConfig.experimental.useCacheTimeout,
staticPageGenerationTimeout:
this.nextConfig.staticPageGenerationTimeout,
sriEnabled: Boolean(this.nextConfig.experimental.sri?.algorithm),
})
return pathsResult
......
......@@ -58,6 +58,8 @@ export async function loadStaticPaths({
nextConfigOutput,
buildId,
authInterrupts,
useCacheTimeout,
staticPageGenerationTimeout,
sriEnabled,
}: {
dir: string
......@@ -81,6 +83,8 @@ export async function loadStaticPaths({
nextConfigOutput: 'standalone' | 'export' | undefined
buildId: string
authInterrupts: boolean
useCacheTimeout: number
staticPageGenerationTimeout: number
sriEnabled: boolean
}): Promise<StaticPathsResult> {
// this needs to be initialized before loadComponents otherwise
......@@ -151,6 +155,8 @@ export async function loadStaticPaths({
partialFallbacksEnabled: config.partialFallbacks,
buildId,
authInterrupts,
useCacheTimeout,
staticPageGenerationTimeout,
rootParamKeys,
})
} else if (!components.getStaticPaths) {
......
import { getRequestMeta, type NextIncomingMessage } from '../request-meta'
const PATCHED_SET_HEADER = Symbol('next.patchSetHeaderWithCookieSupport')
type PatchableResponse = {
setHeader(key: string, value: string | string[]): PatchableResponse
headersSent?: boolean
[PATCHED_SET_HEADER]?: true
}
/**
......@@ -15,7 +19,16 @@ export function patchSetHeaderWithCookieSupport(
req: NextIncomingMessage,
res: PatchableResponse
) {
if (res[PATCHED_SET_HEADER]) {
return
}
const setHeader = res.setHeader.bind(res)
Object.defineProperty(res, PATCHED_SET_HEADER, {
value: true,
})
res.setHeader = (
name: string,
value: string | string[]
......
......@@ -49,6 +49,7 @@ import {
getRequestMeta,
type NextIncomingMessage,
} from '../request-meta'
import { patchSetHeaderWithCookieSupport } from '../lib/patch-set-header'
import { normalizePagePath } from '../../shared/lib/page-path/normalize-page-path'
import { isStaticMetadataRoute } from '../../lib/metadata/is-metadata-route'
import { IncrementalCache } from '../lib/incremental-cache'
......@@ -647,6 +648,10 @@ export abstract class RouteModule<
// edge runtime handles loading instrumentation at the edge adapter level
if (process.env.NEXT_RUNTIME !== 'edge') {
if (res) {
patchSetHeaderWithCookieSupport(req, res)
}
const { join, relative } =
require('node:path') as typeof import('node:path')
......
......@@ -521,12 +521,11 @@ export function createHeadInsertionTransformStream(
})
}
function createClientResumeScriptInsertionTransformStream(): TransformStream<
Uint8Array,
Uint8Array
async function createClientResumeScriptInsertionTransformStream(): Promise<
TransformStream<Uint8Array, Uint8Array>
> {
const segmentPath = '/_full'
const cacheBustingHeader = computeCacheBustingSearchParam(
const cacheBustingHeader = await computeCacheBustingSearchParam(
'1', // headers[NEXT_ROUTER_PREFETCH_HEADER]
'/_full', // headers[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER]
undefined, // headers[NEXT_ROUTER_STATE_TREE_HEADER]
......@@ -586,9 +585,9 @@ function createClientResumeScriptInsertionTransformStream(): TransformStream<
* element inside <head>. Used during instant navigation testing to set
* self.__next_instant_test before any async bootstrap scripts execute.
*/
export function createInstantTestScriptInsertionTransformStream(
export async function createInstantTestScriptInsertionTransformStream(
requestId: string | null
): TransformStream<Uint8Array, Uint8Array> {
): Promise<TransformStream<Uint8Array, Uint8Array>> {
// Kick off a fetch for the static RSC payload. This is the hydration
// source for the locked static shell — same as the __NEXT_CLIENT_RESUME
// fetch used for fallback routes, but with NEXT_INSTANT_PREFETCH_HEADER
......@@ -598,7 +597,7 @@ export function createInstantTestScriptInsertionTransformStream(
// as the feature flag (truthy = instant test mode). The client processes
// this as a fallback prerender payload for hydration.
const segmentPath = '/_full'
const cacheBustingHeader = computeCacheBustingSearchParam(
const cacheBustingHeader = await computeCacheBustingSearchParam(
'1',
segmentPath,
undefined,
......@@ -1139,7 +1138,7 @@ export async function continueStaticFallbackPrerender(
// Insert generated tags to head
createHeadInsertionTransformStream(getServerInsertedHTML),
// Insert the client resume script into the head
createClientResumeScriptInsertionTransformStream(),
await createClientResumeScriptInsertionTransformStream(),
// Transform metadata
createMetadataTransformStream(getServerInsertedMetadata),
// Insert the inlined data (Flight data, form state, etc.) stream into the HTML
......
......@@ -38,6 +38,7 @@ import {
} from '../app-render/work-unit-async-storage.external'
import {
applyOwnerStack,
getRuntimeStage,
makeDevtoolsIOAwarePromise,
makeHangingPromise,
......@@ -628,6 +629,28 @@ function assertDefaultCacheLife(
}
}
// The maximum time we allow a `'use cache'` entry to fill. After this, we
// assume the fill is stalled — either on hanging input to the cached function,
// or on hanging I/O inside of it — and de-opt with an error.
//
// For prerender, the effective value is clamped to 90% of the configured
// `staticPageGenerationTimeout` so the cache-fill error surfaces before the
// build worker kills the page. In dev (`request`), the configured
// `experimental.useCacheTimeout` is used straight.
function getUseCacheFillTimeoutMs(
workStore: WorkStore,
workUnitStoreType: 'prerender' | 'prerender-runtime' | 'request'
): number {
const { useCacheTimeout, staticPageGenerationTimeout } = workStore
const effectiveTimeout =
workUnitStoreType === 'request'
? useCacheTimeout
: Math.min(useCacheTimeout, staticPageGenerationTimeout * 0.9)
return effectiveTimeout * 1000
}
function generateCacheEntryWithCacheContext(
workStore: WorkStore,
cacheContext: CacheContext,
......@@ -1051,18 +1074,20 @@ async function generateCacheEntryImpl(
)
let stream: ReadableStream<Uint8Array>
let devTimeoutSignal: AbortSignal | undefined
let devTimeoutTimer: ReturnType<typeof setTimeout> | undefined
switch (outerWorkUnitStore.type) {
case 'prerender-runtime':
case 'prerender':
const timeoutAbortController = new AbortController()
// If we're prerendering, we give you 50 seconds to fill a cache entry.
// Otherwise we assume you stalled on hanging input and de-opt. This needs
// to be lower than just the general timeout of 60 seconds.
const timer = setTimeout(() => {
workStore.invalidDynamicUsageError = timeoutError
timeoutAbortController.abort(timeoutError)
}, 50000)
const timer = setTimeout(
() => {
workStore.invalidDynamicUsageError = timeoutError
timeoutAbortController.abort(timeoutError)
},
getUseCacheFillTimeoutMs(workStore, outerWorkUnitStore.type)
)
const dynamicAccessAbortSignal =
dynamicAccessAsyncStorage.getStore()?.abortController.signal
......@@ -1127,18 +1152,43 @@ async function generateCacheEntryImpl(
}
break
case 'request':
// If we're filling caches for a staged render, make sure that
// it takes at least a task, so we'll always notice a cache miss between stages.
//
// TODO(restart-on-cache-miss): This is suboptimal.
// Ideally we wouldn't need to restart for microtasky caches,
// but the current logic for omitting short-lived caches only works correctly
// if we do a second render, so that's the best we can do until we refactor that.
if (
process.env.NODE_ENV === 'development' &&
outerWorkUnitStore.cacheSignal
) {
// TODO: We should just check if the render is abandonable. This is
// relevant in restart-on-cache-miss in general, so when we implement that
// for cached navs, it'll also be needed in prod
if (process.env.__NEXT_DEV_SERVER && outerWorkUnitStore.cacheSignal) {
// If we're filling caches for a staged render, make sure that it takes
// at least a task, so we'll always notice a cache miss between stages.
//
// TODO(restart-on-cache-miss): This is suboptimal. Ideally we wouldn't
// need to restart for microtasky caches, but the current logic for
// omitting short-lived caches only works correctly if we do a second
// render, so that's the best we can do until we refactor that.
await new Promise((resolve) => setTimeout(resolve))
// Start a cache-fill timeout so a hanging `'use cache'` entry surfaces
// the same error in dev as during prerender. Cleared when
// pendingCacheResult settles.
//
// Only skip the timeout when we're exactly in the Dynamic stage. That
// mirrors prerender, where caches guarded by e.g. `await connection()`
// aren't executed at all. We can't use `< RenderStage.Dynamic` here
// because `RenderStage.Abandoned` is numerically higher than Dynamic,
// but semantically it means the initial prospective render was aborted
// while caches are still pending — the outer flow then awaits
// `cacheSignal.cacheReady()`, so we need the timer to break a potential
// deadlock.
const stagedRendering = outerWorkUnitStore.stagedRendering
if (stagedRendering?.currentStage !== RenderStage.Dynamic) {
const devTimeoutAbortController = new AbortController()
devTimeoutSignal = devTimeoutAbortController.signal
devTimeoutTimer = setTimeout(
() => {
workStore.invalidDynamicUsageError = timeoutError
devTimeoutAbortController.abort(timeoutError)
},
getUseCacheFillTimeoutMs(workStore, outerWorkUnitStore.type)
)
}
}
// fallthrough
case 'prerender-ppr':
......@@ -1153,6 +1203,7 @@ async function generateCacheEntryImpl(
{
environmentName: 'Cache',
filterStackFrame,
signal: devTimeoutSignal,
temporaryReferences,
onError: handleError,
}
......@@ -1171,7 +1222,11 @@ async function generateCacheEntryImpl(
innerCacheStore,
startTime,
errors
)
).finally(() => {
if (devTimeoutTimer !== undefined) {
clearTimeout(devTimeoutTimer)
}
})
if (process.env.NODE_ENV === 'development') {
// Name the stream for React DevTools.
......@@ -1308,6 +1363,7 @@ export async function cache(
const timeoutError = new UseCacheTimeoutError()
Error.captureStackTrace(timeoutError, cache)
applyOwnerStack(timeoutError)
const wrapAsInvalidDynamicUsageError = (
error: Error,
......
......@@ -302,11 +302,21 @@ export async function adapter(
renderOpts: {
cacheLifeProfiles:
params.request.nextConfig?.experimental?.cacheLife,
// Proxy doesn't do static generation, so this value does not
// apply here. 0 is a sentinel: if something ever reads it,
// it'll surface loudly instead of silently using a misleading
// default.
staticPageGenerationTimeout: 0,
cacheComponents: false,
experimental: {
isRoutePPREnabled: false,
authInterrupts:
!!params.request.nextConfig?.experimental?.authInterrupts,
// Proxy doesn't fill Cache Components entries, so this value
// is never read. 0 is a sentinel: if something ever reads it,
// the cache fill will time out immediately and surface the
// bug.
useCacheTimeout: 0,
},
supportsDynamicResponse: true,
waitUntil,
......
......@@ -126,8 +126,16 @@ export class EdgeRouteModuleWrapper {
cacheComponents: !!process.env.__NEXT_CACHE_COMPONENTS,
experimental: {
authInterrupts: !!process.env.__NEXT_EXPERIMENTAL_AUTH_INTERRUPTS,
// Edge runtime doesn't support Cache Components, so this value is
// never read. 0 is a sentinel: if something ever reads it, the cache
// fill will time out immediately and surface the bug.
useCacheTimeout: 0,
},
cacheLifeProfiles: nextConfig.cacheLife,
// Edge runtime doesn't do static generation, so this value does not
// apply here. 0 is a sentinel: if something ever reads it, it'll
// surface loudly instead of silently using a misleading default.
staticPageGenerationTimeout: 0,
},
sharedContext: {
buildId: '', // TODO: Populate this properly.
......
import { hexHash } from '../../hash'
export function computeCacheBustingSearchParam(
const CACHE_BUSTING_SEARCH_PARAM_DIGEST_BYTES = 12
const textEncoder = new TextEncoder()
function encodeCacheBustingSearchParam(bytes: Uint8Array): string {
let binary = ''
for (let i = 0; i < bytes.length; i++) {
binary += String.fromCharCode(bytes[i])
}
return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
}
function normalizeCacheBustingInput(
value: string | string[] | undefined
): string {
if (value === undefined) {
return '0'
}
return Array.isArray(value) ? value.join(',') : value
}
function createCacheBustingSearchParamInput(
prefetchHeader: '1' | '2' | '0' | undefined,
segmentPrefetchHeader: string | string[] | undefined,
stateTreeHeader: string | string[] | undefined,
nextUrlHeader: string | string[] | undefined
): string {
): string | null {
if (
(prefetchHeader === undefined || prefetchHeader === '0') &&
segmentPrefetchHeader === undefined &&
stateTreeHeader === undefined &&
nextUrlHeader === undefined
) {
return null
}
return [
prefetchHeader ?? '0',
normalizeCacheBustingInput(segmentPrefetchHeader),
normalizeCacheBustingInput(stateTreeHeader),
normalizeCacheBustingInput(nextUrlHeader),
].join(',')
}
async function computeCacheBustingSearchParamFromInput(
input: string
): Promise<string> {
// Truncate SHA-256 to 96 bits to keep `_rsc` compact
const digest = await globalThis.crypto.subtle.digest(
'SHA-256',
textEncoder.encode(input)
)
return encodeCacheBustingSearchParam(
new Uint8Array(digest).subarray(0, CACHE_BUSTING_SEARCH_PARAM_DIGEST_BYTES)
)
}
export async function computeCacheBustingSearchParam(
prefetchHeader: '1' | '2' | '0' | undefined,
segmentPrefetchHeader: string | string[] | undefined,
stateTreeHeader: string | string[] | undefined,
nextUrlHeader: string | string[] | undefined
): Promise<string> {
const input = createCacheBustingSearchParamInput(
prefetchHeader,
segmentPrefetchHeader,
stateTreeHeader,
nextUrlHeader
)
if (input === null) {
return ''
}
return hexHash(
[
prefetchHeader || '0',
segmentPrefetchHeader || '0',
stateTreeHeader || '0',
nextUrlHeader || '0',
].join(',')
return computeCacheBustingSearchParamFromInput(input)
}
export function computeLegacyCacheBustingSearchParam(
prefetchHeader: '1' | '2' | '0' | undefined,
segmentPrefetchHeader: string | string[] | undefined,
stateTreeHeader: string | string[] | undefined,
nextUrlHeader: string | string[] | undefined
): string {
const input = createCacheBustingSearchParamInput(
prefetchHeader,
segmentPrefetchHeader,
stateTreeHeader,
nextUrlHeader
)
if (input === null) {
return ''
}
return hexHash(input)
}
......@@ -27,6 +27,7 @@
},
"devDependencies": {
"react-refresh": "0.12.0",
"typescript": "catalog:",
"webpack": "^4 || ^5"
}
}
This diff is collapsed.
......@@ -6,6 +6,11 @@ packages:
- 'turbopack/crates/*/js'
- 'turbopack/crates/turbopack-tests/tests/execution'
- 'turbopack/packages/*'
catalog:
eslint: 9.37.0
jest: 29.7.0
prettier: 3.6.2
typescript: 6.0.2
updateNotifier: false
publicHoistPattern:
- '*eslint*'
......
......@@ -223,14 +223,24 @@ describe('app dir - prefetching', () => {
16, // PrefetchHint.IsRootLayout
])
)
const response = await next.fetch(`/prefetch-auto/justputit?_rsc=dcqtr`, {
headers: {
rsc: '1',
'next-router-prefetch': '1',
'next-router-state-tree': stateTree,
'next-url': '/prefetch-auto/justputit',
},
})
const headers = {
rsc: '1',
'next-router-prefetch': '1',
'next-router-state-tree': stateTree,
'next-url': '/prefetch-auto/justputit',
} as const
const cacheBustingParam = await computeCacheBustingSearchParam(
headers['next-router-prefetch'],
undefined,
headers['next-router-state-tree'],
headers['next-url']
)
const response = await next.fetch(
`/prefetch-auto/justputit?_rsc=${cacheBustingParam}`,
{
headers,
}
)
const prefetchResponse = await response.text()
expect(prefetchResponse).not.toContain('Page Data!')
......@@ -267,7 +277,7 @@ describe('app dir - prefetching', () => {
}
const url = new URL('/prefetch-auto/justputit', 'http://localhost')
const cacheBustingParam = computeCacheBustingSearchParam(
const cacheBustingParam = await computeCacheBustingSearchParam(
headers['next-router-prefetch'] ? '1' : '0',
undefined,
headers['next-router-state-tree'],
......
import { nextTestSetup } from 'e2e-utils'
import { computeCacheBustingSearchParam } from 'next/dist/shared/lib/router/utils/cache-busting-search-param'
import {
computeCacheBustingSearchParam,
computeLegacyCacheBustingSearchParam,
} from 'next/dist/shared/lib/router/utils/cache-busting-search-param'
describe('app dir - validation', () => {
const { next, skipped } = nextTestSetup({
......@@ -29,13 +32,13 @@ describe('app dir - validation', () => {
const url2 = new URL('/', 'http://localhost')
// Add cache busting search param for both requests
const cacheBustingParam1 = computeCacheBustingSearchParam(
const cacheBustingParam1 = await computeCacheBustingSearchParam(
undefined,
undefined,
stateTree1,
undefined
)
const cacheBustingParam2 = computeCacheBustingSearchParam(
const cacheBustingParam2 = await computeCacheBustingSearchParam(
undefined,
undefined,
stateTree2,
......@@ -55,4 +58,51 @@ describe('app dir - validation', () => {
const res2 = await next.fetch(url2.toString(), { headers: headers2 })
expect(res2.status).toBe(200)
})
it('should generate distinct cache-busting params for known colliding RSC variants', async () => {
const stateTree = '%5B%22%22%2C%7B%7D%5D'
const fullRequestHash = await computeCacheBustingSearchParam(
undefined,
undefined,
stateTree,
undefined
)
const prefetchRequestHash = await computeCacheBustingSearchParam(
'1',
'/_tree',
stateTree,
'/pcsta0'
)
expect(fullRequestHash).toHaveLength(16)
expect(prefetchRequestHash).toHaveLength(16)
expect(fullRequestHash).not.toBe(prefetchRequestHash)
})
it('should accept legacy cache-busting params on plain HTTP requests', async () => {
const stateTree = '%5B%22%22%2C%7B%7D%5D'
const url = new URL('/', 'http://localhost')
const headers = {
rsc: '1',
'next-router-state-tree': stateTree,
}
url.searchParams.set(
'_rsc',
computeLegacyCacheBustingSearchParam(
undefined,
undefined,
stateTree,
undefined
)
)
const res = await next.fetch(url.toString(), {
headers,
redirect: 'manual',
})
expect(res.status).toBe(200)
})
})
import http from 'node:http'
import path from 'node:path'
import { nextTestSetup } from 'e2e-utils'
import { computeCacheBustingSearchParam } from 'next/dist/shared/lib/router/utils/cache-busting-search-param'
import cheerio from 'cheerio'
import { fetchViaHTTP, findPort } from 'next-test-utils'
describe('cache-components', () => {
const { next, isNextDev, skipped } = nextTestSetup({
const { next, isNextDev, isNextStart, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})
......@@ -349,6 +353,144 @@ describe('cache-components', () => {
}
})
if (isNextStart) {
it('should ignore late setHeader calls for direct RSC handlers after headers are sent', async () => {
const pageModulePath = path.join(
next.testDir,
'.next',
'server',
'app',
'cases',
'static',
'page.js'
)
const previousCwd = process.cwd()
const port = await findPort()
let server: http.Server | undefined
let handlerError: unknown
let lateHeaderAttempted = false
let lateHeaderError: unknown
let resolveHandled: (() => void) | undefined
const handled = new Promise<void>((resolve) => {
resolveHandled = resolve
})
try {
process.chdir(next.testDir)
const { handler } = require(pageModulePath) as {
handler: (
req: http.IncomingMessage,
res: http.ServerResponse,
ctx: {
requestMeta?: Record<string, unknown>
waitUntil?: (promise: Promise<void>) => void
}
) => Promise<void>
}
server = http.createServer(async (req, res) => {
const originalWriteHead = res.writeHead.bind(res)
res.writeHead = ((...args: any[]) => {
const result = originalWriteHead(...args)
if (!lateHeaderAttempted) {
lateHeaderAttempted = true
try {
res.setHeader('x-test-late', '1')
} catch (error) {
lateHeaderError = error
}
}
return result
}) as typeof res.writeHead
try {
await handler(req, res, {
waitUntil: () => {},
requestMeta: {
initURL: `https://localhost:${port}${req.url ?? '/'}`,
minimalMode: true,
relativeProjectDir: '.',
},
})
} catch (error) {
handlerError = error
if (!res.writableEnded) {
if (!res.headersSent) {
res.statusCode = 500
}
res.end()
}
} finally {
resolveHandled?.()
}
})
await new Promise<void>((resolve, reject) => {
server.listen(port, () => {
resolve()
})
server.once('error', reject)
})
const stateTree = JSON.stringify(['', {}])
const requestUrl = new URL('/cases/static', `http://localhost:${port}`)
const cacheBustingParam = computeCacheBustingSearchParam(
undefined,
undefined,
stateTree,
undefined
)
if (cacheBustingParam) {
requestUrl.searchParams.set('_rsc', cacheBustingParam)
}
const res = await fetchViaHTTP(
port,
requestUrl.pathname + requestUrl.search,
undefined,
{
headers: {
rsc: '1',
'next-router-state-tree': stateTree,
},
redirect: 'manual',
}
)
const flight = await res.text()
expect(res.status).toBe(200)
expect(res.headers.get('content-type')).toContain('text/x-component')
await handled
expect(handlerError).toBeUndefined()
expect(lateHeaderAttempted).toBe(true)
expect(lateHeaderError).toBeUndefined()
expect(flight.length).toBeGreaterThan(0)
} finally {
process.chdir(previousCwd)
if (server) {
await new Promise<void>((resolve, reject) => {
server.close((error) => {
if (error) {
reject(error)
return
}
resolve()
})
})
}
}
})
}
it('should not resume when client components are dynamic but the RSC render was static', async () => {
let html = await next.render('/cases/static-rsc-dynamic-client', {})
const $ = cheerio.load(html)
......
......@@ -47,18 +47,18 @@ const pages: Page[] = [
},
]
const addCacheBustingSearchParam = (
const addCacheBustingSearchParam = async (
pathname: string,
headers: Record<string, string | string[] | undefined>
) => {
const cacheKey = computeCacheBustingSearchParam(
const cacheKey = await computeCacheBustingSearchParam(
headers['next-router-prefetch'] ? '1' : '0',
headers['next-router-segment-prefetch'],
headers['next-router-state-tree'],
headers['next-url']
)
if (cacheKey === null) {
if (cacheKey.length === 0) {
return pathname
}
......@@ -582,7 +582,7 @@ describe.skip('ppr-full', () => {
rsc: '1',
'next-router-prefetch': '1',
}
const urlWithCacheBusting = addCacheBustingSearchParam(
const urlWithCacheBusting = await addCacheBustingSearchParam(
pathname,
headers
)
......@@ -619,7 +619,7 @@ describe.skip('ppr-full', () => {
'next-router-prefetch': '1',
'X-Test-Input': unexpected,
}
const urlWithCacheBusting = addCacheBustingSearchParam(
const urlWithCacheBusting = await addCacheBustingSearchParam(
pathname,
headers
)
......@@ -639,7 +639,7 @@ describe.skip('ppr-full', () => {
describe.each(pages)('for $pathname', ({ pathname, dynamic }) => {
it('should have correct headers', async () => {
const headers = { rsc: '1' }
const urlWithCacheBusting = addCacheBustingSearchParam(
const urlWithCacheBusting = await addCacheBustingSearchParam(
pathname,
headers
)
......@@ -671,7 +671,7 @@ describe.skip('ppr-full', () => {
rsc: '1',
'X-Test-Input': expected,
}
const urlWithCacheBusting = addCacheBustingSearchParam(
const urlWithCacheBusting = await addCacheBustingSearchParam(
pathname,
headers
)
......@@ -691,7 +691,7 @@ describe.skip('ppr-full', () => {
rsc: '1',
'X-Test-Input': unexpected,
}
const urlWithCacheBusting = addCacheBustingSearchParam(
const urlWithCacheBusting = await addCacheBustingSearchParam(
pathname,
headers
)
......
......@@ -30,7 +30,12 @@ describe('resume-data-cache', () => {
url.searchParams.set(
'_rsc',
computeCacheBustingSearchParam('1', '/__PAGE__', undefined, undefined)
await computeCacheBustingSearchParam(
'1',
'/__PAGE__',
undefined,
undefined
)
)
const rsc = await next
......
......@@ -476,7 +476,7 @@ describe('rewrite-headers', () => {
// Add cache busting param for RSC requests
if (headers.rsc === '1') {
const cacheBustingParam = computeCacheBustingSearchParam(
const cacheBustingParam = await computeCacheBustingSearchParam(
headers['next-router-prefetch'],
headers['next-router-segment-prefetch'],
undefined,
......
......@@ -135,7 +135,7 @@ describe('middleware-static-rewrite', () => {
it('should revalidate the overview page without replacing it with a 404', async () => {
const url = new URL('/my-team', 'http://localhost')
const rsc = computeCacheBustingSearchParam(
const rsc = await computeCacheBustingSearchParam(
'1',
'/_head',
undefined,
......
// Cache body sleeps 17s, which exceeds the configured dev `useCacheTimeout`
// (15s) — proving the configured value is actually applied in dev (and
// not the hard-coded default or some other value).
async function getCachedData(): Promise<string> {
'use cache'
await new Promise((resolve) => setTimeout(resolve, 17_000))
return 'cached'
}
async function Cached() {
const data = await getCachedData()
return <p id="result">{data}</p>
}
export default function Page() {
return <Cached />
}
// Cache body sleeps 11s, which falls between the build-time clamp (9s)
// and the configured dev `useCacheTimeout` (15s).
// - Dev: the cache fills successfully (15s > 11s, no clamp).
// - Build: the cache times out (clamp 9s < 11s).
async function getCachedData(): Promise<string> {
'use cache'
await new Promise((resolve) => setTimeout(resolve, 11_000))
return 'cached'
}
async function Cached() {
const data = await getCachedData()
return <p id="result">{data}</p>
}
export default function Page() {
return <Cached />
}
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
/**
* This configuration is NOT realistic — it's set up to exercise the clamp
* logic in both dev and build. In practice, users might tune
* `experimental.useCacheTimeout` up (e.g. for slower dev-time backends) or
* down (e.g. for faster feedback while iterating); what's unusual here is
* the very low `staticPageGenerationTimeout`.
*
* Setup:
* - `staticPageGenerationTimeout: 10` → build-time clamp = 9s, with a
* 1s buffer before the build worker kills the page at 10s.
* - Dev `useCacheTimeout: 15` → would be clamped to 9s if we clamped in
* dev. We don't, so the raw 15s applies.
* - Build `useCacheTimeout: 60` → clamped to 9s.
* - `/below-dev-timeout` sleeps 11s (between 9s and 15s).
* - `/above-dev-timeout` sleeps 17s (above 15s).
*
* This way:
* - Dev `/below-dev-timeout` succeeds (proves no clamp; if we clamped, it
* would time out at 9s < 11s).
* - Dev `/above-dev-timeout` times out at 15s (proves the configured dev
* value is actually applied).
* - Build fails on both pages via the clamp (9s < 11s, 9s < 17s).
*
* @type {import('next').NextConfig}
*/
const nextConfig = {
cacheComponents: true,
staticPageGenerationTimeout: 10,
experimental: {
useCacheTimeout: process.env.__NEXT_DEV_SERVER ? 15 : 60,
// Keep building all pages after a failure so the test can assert that
// both pages timed out via the clamp.
prerenderEarlyExit: false,
},
}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
import stripAnsi from 'strip-ansi'
const expectedTimeoutErrorMessage =
'Filling a cache during prerender timed out, likely because request-specific arguments such as params, searchParams, cookies() or dynamic data were used inside "use cache".'
describe('use-cache-configured-timeout', () => {
const { next, isNextDev, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
skipStart: process.env.NEXT_TEST_MODE !== 'dev',
})
if (skipped) {
return
}
if (isNextDev) {
describe('when a "use cache" fill is below the configured dev `useCacheTimeout`', () => {
it('should not clamp the dev timeout and allow the cache fill to complete', async () => {
const outputIndex = next.cliOutput.length
const browser = await next.browser('/below-dev-timeout')
await expect(browser.elementByCss('#result').text()).resolves.toBe(
'cached'
)
const cliOutput = stripAnsi(next.cliOutput.slice(outputIndex))
expect(cliOutput).not.toContain(expectedTimeoutErrorMessage)
})
})
describe('when a "use cache" fill exceeds the configured dev `useCacheTimeout`', () => {
it('should apply the configured timeout and show the error', async () => {
const outputIndex = next.cliOutput.length
const browser = await next.browser('/above-dev-timeout')
await expect(browser).toDisplayRedbox(`
{
"code": "E394",
"description": "Filling a cache during prerender timed out, likely because request-specific arguments such as params, searchParams, cookies() or dynamic data were used inside "use cache".",
"environmentLabel": null,
"label": "Runtime Error",
"source": "app/above-dev-timeout/page.tsx (4:1) @ getCachedData
> 4 | async function getCachedData(): Promise<string> {
| ^",
"stack": [
"getCachedData app/above-dev-timeout/page.tsx (4:1)",
"Cached app/above-dev-timeout/page.tsx (13:22)",
"Page app/above-dev-timeout/page.tsx (19:10)",
],
}
`)
const cliOutput = stripAnsi(next.cliOutput.slice(outputIndex))
expect(cliOutput).toContain(expectedTimeoutErrorMessage)
})
})
} else {
describe('when `experimental.useCacheTimeout` exceeds `staticPageGenerationTimeout` during prerendering', () => {
it('should clamp the build timeout and fail both pages with a timeout error', async () => {
try {
await next.start()
} catch {
// expected
}
expect(next.cliOutput).toContain(expectedTimeoutErrorMessage)
expect(next.cliOutput).toContain(
'Error occurred prerendering page "/below-dev-timeout"'
)
expect(next.cliOutput).toContain(
'Error occurred prerendering page "/above-dev-timeout"'
)
})
})
}
})
import { connection } from 'next/server'
import { ReactNode, Suspense } from 'react'
// A cache fill in the Dynamic stage (after `connection()`) should not be
// subject to the dev fill timeout. This mirrors prerender, where caches
// past `connection()` aren't executed at all and therefore not subject to
// the timeout handling either. The sleep is intentionally longer than the
// configured `experimental.useCacheTimeout` (10s) so the test fails if the
// timer isn't cleared.
async function Cached(): Promise<ReactNode> {
'use cache'
await new Promise((resolve) => setTimeout(resolve, 12_000))
return <p id="cached">cached</p>
}
async function Dynamic(): Promise<ReactNode> {
await connection()
return <Cached />
}
export default function Page() {
return (
<Suspense fallback="Loading...">
<Dynamic />
</Suspense>
)
}
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body>{children}</body>
</html>
)
}
import { cookies } from 'next/headers'
import { Suspense } from 'react'
import { getData, preload } from '../shared'
const sharedUrl =
'https://next-data-api-endpoint.vercel.app/api/random?page=runtime'
async function getCachedData(): Promise<string> {
'use cache'
// Joins the outer fetch via the module-scoped dedupe map (see note in
// ../shared.ts).
return getData(sharedUrl).then((res) => res.text())
}
async function Cached() {
// The timeout error must also be shown in the Next.js DevTools when the
// invocation is wrapped in a try/catch.
try {
const data = await getCachedData()
return <p id="result">{data}</p>
} catch (error) {
return <p id="result">Error: {error.message}</p>
}
}
async function Runtime() {
await cookies()
// Simulate another part of the tree (e.g. a sibling component or a shared
// data loader) kicking off the same fetch in outer scope before `Cached`
// runs.
preload(sharedUrl)
return <Cached />
}
export default function Page() {
return (
<Suspense fallback="Loading...">
<Runtime />
</Suspense>
)
}
// This module simulates a realistic migration hazard. The documented "preload"
// pattern (see the "Preloading data" section of the
// caching-without-cache-components guide in the Next.js docs) wraps the loader
// in `React.cache`, so its store is tied to the ALS snapshot — `'use cache'`
// bodies run in a clean snapshot and miss, re-executing the loader in cache
// scope. That path is safe during migration.
//
// What's modeled here is the common-in-the-wild variant that uses a
// module-scoped `Map` instead of `React.cache`. The map lives above ALS, so a
// `'use cache'` body joining it reuses the outer-scope promise (request scope
// or prerender scope) — a promise that was never meant to resolve for the
// cache:
// - During prerendering, an uncached fetch in the outer scope returns a
// hanging promise that intentionally never resolves.
// - In dev, the outer fetch is parked on the Dynamic stage, which can't
// advance until the cache fills. The cache awaits the outer fetch via the
// shared loader, so neither can make progress.
// In both cases, the cache never fills and times out.
const pendingFetches = new Map<string, Promise<Response>>()
export function getData(url: string): Promise<Response> {
let promise = pendingFetches.get(url)
if (!promise) {
promise = fetch(url)
pendingFetches.set(url, promise)
promise.then(
() => pendingFetches.delete(url),
() => pendingFetches.delete(url)
)
}
return promise
}
// "Preload" primes the shared loader without awaiting so later callers reuse
// the stored promise.
export function preload(url: string): void {
void getData(url)
}
import { getData, preload } from '../shared'
const sharedUrl =
'https://next-data-api-endpoint.vercel.app/api/random?page=static'
async function getCachedData(): Promise<string> {
'use cache'
// Joins the outer fetch via the module-scoped dedupe map (see note in
// ../shared.ts).
return getData(sharedUrl).then((res) => res.text())
}
async function Cached() {
// The timeout error must also be shown in the Next.js DevTools when the
// invocation is wrapped in a try/catch.
try {
const data = await getCachedData()
return <p id="result">{data}</p>
} catch (error) {
return <p id="result">Error: {error.message}</p>
}
}
export default function Page() {
// Simulate another part of the tree (e.g. a sibling component or a shared
// data loader) kicking off the same fetch in outer scope before `Cached`
// runs.
preload(sharedUrl)
return <Cached />
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
cacheComponents: true,
experimental: {
// Lower the `'use cache'` fill timeout so tests don't race the browser's
// page.goto ceiling (default ~60s) after the 54s default + compile time.
useCacheTimeout: 10,
},
}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
import stripAnsi from 'strip-ansi'
const expectedTimeoutErrorMessage =
'Filling a cache during prerender timed out, likely because request-specific arguments such as params, searchParams, cookies() or dynamic data were used inside "use cache".'
describe('use-cache-hanging', () => {
const { next, isNextDev, skipped, isTurbopack } = nextTestSetup({
files: __dirname,
skipDeployment: true,
skipStart: process.env.NEXT_TEST_MODE !== 'dev',
})
if (skipped) {
return
}
if (isNextDev) {
describe('when a "use cache" fill hangs in the static stage', () => {
it('should show an error toast after a timeout', async () => {
const outputIndex = next.cliOutput.length
const browser = await next.browser('/static')
await expect(browser).toDisplayRedbox(`
{
"code": "E394",
"description": "Filling a cache during prerender timed out, likely because request-specific arguments such as params, searchParams, cookies() or dynamic data were used inside "use cache".",
"environmentLabel": null,
"label": "Runtime Error",
"source": "app/static/page.tsx (6:1) @ getCachedData
> 6 | async function getCachedData(): Promise<string> {
| ^",
"stack": [
"getCachedData app/static/page.tsx (6:1)",
"Cached app/static/page.tsx (18:24)",
"Page app/static/page.tsx (32:10)",
],
}
`)
const cliOutput = stripAnsi(next.cliOutput.slice(outputIndex))
expect(cliOutput).toContain(`Error: ${expectedTimeoutErrorMessage}
at getCachedData (app/static/page.tsx:6:1)`)
})
})
describe('when a "use cache" fill hangs in the runtime stage', () => {
it('should show an error toast after a timeout', async () => {
const outputIndex = next.cliOutput.length
const browser = await next.browser('/runtime')
await expect(browser).toDisplayRedbox(`
{
"code": "E394",
"description": "Filling a cache during prerender timed out, likely because request-specific arguments such as params, searchParams, cookies() or dynamic data were used inside "use cache".",
"environmentLabel": null,
"label": "Runtime Error",
"source": "app/runtime/page.tsx (8:1) @ getCachedData
> 8 | async function getCachedData(): Promise<string> {
| ^",
"stack": [
"getCachedData app/runtime/page.tsx (8:1)",
"Cached app/runtime/page.tsx (20:24)",
"Page app/runtime/page.tsx (42:7)",
],
}
`)
const cliOutput = stripAnsi(next.cliOutput.slice(outputIndex))
expect(cliOutput).toContain(`Error: ${expectedTimeoutErrorMessage}
at getCachedData (app/runtime/page.tsx:8:1)`)
})
})
describe('when a "use cache" performs long-running I/O in the dynamic stage', () => {
it('should not time out', async () => {
const outputIndex = next.cliOutput.length
const browser = await next.browser('/dynamic')
await expect(browser.elementByCss('#cached').text()).resolves.toBe(
'cached'
)
const cliOutput = stripAnsi(next.cliOutput.slice(outputIndex))
expect(cliOutput).not.toContain(expectedTimeoutErrorMessage)
})
})
} else {
describe('when a "use cache" fill hangs during prerendering', () => {
it('should fail the build with a timeout error', async () => {
try {
await next.start()
} catch {
// expected
}
if (isTurbopack) {
expect(next.cliOutput)
.toContain(`Error: ${expectedTimeoutErrorMessage}
at <unknown> (app/static/page.tsx:6:1)`)
} else {
// Webpack production builds don't have source maps by default.
expect(next.cliOutput).toContain(expectedTimeoutErrorMessage)
}
})
})
}
})
......@@ -44,7 +44,6 @@ either = { workspace = true }
hashbrown = { workspace = true, features = ["raw"] }
indexmap = { workspace = true }
lzzzz = { workspace = true, optional = true }
once_cell = { workspace = true }
parking_lot = { workspace = true }
rand = { workspace = true }
ringmap = { workspace = true, features = ["serde"] }
......
......@@ -13,7 +13,6 @@ anyhow = { workspace = true }
arbitrary = { version = "1.4.1", features = ["derive"] }
bincode = { workspace = true }
libfuzzer-sys = "0.4.9"
once_cell = { workspace = true }
serde = { workspace = true }
tokio = { workspace = true, features = ["full"] }
turbo-tasks = { workspace = true, features = ["non_operation_vc_strongly_consistent"] }
......
use std::sync::Arc;
use std::sync::{Arc, LazyLock};
use anyhow::Result;
use arbitrary::Arbitrary;
use bincode::{Decode, Encode};
use once_cell::sync::Lazy;
use turbo_tasks::{self, NonLocalValue, State, TaskInput, TurboTasks, Vc, trace::TraceRawVcs};
use turbo_tasks_malloc::TurboMalloc;
......@@ -81,7 +80,7 @@ impl<'a> Iterator for Iter<'a> {
}
}
static RUNTIME: Lazy<tokio::runtime::Runtime> = Lazy::new(|| {
static RUNTIME: LazyLock<tokio::runtime::Runtime> = LazyLock::new(|| {
tokio::runtime::Builder::new_multi_thread()
.enable_all()
.on_thread_stop(|| {
......
use std::mem::take;
use std::{cell::LazyCell, mem::take};
use bincode::{Decode, Encode};
use once_cell::unsync::Lazy;
use rustc_hash::FxHashSet;
use smallvec::SmallVec;
use turbo_tasks::{
......@@ -131,7 +130,7 @@ impl UpdateCellOperation {
#[cfg(feature = "trace_task_dirty")]
let has_updated_key_hashes = updated_key_hashes.is_some();
let updated_key_hashes_set = updated_key_hashes.map(|updated_key_hashes| {
Lazy::new(|| updated_key_hashes.into_iter().collect::<FxHashSet<u64>>())
LazyCell::new(|| updated_key_hashes.into_iter().collect::<FxHashSet<u64>>())
});
// Collect dependent tasks only when not skipping invalidation.
......
......@@ -559,11 +559,14 @@ impl TurboFn<'_> {
let this = #converted_this;
let persistence = #persistence;
let mut arg = turbo_tasks::StackDynTaskInputsSlot::new(inputs);
static TRAIT_METHOD: turbo_tasks::macro_helpers::Lazy<&'static turbo_tasks::TraitMethod> =
turbo_tasks::macro_helpers::Lazy::new(|| #trait_type_ident.get(stringify!(#ident)));
static TRAIT_METHOD: &'static turbo_tasks::TraitMethod = &#trait_type_ident
.methods[turbo_tasks::macro_helpers::index_of_method_name(
#trait_type_ident.methods,
stringify!(#ident),
)];
<#output as turbo_tasks::task::TaskOutput>::try_from_raw_vc(
turbo_tasks::trait_call(
*TRAIT_METHOD,
TRAIT_METHOD,
this,
&mut arg,
persistence,
......
......@@ -69,7 +69,6 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
let trait_type_ident = get_trait_type_ident(trait_ident);
let mut dynamic_trait_fns = Vec::new();
let mut trait_methods: Vec<TokenStream2> = Vec::new();
let mut method_names: Vec<TokenStream2> = Vec::new();
let mut default_methods: Vec<TokenStream2> = Vec::new();
let mut native_functions = Vec::new();
let mut items: Vec<TokenStream2> = Vec::with_capacity(raw_items.len());
......@@ -223,7 +222,7 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
let method_name_str = syn::LitStr::new(&ident.to_string(), ident.span());
let index = trait_methods.len() as u8;
trait_methods.push(quote! {
#method_name_str => turbo_tasks::TraitMethod {
turbo_tasks::TraitMethod {
trait_type: &#trait_type_ident,
trait_name: stringify!(#trait_ident),
method_name: #method_name_str,
......@@ -231,7 +230,6 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
index: #index,
},
});
method_names.push(quote! { #method_name_str });
default_methods.push(quote! { Some(&#native_function_ident) });
native_functions.push(quote! {
......@@ -261,7 +259,7 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
let method_name_str = syn::LitStr::new(&ident.to_string(), ident.span());
let index = trait_methods.len() as u8;
trait_methods.push(quote! {
#method_name_str => turbo_tasks::TraitMethod {
turbo_tasks::TraitMethod {
trait_type: &#trait_type_ident,
trait_name: stringify!(#trait_ident),
method_name: #method_name_str,
......@@ -269,7 +267,6 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
index: #index,
},
});
method_names.push(quote! { #method_name_str });
default_methods.push(quote! { None });
quote! { ; }
};
......@@ -311,7 +308,7 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
extended_supertraits.push(quote!(turbo_tasks::debug::ValueDebug));
}
let num_methods = method_names.len();
let num_methods = default_methods.len();
let trait_name = global_name_for_type(quote! { dyn #trait_ident });
let expanded = quote! {
#[must_use]
......@@ -324,23 +321,17 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
#(#native_functions)*
turbo_tasks::macro_helpers::turbo_register!(
Box<dyn #trait_ident> => #trait_type_ident: turbo_tasks::TraitType = {
use turbo_tasks::macro_helpers::{phf, phf::phf_map};
Box<dyn #trait_ident> => #trait_type_ident: turbo_tasks::TraitType =
turbo_tasks::TraitType::new::<&'static dyn #trait_ident>(
stringify!(#trait_ident),
#trait_name,
phf_map! {
#(#trait_methods)*
},
&[#(#method_names),*],
&[#(#trait_methods)*],
&[#(#default_methods),*]
)
}
);
impl turbo_tasks::macro_helpers::TraitVtablePrototype for Box<dyn #trait_ident> {
const LEN: usize = #num_methods;
const NAMES: &[&str] = &[#(#method_names),*];
const DEFAULTS: &[Option<&turbo_tasks::macro_helpers::NativeFunction>] = &[#(#default_methods),*];
}
......@@ -354,8 +345,8 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
// TODO: Remove this Lazy VTableRegistry once trait resolution is fully migrated
fn get_impl_vtables() -> &'static turbo_tasks::macro_helpers::VTableRegistry<Self::ValueTrait> {
static registry: turbo_tasks::macro_helpers::Lazy<turbo_tasks::macro_helpers::VTableRegistry<dyn # trait_ident>> =
turbo_tasks::macro_helpers::Lazy::new(|| turbo_tasks::macro_helpers::VTableRegistry::new(turbo_tasks::registry::get_trait_type_id(&#trait_type_ident)));
static registry: ::std::sync::LazyLock<turbo_tasks::macro_helpers::VTableRegistry<dyn # trait_ident>> =
::std::sync::LazyLock::new(|| turbo_tasks::macro_helpers::VTableRegistry::new(turbo_tasks::registry::get_trait_type_id(&#trait_type_ident)));
&*registry
}
......
......@@ -35,9 +35,7 @@ event-listener = "5.4.0"
futures = { workspace = true }
indexmap = { workspace = true, features = ["serde"] }
inventory = { workspace = true }
once_cell = { workspace = true }
parking_lot = { workspace = true, features = ["serde"]}
phf = { workspace = true }
pin-project-lite = { workspace = true }
rayon = { workspace = true }
regex = { workspace = true }
......
//! Runtime helpers for [turbo-tasks-macro].
use std::sync::LazyLock;
pub use async_trait::async_trait;
pub use bincode;
pub use inventory;
pub use once_cell::sync::{Lazy, OnceCell};
pub use phf;
use rustc_hash::FxHashMap;
pub use shrink_to_fit;
pub use tracing;
......@@ -27,7 +27,7 @@ pub use crate::{
registry::RegistryDef,
task::function::{into_task_fn, into_task_fn_with_this},
turbo_register,
value_type::{TraitVtablePrototype, build_trait_vtable},
value_type::{TraitVtablePrototype, build_trait_vtable, index_of_method_name},
};
#[cfg(debug_assertions)]
......@@ -209,18 +209,19 @@ unsafe impl Sync for RawPtr {}
unsafe impl Send for RawPtr {}
// Accumulate all trait impls by trait id
static TRAIT_CAST_FNS: Lazy<FxDashMap<TraitTypeId, Vec<(ValueTypeId, RawPtr)>>> = Lazy::new(|| {
let map: FxDashMap<TraitTypeId, Vec<(ValueTypeId, RawPtr)>> = FxDashMap::default();
for CollectableTraitCastFunctions(trait_id_fn, value_id_fn, cast_fn) in
inventory::iter::<CollectableTraitCastFunctions>
{
map.entry(trait_id_fn())
.or_default()
.value_mut()
.push((value_id_fn(), RawPtr(*cast_fn)));
}
map
});
static TRAIT_CAST_FNS: LazyLock<FxDashMap<TraitTypeId, Vec<(ValueTypeId, RawPtr)>>> =
LazyLock::new(|| {
let map: FxDashMap<TraitTypeId, Vec<(ValueTypeId, RawPtr)>> = FxDashMap::default();
for CollectableTraitCastFunctions(trait_id_fn, value_id_fn, cast_fn) in
inventory::iter::<CollectableTraitCastFunctions>
{
map.entry(trait_id_fn())
.or_default()
.value_mut()
.push((value_id_fn(), RawPtr(*cast_fn)));
}
map
});
// Holds a raw pointer to a function that can perform a fat pointer cast
pub struct CollectableTraitCastFunctions(
......
use std::{cell::SyncUnsafeCell, num::NonZeroU16};
use std::{cell::SyncUnsafeCell, num::NonZeroU16, sync::LazyLock};
use anyhow::Error;
use once_cell::sync::Lazy;
use crate::{
TraitType, ValueType,
......@@ -134,16 +133,16 @@ fn init_registry<T: Registerable>(mut items: Vec<&'static T>) -> Box<[&'static T
/// Get an item by its ID from a registry slice
#[inline]
fn get_item<T: Registerable>(registry: &Lazy<Box<[&'static T]>>, id: T::Id) -> &'static T {
fn get_item<T: Registerable>(registry: &LazyLock<Box<[&'static T]>>, id: T::Id) -> &'static T {
registry[*id as usize - 1]
}
/// Get the ID for a registered item. Forces registry init if needed, which
/// assigns IDs to all items as a side effect.
#[inline]
fn get_id<T: Registerable>(registry: &Lazy<Box<[&'static T]>>, item: &'static T) -> T::Id {
Lazy::force(registry);
// SAFETY: The ID write happens-before this read thanks to the fence inside of Lazy
fn get_id<T: Registerable>(registry: &LazyLock<Box<[&'static T]>>, item: &'static T) -> T::Id {
LazyLock::force(registry);
// SAFETY: The ID write happens-before this read thanks to the fence inside of LazyLock
let n = unsafe { std::ptr::read(item.ty().id.get()) };
let Some(id) = NonZeroU16::new(n) else {
panic!(
......@@ -156,7 +155,10 @@ fn get_id<T: Registerable>(registry: &Lazy<Box<[&'static T]>>, item: &'static T)
}
/// Validate that an ID is within the valid range
fn validate_id<T: Registerable>(registry: &Lazy<Box<[&'static T]>>, id: T::Id) -> Option<Error> {
fn validate_id<T: Registerable>(
registry: &LazyLock<Box<[&'static T]>>,
id: T::Id,
) -> Option<Error> {
let len = registry.len();
if *id as usize <= len {
None
......@@ -168,7 +170,7 @@ fn validate_id<T: Registerable>(registry: &Lazy<Box<[&'static T]>>, id: T::Id) -
}
}
static FUNCTIONS: Lazy<Box<[&'static NativeFunction]>> = Lazy::new(|| {
static FUNCTIONS: LazyLock<Box<[&'static NativeFunction]>> = LazyLock::new(|| {
init_registry(
inventory::iter::<&'static NativeFunction>
.into_iter()
......@@ -191,7 +193,7 @@ pub fn validate_function_id(id: FunctionId) -> Option<Error> {
validate_id(&FUNCTIONS, id)
}
pub(crate) static VALUES: Lazy<Box<[&'static ValueType]>> = Lazy::new(|| {
pub(crate) static VALUES: LazyLock<Box<[&'static ValueType]>> = LazyLock::new(|| {
let items = init_registry(
inventory::iter::<&'static ValueType>
.into_iter()
......@@ -222,7 +224,7 @@ pub(crate) fn trait_type_count() -> usize {
TRAITS.len()
}
static TRAITS: Lazy<Box<[&'static TraitType]>> = Lazy::new(|| {
static TRAITS: LazyLock<Box<[&'static TraitType]>> = LazyLock::new(|| {
init_registry(
inventory::iter::<&'static TraitType>
.into_iter()
......
......@@ -6,14 +6,13 @@ use std::{
marker::PhantomData,
panic::{self, AssertUnwindSafe, catch_unwind},
sync::{
Arc,
Arc, LazyLock,
atomic::{AtomicUsize, Ordering},
},
thread::{self, Thread, available_parallelism},
time::{Duration, Instant},
};
use once_cell::sync::Lazy;
use parking_lot::{Condvar, Mutex};
use tokio::{runtime::Handle, task::block_in_place};
use tracing::{Span, info_span};
......@@ -22,7 +21,8 @@ use crate::{TurboTasksApi, manager::try_turbo_tasks, turbo_tasks_scope};
/// Number of worker tasks to spawn that process jobs. It's 1 less than the number of cpus as we
/// also use the current task as worker.
static WORKER_TASKS: Lazy<usize> = Lazy::new(|| available_parallelism().map_or(0, |n| n.get() - 1));
static WORKER_TASKS: LazyLock<usize> =
LazyLock::new(|| available_parallelism().map_or(0, |n| n.get() - 1));
enum WorkQueueJob {
Job(usize, Box<dyn FnOnce() + Send + 'static>),
......
......@@ -255,8 +255,7 @@ impl TraitMethod {
pub struct TraitType {
pub ty: RegistryType,
pub methods: phf::Map<&'static str, TraitMethod>,
pub method_names: &'static [&'static str],
pub methods: &'static [TraitMethod],
pub default_methods: &'static [Option<&'static NativeFunction>],
}
......@@ -264,8 +263,8 @@ impl Debug for TraitType {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut d = f.debug_struct("TraitType");
d.field("name", &self.ty.name);
for (name, method) in self.methods.entries() {
d.field(name, method);
for method in self.methods.iter() {
d.field(method.method_name, method);
}
d.finish()
}
......@@ -281,20 +280,22 @@ impl TraitType {
pub const fn new<T: 'static>(
name: &'static str,
global_name: &'static str,
methods: phf::Map<&'static str, TraitMethod>,
method_names: &'static [&'static str],
methods: &'static [TraitMethod],
default_methods: &'static [Option<&'static NativeFunction>],
) -> Self {
Self {
ty: RegistryType::new::<T>(name, global_name),
methods,
method_names,
default_methods,
}
}
#[cfg(test)]
pub fn get(&self, name: &str) -> &TraitMethod {
self.methods.get(name).unwrap()
self.methods
.iter()
.find(|method| method.method_name == name)
.expect("Method not found!")
}
}
......@@ -302,17 +303,20 @@ turbo_registry!("Trait", TraitType);
pub trait TraitVtablePrototype {
const LEN: usize;
const NAMES: &'static [&'static str];
const DEFAULTS: &'static [Option<&'static NativeFunction>];
}
pub(crate) const fn index_of_name(array: &'static [&'static str], name: &'static str) -> usize {
/// Linear-scan lookup of a [`TraitMethod`] by `method_name` in a `&'static [TraitMethod]`. Const
/// so the `value_trait` macro's per-method dispatch site can resolve to an array index at
/// compile time.
pub const fn index_of_method_name(methods: &'static [TraitMethod], name: &'static str) -> usize {
let mut i = 0;
'outer: while i < array.len() {
if array[i].len() == name.len() {
'outer: while i < methods.len() {
let entry = methods[i].method_name;
if entry.len() == name.len() {
let mut j = 0;
while j < name.len() {
if array[i].as_bytes()[j] != name.as_bytes()[j] {
if entry.as_bytes()[j] != name.as_bytes()[j] {
i += 1;
continue 'outer;
}
......@@ -325,7 +329,10 @@ pub(crate) const fn index_of_name(array: &'static [&'static str], name: &'static
panic!("Method not found!")
}
pub const fn build_trait_vtable<B: TraitVtablePrototype, const LEN: usize>(
pub const fn build_trait_vtable<
B: TraitVtablePrototype + crate::registry::RegistryDef<TraitType>,
const LEN: usize,
>(
overrides: &[(&'static str, &'static NativeFunction)],
) -> [&'static NativeFunction; LEN] {
let mut methods = [&crate::native_function::VTABLE_DEFAULT; LEN];
......@@ -340,7 +347,10 @@ pub const fn build_trait_vtable<B: TraitVtablePrototype, const LEN: usize>(
let mut i = 0;
while i < overrides.len() {
let (name, f) = overrides[i];
methods[index_of_name(B::NAMES, name)] = f;
methods[index_of_method_name(
<B as crate::registry::RegistryDef<TraitType>>::DEF.methods,
name,
)] = f;
i += 1;
}
methods
......
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