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: ...@@ -42,6 +42,7 @@ jobs:
if: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.fork }} if: ${{ github.event_name != 'pull_request' || github.event.pull_request.head.repo.fork }}
outputs: outputs:
value: ${{ steps.deploy-target.outputs.value }} value: ${{ steps.deploy-target.outputs.value }}
release_environment: ${{ steps.deploy-target.outputs.release_environment }}
steps: steps:
- uses: actions/checkout@v6 - uses: actions/checkout@v6
with: with:
...@@ -62,9 +63,31 @@ jobs: ...@@ -62,9 +63,31 @@ jobs:
# 'staging' for canary branch since that will eventually be published i.e. become the production build. # 'staging' for canary branch since that will eventually be published i.e. become the production build.
id: deploy-target id: deploy-target
run: | 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 then
echo "value=production" >> $GITHUB_OUTPUT 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' ] elif [ ''"${GIT_REF}"'' == 'refs/heads/canary' ]
then then
echo "value=staging" >> $GITHUB_OUTPUT echo "value=staging" >> $GITHUB_OUTPUT
...@@ -472,6 +495,7 @@ jobs: ...@@ -472,6 +495,7 @@ jobs:
if: ${{ needs.deploy-target.outputs.value == 'production' }} if: ${{ needs.deploy-target.outputs.value == 'production' }}
name: Potentially publish release name: Potentially publish release
runs-on: ubuntu-latest runs-on: ubuntu-latest
environment: ${{ needs.deploy-target.outputs.release_environment }}
needs: needs:
- deploy-target - deploy-target
- build - build
......
...@@ -45,7 +45,6 @@ jobs: ...@@ -45,7 +45,6 @@ jobs:
# canary next-swc binaries in the monorepo # canary next-swc binaries in the monorepo
NEXT_SKIP_NATIVE_POSTINSTALL: 1 NEXT_SKIP_NATIVE_POSTINSTALL: 1
environment: release-${{ github.event.inputs.releaseType || 'canary' }}
steps: steps:
- name: Setup node - name: Setup node
uses: actions/setup-node@v4 uses: actions/setup-node@v4
......
...@@ -9900,9 +9900,7 @@ dependencies = [ ...@@ -9900,9 +9900,7 @@ dependencies = [
"futures", "futures",
"indexmap 2.13.0", "indexmap 2.13.0",
"inventory", "inventory",
"once_cell",
"parking_lot", "parking_lot",
"phf",
"pin-project-lite", "pin-project-lite",
"rayon", "rayon",
"regex", "regex",
...@@ -9942,7 +9940,6 @@ dependencies = [ ...@@ -9942,7 +9940,6 @@ dependencies = [
"indexmap 2.13.0", "indexmap 2.13.0",
"indoc", "indoc",
"lzzzz", "lzzzz",
"once_cell",
"parking_lot", "parking_lot",
"rand 0.10.0", "rand 0.10.0",
"regex", "regex",
...@@ -9975,7 +9972,6 @@ dependencies = [ ...@@ -9975,7 +9972,6 @@ dependencies = [
"arbitrary", "arbitrary",
"bincode 2.0.1", "bincode 2.0.1",
"libfuzzer-sys", "libfuzzer-sys",
"once_cell",
"serde", "serde",
"tokio", "tokio",
"turbo-tasks", "turbo-tasks",
......
...@@ -35,10 +35,11 @@ ...@@ -35,10 +35,11 @@
"@types/react": "^18", "@types/react": "^18",
"@types/react-dom": "^18", "@types/react-dom": "^18",
"cross-env": "10.1.0", "cross-env": "10.1.0",
"eslint": "catalog:",
"inliner": "1.13.1", "inliner": "1.13.1",
"postcss": "^8.5", "postcss": "^8.5",
"tailwindcss": "^4.1.9", "tailwindcss": "^4.1.9",
"tw-animate-css": "1.3.3", "tw-animate-css": "1.3.3",
"typescript": "6.0.2" "typescript": "catalog:"
} }
} }
...@@ -198,7 +198,7 @@ ...@@ -198,7 +198,7 @@
"dd-trace": "4.12.0", "dd-trace": "4.12.0",
"es5-ext": "0.10.53", "es5-ext": "0.10.53",
"escape-string-regexp": "2.0.0", "escape-string-regexp": "2.0.0",
"eslint": "9.37.0", "eslint": "catalog:",
"eslint-config-next": "workspace:*", "eslint-config-next": "workspace:*",
"eslint-formatter-codeframe": "7.32.1", "eslint-formatter-codeframe": "7.32.1",
"eslint-plugin-eslint-plugin": "5.2.1", "eslint-plugin-eslint-plugin": "5.2.1",
...@@ -230,7 +230,7 @@ ...@@ -230,7 +230,7 @@
"image-size": "1.2.1", "image-size": "1.2.1",
"is-animated": "2.0.2", "is-animated": "2.0.2",
"isomorphic-unfetch": "3.0.0", "isomorphic-unfetch": "3.0.0",
"jest": "29.7.0", "jest": "catalog:",
"jest-diff": "29.7.0", "jest-diff": "29.7.0",
"jest-environment-jsdom": "29.7.0", "jest-environment-jsdom": "29.7.0",
"jest-extended": "4.0.2", "jest-extended": "4.0.2",
...@@ -262,7 +262,7 @@ ...@@ -262,7 +262,7 @@
"postcss-pseudoelements": "5.0.0", "postcss-pseudoelements": "5.0.0",
"postcss-short-size": "4.0.0", "postcss-short-size": "4.0.0",
"postcss-trolling": "0.1.7", "postcss-trolling": "0.1.7",
"prettier": "3.6.2", "prettier": "catalog:",
"pretty-bytes": "5.3.0", "pretty-bytes": "5.3.0",
"pretty-ms": "7.0.0", "pretty-ms": "7.0.0",
"random-seed": "0.3.0", "random-seed": "0.3.0",
...@@ -302,7 +302,7 @@ ...@@ -302,7 +302,7 @@
"tsec": "0.2.1", "tsec": "0.2.1",
"tsx": "4.19.2", "tsx": "4.19.2",
"turbo": "2.9.4", "turbo": "2.9.4",
"typescript": "6.0.2", "typescript": "catalog:",
"typescript-eslint": "8.46.0", "typescript-eslint": "8.46.0",
"unfetch": "4.2.0", "unfetch": "4.2.0",
"wait-port": "0.2.2", "wait-port": "0.2.2",
......
...@@ -41,6 +41,7 @@ ...@@ -41,6 +41,7 @@
"cross-spawn": "7.0.3", "cross-spawn": "7.0.3",
"fast-glob": "3.3.1", "fast-glob": "3.3.1",
"picocolors": "1.1.1", "picocolors": "1.1.1",
"prettier": "catalog:",
"prettier-plugin-tailwindcss": "0.6.2", "prettier-plugin-tailwindcss": "0.6.2",
"prompts": "2.4.2", "prompts": "2.4.2",
"tar": "7.5.7", "tar": "7.5.7",
......
...@@ -17,7 +17,8 @@ ...@@ -17,7 +17,8 @@
}, },
"devDependencies": { "devDependencies": {
"@types/eslint": "9.6.1", "@types/eslint": "9.6.1",
"eslint": "9.37.0" "eslint": "9.37.0",
"typescript": "catalog:"
}, },
"scripts": { "scripts": {
"dev": "pnpm build", "dev": "pnpm build",
......
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
"@types/jscodeshift": "0.11.0", "@types/jscodeshift": "0.11.0",
"@types/prompts": "2.4.2", "@types/prompts": "2.4.2",
"@types/semver": "7.3.1", "@types/semver": "7.3.1",
"typescript": "6.0.2" "jest": "catalog:",
"typescript": "catalog:"
} }
} }
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
"devDependencies": { "devDependencies": {
"@vercel/ncc": "0.34.0", "@vercel/ncc": "0.34.0",
"dotenv": "16.3.1", "dotenv": "16.3.1",
"dotenv-expand": "10.0.0" "dotenv-expand": "10.0.0",
"typescript": "catalog:"
} }
} }
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
"@types/jest": "^29.5.0", "@types/jest": "^29.5.0",
"@vercel/ncc": "0.34.0", "@vercel/ncc": "0.34.0",
"jest": "^29.5.0", "jest": "^29.5.0",
"ts-jest": "^29.1.0" "ts-jest": "^29.1.0",
"typescript": "catalog:"
} }
} }
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
"resolveJsonModule": true, "resolveJsonModule": true,
"esModuleInterop": true, "esModuleInterop": true,
"skipLibCheck": false, "skipLibCheck": false,
"lib": ["es2022", "dom"], "lib": ["es2022", "dom", "dom.iterable"],
"rootDir": "src", "rootDir": "src",
"types": ["jest"] "types": ["jest"]
}, },
......
...@@ -2098,6 +2098,8 @@ export default async function build( ...@@ -2098,6 +2098,8 @@ export default async function build(
configFileName, configFileName,
cacheComponents: isAppCacheComponentsEnabled, cacheComponents: isAppCacheComponentsEnabled,
authInterrupts: isAuthInterruptsEnabled, authInterrupts: isAuthInterruptsEnabled,
useCacheTimeout: config.experimental.useCacheTimeout,
staticPageGenerationTimeout: config.staticPageGenerationTimeout,
httpAgentOptions: config.httpAgentOptions, httpAgentOptions: config.httpAgentOptions,
locales: config.i18n?.locales, locales: config.i18n?.locales,
defaultLocale: config.i18n?.defaultLocale, defaultLocale: config.i18n?.defaultLocale,
...@@ -2325,6 +2327,10 @@ export default async function build( ...@@ -2325,6 +2327,10 @@ export default async function build(
pageType, pageType,
cacheComponents: isAppCacheComponentsEnabled, cacheComponents: isAppCacheComponentsEnabled,
authInterrupts: isAuthInterruptsEnabled, authInterrupts: isAuthInterruptsEnabled,
useCacheTimeout:
config.experimental.useCacheTimeout,
staticPageGenerationTimeout:
config.staticPageGenerationTimeout,
cacheHandler: config.cacheHandler, cacheHandler: config.cacheHandler,
cacheHandlers: config.cacheHandlers, cacheHandlers: config.cacheHandlers,
isrFlushToDisk: ciEnvironment.hasNextSupport isrFlushToDisk: ciEnvironment.hasNextSupport
......
...@@ -782,6 +782,8 @@ export async function buildAppStaticPaths({ ...@@ -782,6 +782,8 @@ export async function buildAppStaticPaths({
distDir, distDir,
cacheComponents, cacheComponents,
authInterrupts, authInterrupts,
useCacheTimeout,
staticPageGenerationTimeout,
segments, segments,
isrFlushToDisk, isrFlushToDisk,
cacheHandler, cacheHandler,
...@@ -802,6 +804,8 @@ export async function buildAppStaticPaths({ ...@@ -802,6 +804,8 @@ export async function buildAppStaticPaths({
route: NormalizedAppRoute route: NormalizedAppRoute
cacheComponents: boolean cacheComponents: boolean
authInterrupts: boolean authInterrupts: boolean
useCacheTimeout: number
staticPageGenerationTimeout: number
segments: readonly Readonly<AppSegment>[] segments: readonly Readonly<AppSegment>[]
distDir: string distDir: string
isrFlushToDisk?: boolean isrFlushToDisk?: boolean
...@@ -859,10 +863,12 @@ export async function buildAppStaticPaths({ ...@@ -859,10 +863,12 @@ export async function buildAppStaticPaths({
renderOpts: { renderOpts: {
incrementalCache, incrementalCache,
cacheLifeProfiles, cacheLifeProfiles,
staticPageGenerationTimeout,
supportsDynamicResponse: true, supportsDynamicResponse: true,
cacheComponents, cacheComponents,
experimental: { experimental: {
authInterrupts, authInterrupts,
useCacheTimeout,
}, },
waitUntil: afterRunner.context.waitUntil, waitUntil: afterRunner.context.waitUntil,
onClose: afterRunner.context.onClose, onClose: afterRunner.context.onClose,
......
...@@ -860,6 +860,7 @@ export async function handler( ...@@ -860,6 +860,7 @@ export async function handler(
prefetchHints: prefetchHintsManifest, prefetchHints: prefetchHintsManifest,
incrementalCache, incrementalCache,
cacheLifeProfiles: nextConfig.cacheLife, cacheLifeProfiles: nextConfig.cacheLife,
staticPageGenerationTimeout: nextConfig.staticPageGenerationTimeout,
basePath: nextConfig.basePath, basePath: nextConfig.basePath,
serverActions: nextConfig.experimental.serverActions, serverActions: nextConfig.experimental.serverActions,
logServerFunctions: logServerFunctions:
...@@ -888,6 +889,7 @@ export async function handler( ...@@ -888,6 +889,7 @@ export async function handler(
inlineCss: Boolean(nextConfig.experimental.inlineCss), inlineCss: Boolean(nextConfig.experimental.inlineCss),
prefetchInlining: nextConfig.experimental.prefetchInlining ?? false, prefetchInlining: nextConfig.experimental.prefetchInlining ?? false,
authInterrupts: Boolean(nextConfig.experimental.authInterrupts), authInterrupts: Boolean(nextConfig.experimental.authInterrupts),
useCacheTimeout: nextConfig.experimental.useCacheTimeout,
cachedNavigations: Boolean( cachedNavigations: Boolean(
nextConfig.experimental.cachedNavigations nextConfig.experimental.cachedNavigations
), ),
...@@ -1791,7 +1793,9 @@ export async function handler( ...@@ -1791,7 +1793,9 @@ export async function handler(
const instantTestRequestId = const instantTestRequestId =
routeModule.isDev === true ? crypto.randomUUID() : null routeModule.isDev === true ? crypto.randomUUID() : null
body.pipeThrough( body.pipeThrough(
createInstantTestScriptInsertionTransformStream(instantTestRequestId) await createInstantTestScriptInsertionTransformStream(
instantTestRequestId
)
) )
return sendRenderResult({ return sendRenderResult({
req, req,
......
...@@ -243,11 +243,13 @@ export async function handler( ...@@ -243,11 +243,13 @@ export async function handler(
renderOpts: { renderOpts: {
experimental: { experimental: {
authInterrupts: Boolean(nextConfig.experimental.authInterrupts), authInterrupts: Boolean(nextConfig.experimental.authInterrupts),
useCacheTimeout: nextConfig.experimental.useCacheTimeout,
}, },
cacheComponents: Boolean(nextConfig.cacheComponents), cacheComponents: Boolean(nextConfig.cacheComponents),
supportsDynamicResponse, supportsDynamicResponse,
incrementalCache, incrementalCache,
cacheLifeProfiles: nextConfig.cacheLife, cacheLifeProfiles: nextConfig.cacheLife,
staticPageGenerationTimeout: nextConfig.staticPageGenerationTimeout,
waitUntil: ctx.waitUntil, waitUntil: ctx.waitUntil,
onClose: (cb) => { onClose: (cb) => {
res.on('close', cb) res.on('close', cb)
......
...@@ -152,6 +152,7 @@ async function requestHandler( ...@@ -152,6 +152,7 @@ async function requestHandler(
multiZoneDraftMode: false, multiZoneDraftMode: false,
cacheLifeProfiles: nextConfig.cacheLife, cacheLifeProfiles: nextConfig.cacheLife,
staticPageGenerationTimeout: nextConfig.staticPageGenerationTimeout,
basePath: nextConfig.basePath, basePath: nextConfig.basePath,
serverActions: nextConfig.experimental.serverActions, serverActions: nextConfig.experimental.serverActions,
logServerFunctions: logServerFunctions:
...@@ -167,6 +168,7 @@ async function requestHandler( ...@@ -167,6 +168,7 @@ async function requestHandler(
inlineCss: Boolean(nextConfig.experimental.inlineCss), inlineCss: Boolean(nextConfig.experimental.inlineCss),
prefetchInlining: nextConfig.experimental.prefetchInlining ?? false, prefetchInlining: nextConfig.experimental.prefetchInlining ?? false,
authInterrupts: Boolean(nextConfig.experimental.authInterrupts), authInterrupts: Boolean(nextConfig.experimental.authInterrupts),
useCacheTimeout: nextConfig.experimental.useCacheTimeout,
cachedNavigations: Boolean(nextConfig.experimental.cachedNavigations), cachedNavigations: Boolean(nextConfig.experimental.cachedNavigations),
clientTraceMetadata: clientTraceMetadata:
nextConfig.experimental.clientTraceMetadata || ([] as any), nextConfig.experimental.clientTraceMetadata || ([] as any),
......
...@@ -684,6 +684,8 @@ export async function isPageStatic({ ...@@ -684,6 +684,8 @@ export async function isPageStatic({
pageType, pageType,
cacheComponents, cacheComponents,
authInterrupts, authInterrupts,
useCacheTimeout,
staticPageGenerationTimeout,
originalAppPath, originalAppPath,
isrFlushToDisk, isrFlushToDisk,
cacheMaxMemorySize, cacheMaxMemorySize,
...@@ -702,6 +704,8 @@ export async function isPageStatic({ ...@@ -702,6 +704,8 @@ export async function isPageStatic({
distDir: string distDir: string
cacheComponents: boolean cacheComponents: boolean
authInterrupts: boolean authInterrupts: boolean
useCacheTimeout: number
staticPageGenerationTimeout: number
configFileName: string configFileName: string
httpAgentOptions: NextConfigComplete['httpAgentOptions'] httpAgentOptions: NextConfigComplete['httpAgentOptions']
locales?: readonly string[] locales?: readonly string[]
...@@ -874,6 +878,8 @@ export async function isPageStatic({ ...@@ -874,6 +878,8 @@ export async function isPageStatic({
route, route,
cacheComponents, cacheComponents,
authInterrupts, authInterrupts,
useCacheTimeout,
staticPageGenerationTimeout,
segments, segments,
distDir, distDir,
requestHeaders: {}, requestHeaders: {},
......
...@@ -532,7 +532,7 @@ export async function createFetch<T>( ...@@ -532,7 +532,7 @@ export async function createFetch<T>(
// search param to it. This should not leak outside of this function, so we // search param to it. This should not leak outside of this function, so we
// track them separately. // track them separately.
let fetchUrl = new URL(url) let fetchUrl = new URL(url)
setCacheBustingSearchParam(fetchUrl, headers) await setCacheBustingSearchParam(fetchUrl, headers)
let processed = fetch(fetchUrl, fetchOptions).then(processFetch) let processed = fetch(fetchUrl, fetchOptions).then(processFetch)
let fetchPromise = processed.then(({ response }) => response) let fetchPromise = processed.then(({ response }) => response)
...@@ -602,7 +602,7 @@ export async function createFetch<T>( ...@@ -602,7 +602,7 @@ export async function createFetch<T>(
// fetch again. // fetch again.
// TODO: We should abort the previous request. // TODO: We should abort the previous request.
fetchUrl = new URL(responseUrl) fetchUrl = new URL(responseUrl)
setCacheBustingSearchParam(fetchUrl, headers) await setCacheBustingSearchParam(fetchUrl, headers)
processed = fetch(fetchUrl, fetchOptions).then(processFetch) processed = fetch(fetchUrl, fetchOptions).then(processFetch)
fetchPromise = processed.then(({ response }) => response) fetchPromise = processed.then(({ response }) => response)
flightResponsePromise = shouldImmediatelyDecode 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' '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 { import {
NEXT_ROUTER_PREFETCH_HEADER, NEXT_ROUTER_PREFETCH_HEADER,
NEXT_ROUTER_SEGMENT_PREFETCH_HEADER, NEXT_ROUTER_SEGMENT_PREFETCH_HEADER,
...@@ -10,6 +13,26 @@ import { ...@@ -10,6 +13,26 @@ import {
} from '../app-router-headers' } from '../app-router-headers'
import type { RequestHeaders } from './fetch-server-response' 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 * 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. * support custom headers. This helps avoid caching conflicts by making each request unique.
...@@ -21,21 +44,17 @@ import type { RequestHeaders } from './fetch-server-response' ...@@ -21,21 +44,17 @@ import type { RequestHeaders } from './fetch-server-response'
* URL before: https://example.com/path?query=1 * URL before: https://example.com/path?query=1
* URL after: https://example.com/path?query=1&_rsc=abc123 * 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 * 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. * headers approach entirely and just use search params.
*/ */
export const setCacheBustingSearchParam = ( export const setCacheBustingSearchParam = async (
url: URL, url: URL,
headers: RequestHeaders headers: RequestHeaders
): void => { ): Promise<void> => {
const uniqueCacheKey = computeCacheBustingSearchParam( const uniqueCacheKey = await computeClientCacheBustingSearchParam(headers)
headers[NEXT_ROUTER_PREFETCH_HEADER],
headers[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER],
headers[NEXT_ROUTER_STATE_TREE_HEADER],
headers[NEXT_URL]
)
setCacheBustingSearchParamWithHash(url, uniqueCacheKey) setCacheBustingSearchParamWithHash(url, uniqueCacheKey)
} }
...@@ -50,7 +69,7 @@ export const setCacheBustingSearchParam = ( ...@@ -50,7 +69,7 @@ export const setCacheBustingSearchParam = (
* hash: "abc123" * hash: "abc123"
* URL after: https://example.com/path?query=1&_rsc=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 * Like this: https://example.com/path?query=1&_rsc
* *
* Note: This function mutates the input URL directly and does not return anything. * Note: This function mutates the input URL directly and does not return anything.
......
...@@ -496,6 +496,7 @@ async function exportAppImpl( ...@@ -496,6 +496,7 @@ async function exportAppImpl(
serverActions: nextConfig.experimental.serverActions, serverActions: nextConfig.experimental.serverActions,
serverComponents: enabledDirectories.app, serverComponents: enabledDirectories.app,
cacheLifeProfiles: nextConfig.cacheLife, cacheLifeProfiles: nextConfig.cacheLife,
staticPageGenerationTimeout: nextConfig.staticPageGenerationTimeout,
nextFontManifest: require( nextFontManifest: require(
join(distDir, 'server', `${NEXT_FONT_MANIFEST}.json`) join(distDir, 'server', `${NEXT_FONT_MANIFEST}.json`)
), ),
...@@ -512,6 +513,7 @@ async function exportAppImpl( ...@@ -512,6 +513,7 @@ async function exportAppImpl(
inlineCss: nextConfig.experimental.inlineCss ?? false, inlineCss: nextConfig.experimental.inlineCss ?? false,
prefetchInlining: nextConfig.experimental.prefetchInlining ?? false, prefetchInlining: nextConfig.experimental.prefetchInlining ?? false,
authInterrupts: !!nextConfig.experimental.authInterrupts, authInterrupts: !!nextConfig.experimental.authInterrupts,
useCacheTimeout: nextConfig.experimental.useCacheTimeout,
cachedNavigations: nextConfig.experimental.cachedNavigations ?? false, cachedNavigations: nextConfig.experimental.cachedNavigations ?? false,
maxPostponedStateSizeBytes: parseMaxPostponedStateSize( maxPostponedStateSizeBytes: parseMaxPostponedStateSize(
nextConfig.experimental.maxPostponedStateSize nextConfig.experimental.maxPostponedStateSize
......
...@@ -48,7 +48,10 @@ export async function exportAppRoute( ...@@ -48,7 +48,10 @@ export async function exportAppRoute(
htmlFilepath: string, htmlFilepath: string,
fileWriter: MultiFileWriter, fileWriter: MultiFileWriter,
cacheComponents: boolean, cacheComponents: boolean,
experimental: Required<Pick<ExperimentalConfig, 'authInterrupts'>>, staticPageGenerationTimeout: number,
experimental: Required<
Pick<ExperimentalConfig, 'authInterrupts' | 'useCacheTimeout'>
>,
buildId: string buildId: string
): Promise<ExportRouteResult> { ): Promise<ExportRouteResult> {
// Ensure that the URL is absolute. // Ensure that the URL is absolute.
...@@ -81,6 +84,7 @@ export async function exportAppRoute( ...@@ -81,6 +84,7 @@ export async function exportAppRoute(
onClose: afterRunner.context.onClose, onClose: afterRunner.context.onClose,
onAfterTaskError: afterRunner.context.onTaskError, onAfterTaskError: afterRunner.context.onTaskError,
cacheLifeProfiles, cacheLifeProfiles,
staticPageGenerationTimeout,
}, },
sharedContext: { sharedContext: {
buildId, buildId,
......
...@@ -257,6 +257,7 @@ async function exportPageImpl( ...@@ -257,6 +257,7 @@ async function exportPageImpl(
htmlFilepath, htmlFilepath,
fileWriter, fileWriter,
commonRenderOpts.cacheComponents, commonRenderOpts.cacheComponents,
commonRenderOpts.staticPageGenerationTimeout,
commonRenderOpts.experimental, commonRenderOpts.experimental,
buildId buildId
) )
......
...@@ -68,11 +68,12 @@ import { workUnitAsyncStorage } from '../app-render/work-unit-async-storage.exte ...@@ -68,11 +68,12 @@ import { workUnitAsyncStorage } from '../app-render/work-unit-async-storage.exte
import { InvariantError } from '../../shared/lib/invariant-error' import { InvariantError } from '../../shared/lib/invariant-error'
import { executeRevalidates } from '../revalidation-utils' import { executeRevalidates } from '../revalidation-utils'
import { addRequestMeta, getRequestMeta } from '../request-meta' 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 { import {
ActionDidNotRevalidate, ActionDidNotRevalidate,
ActionDidRevalidateStaticAndDynamic, ActionDidRevalidateStaticAndDynamic,
} from '../../shared/lib/action-revalidation-kind' } from '../../shared/lib/action-revalidation-kind'
import { computeCacheBustingSearchParam } from '../../shared/lib/router/utils/cache-busting-search-param'
const INLINE_ACTION_PREFIX = '$$RSC_SERVER_ACTION_' const INLINE_ACTION_PREFIX = '$$RSC_SERVER_ACTION_'
...@@ -401,19 +402,15 @@ async function createRedirectRenderResult( ...@@ -401,19 +402,15 @@ async function createRedirectRenderResult(
forwardedHeaders.delete(ACTION_HEADER) forwardedHeaders.delete(ACTION_HEADER)
try { try {
setCacheBustingSearchParam(fetchUrl, { const cacheBustingSearchParam = await computeCacheBustingSearchParam(
[NEXT_ROUTER_PREFETCH_HEADER]: forwardedHeaders.get( forwardedHeaders.get(NEXT_ROUTER_PREFETCH_HEADER)
NEXT_ROUTER_PREFETCH_HEADER
)
? ('1' as const) ? ('1' as const)
: undefined, : undefined,
[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER]: forwardedHeaders.get(NEXT_ROUTER_SEGMENT_PREFETCH_HEADER) ?? undefined,
forwardedHeaders.get(NEXT_ROUTER_SEGMENT_PREFETCH_HEADER) ?? forwardedHeaders.get(NEXT_ROUTER_STATE_TREE_HEADER) ?? undefined,
undefined, forwardedHeaders.get(NEXT_URL) ?? undefined
[NEXT_ROUTER_STATE_TREE_HEADER]: )
forwardedHeaders.get(NEXT_ROUTER_STATE_TREE_HEADER) ?? undefined, setCacheBustingSearchParamWithHash(fetchUrl, cacheBustingSearchParam)
[NEXT_URL]: forwardedHeaders.get(NEXT_URL) ?? undefined,
})
const response = await fetch(fetchUrl, { const response = await fetch(fetchUrl, {
method: 'GET', method: 'GET',
......
...@@ -6814,6 +6814,8 @@ async function validateInstantConfigInBuildWithSample( ...@@ -6814,6 +6814,8 @@ async function validateInstantConfigInBuildWithSample(
route: outerWorkStore.route, route: outerWorkStore.route,
incrementalCache: outerWorkStore.incrementalCache, incrementalCache: outerWorkStore.incrementalCache,
cacheLifeProfiles: outerWorkStore.cacheLifeProfiles, cacheLifeProfiles: outerWorkStore.cacheLifeProfiles,
useCacheTimeout: outerWorkStore.useCacheTimeout,
staticPageGenerationTimeout: outerWorkStore.staticPageGenerationTimeout,
isBuildTimePrerendering: false, isBuildTimePrerendering: false,
fetchCache: outerWorkStore.fetchCache, fetchCache: outerWorkStore.fetchCache,
isOnDemandRevalidate: false, isOnDemandRevalidate: false,
......
...@@ -111,6 +111,7 @@ export interface RenderOptsPartial { ...@@ -111,6 +111,7 @@ export interface RenderOptsPartial {
cacheLifeProfiles?: { cacheLifeProfiles?: {
[profile: string]: import('../use-cache/cache-life').CacheLife [profile: string]: import('../use-cache/cache-life').CacheLife
} }
staticPageGenerationTimeout: number
isOnDemandRevalidate?: boolean isOnDemandRevalidate?: boolean
isPossibleServerAction?: boolean isPossibleServerAction?: boolean
setCacheStatus?: (status: ServerCacheStatus, htmlRequestId: string) => void setCacheStatus?: (status: ServerCacheStatus, htmlRequestId: string) => void
...@@ -165,6 +166,7 @@ export interface RenderOptsPartial { ...@@ -165,6 +166,7 @@ export interface RenderOptsPartial {
inlineCss: boolean inlineCss: boolean
prefetchInlining: PrefetchInliningConfig prefetchInlining: PrefetchInliningConfig
authInterrupts: boolean authInterrupts: boolean
useCacheTimeout: number
cachedNavigations: boolean cachedNavigations: boolean
/** /**
......
...@@ -29,6 +29,8 @@ export interface WorkStore { ...@@ -29,6 +29,8 @@ export interface WorkStore {
readonly incrementalCache?: IncrementalCache readonly incrementalCache?: IncrementalCache
readonly cacheLifeProfiles?: { [profile: string]: CacheLife } readonly cacheLifeProfiles?: { [profile: string]: CacheLife }
readonly useCacheTimeout: number
readonly staticPageGenerationTimeout: number
readonly isOnDemandRevalidate?: boolean readonly isOnDemandRevalidate?: boolean
readonly isBuildTimePrerendering?: boolean readonly isBuildTimePrerendering?: boolean
......
...@@ -23,6 +23,7 @@ export type WorkStoreContext = { ...@@ -23,6 +23,7 @@ export type WorkStoreContext = {
nonce?: string nonce?: string
renderOpts: { renderOpts: {
cacheLifeProfiles?: { [profile: string]: CacheLife } cacheLifeProfiles?: { [profile: string]: CacheLife }
staticPageGenerationTimeout: number
incrementalCache?: IncrementalCache incrementalCache?: IncrementalCache
isOnDemandRevalidate?: boolean isOnDemandRevalidate?: boolean
cacheComponents: boolean cacheComponents: boolean
...@@ -31,7 +32,7 @@ export type WorkStoreContext = { ...@@ -31,7 +32,7 @@ export type WorkStoreContext = {
pendingWaitUntil?: Promise<any> pendingWaitUntil?: Promise<any>
experimental: Pick< experimental: Pick<
RenderOpts['experimental'], RenderOpts['experimental'],
'isRoutePPREnabled' | 'authInterrupts' 'isRoutePPREnabled' | 'authInterrupts' | 'useCacheTimeout'
> >
/** /**
...@@ -121,6 +122,8 @@ export function createWorkStore({ ...@@ -121,6 +122,8 @@ export function createWorkStore({
// so that it can access the fs cache without mocks // so that it can access the fs cache without mocks
renderOpts.incrementalCache || (globalThis as any).__incrementalCache, renderOpts.incrementalCache || (globalThis as any).__incrementalCache,
cacheLifeProfiles: renderOpts.cacheLifeProfiles, cacheLifeProfiles: renderOpts.cacheLifeProfiles,
useCacheTimeout: renderOpts.experimental.useCacheTimeout,
staticPageGenerationTimeout: renderOpts.staticPageGenerationTimeout,
isBuildTimePrerendering: renderOpts.isBuildTimePrerendering, isBuildTimePrerendering: renderOpts.isBuildTimePrerendering,
fetchCache: renderOpts.fetchCache, fetchCache: renderOpts.fetchCache,
isOnDemandRevalidate: renderOpts.isOnDemandRevalidate, isOnDemandRevalidate: renderOpts.isOnDemandRevalidate,
......
...@@ -145,7 +145,6 @@ import { shouldServeStreamingMetadata } from './lib/streaming-metadata' ...@@ -145,7 +145,6 @@ import { shouldServeStreamingMetadata } from './lib/streaming-metadata'
import { decodeQueryPathParameter } from './lib/decode-query-path-parameter' import { decodeQueryPathParameter } from './lib/decode-query-path-parameter'
import { NoFallbackError } from '../shared/lib/no-fallback-error.external' import { NoFallbackError } from '../shared/lib/no-fallback-error.external'
import { fixMojibake } from './lib/fix-mojibake' 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 { setCacheBustingSearchParamWithHash } from '../client/components/router-reducer/set-cache-busting-search-param'
import type { CacheControl } from './lib/cache-control' import type { CacheControl } from './lib/cache-control'
import type { PrerenderedRoute } from '../build/static-paths/types' import type { PrerenderedRoute } from '../build/static-paths/types'
...@@ -157,6 +156,10 @@ import { ...@@ -157,6 +156,10 @@ import {
getPostponedStateExceededErrorMessage, getPostponedStateExceededErrorMessage,
readBodyWithSizeLimit, readBodyWithSizeLimit,
} from './lib/postponed-request-body' } from './lib/postponed-request-body'
import {
computeCacheBustingSearchParam,
computeLegacyCacheBustingSearchParam,
} from '../shared/lib/router/utils/cache-busting-search-param'
export type FindComponentsResult< export type FindComponentsResult<
NextModule extends GenericComponentMod = GenericComponentMod, NextModule extends GenericComponentMod = GenericComponentMod,
...@@ -553,6 +556,7 @@ export default abstract class Server< ...@@ -553,6 +556,7 @@ export default abstract class Server<
distDir: this.distDir, distDir: this.distDir,
serverComponents: this.enabledDirectories.app, serverComponents: this.enabledDirectories.app,
cacheLifeProfiles: this.nextConfig.cacheLife, cacheLifeProfiles: this.nextConfig.cacheLife,
staticPageGenerationTimeout: this.nextConfig.staticPageGenerationTimeout,
enableTainting: this.nextConfig.experimental.taint, enableTainting: this.nextConfig.experimental.taint,
crossOrigin: this.nextConfig.crossOrigin crossOrigin: this.nextConfig.crossOrigin
? this.nextConfig.crossOrigin ? this.nextConfig.crossOrigin
...@@ -576,6 +580,7 @@ export default abstract class Server< ...@@ -576,6 +580,7 @@ export default abstract class Server<
prefetchInlining: prefetchInlining:
this.nextConfig.experimental.prefetchInlining ?? false, this.nextConfig.experimental.prefetchInlining ?? false,
authInterrupts: !!this.nextConfig.experimental.authInterrupts, authInterrupts: !!this.nextConfig.experimental.authInterrupts,
useCacheTimeout: this.nextConfig.experimental.useCacheTimeout,
cachedNavigations: cachedNavigations:
this.nextConfig.experimental.cachedNavigations ?? false, this.nextConfig.experimental.cachedNavigations ?? false,
maxPostponedStateSizeBytes: parseMaxPostponedStateSize( maxPostponedStateSizeBytes: parseMaxPostponedStateSize(
...@@ -2078,7 +2083,7 @@ export default abstract class Server< ...@@ -2078,7 +2083,7 @@ export default abstract class Server<
headers[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER] || headers[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER] ||
getRequestMeta(req, 'segmentPrefetchRSCRequest') getRequestMeta(req, 'segmentPrefetchRSCRequest')
const expectedHash = computeCacheBustingSearchParam( const expectedHash = await computeCacheBustingSearchParam(
routerPrefetch, routerPrefetch,
segmentPrefetchRSCRequest, segmentPrefetchRSCRequest,
headers[NEXT_ROUTER_STATE_TREE_HEADER], headers[NEXT_ROUTER_STATE_TREE_HEADER],
...@@ -2090,10 +2095,24 @@ export default abstract class Server< ...@@ -2090,10 +2095,24 @@ export default abstract class Server<
NEXT_RSC_UNION_QUERY 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. // The hash sent by the client does not match the expected value.
// Redirect to the URL with the correct cache-busting search param. // Redirect to the URL with the correct cache-busting search param.
// This prevents cache poisoning attacks on CDNs that don't respect Vary headers. // 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 // Note: When no headers are present, expectedHash is empty string and client
// must send `_rsc` param, otherwise actualHash is null and hash check fails. // must send `_rsc` param, otherwise actualHash is null and hash check fails.
const url = new URL(req.url || '', 'http://localhost') const url = new URL(req.url || '', 'http://localhost')
......
...@@ -398,6 +398,7 @@ export const experimentalSchema = { ...@@ -398,6 +398,7 @@ export const experimentalSchema = {
serverComponentsHmrCache: z.boolean().optional(), serverComponentsHmrCache: z.boolean().optional(),
authInterrupts: z.boolean().optional(), authInterrupts: z.boolean().optional(),
useCache: z.boolean().optional(), useCache: z.boolean().optional(),
useCacheTimeout: z.number().positive().optional(),
useNodeStreams: z.boolean().optional(), useNodeStreams: z.boolean().optional(),
slowModuleDetection: z slowModuleDetection: z
.object({ .object({
......
...@@ -35,6 +35,8 @@ export type NextConfigComplete = Required<Omit<NextConfig, 'configFile'>> & { ...@@ -35,6 +35,8 @@ export type NextConfigComplete = Required<Omit<NextConfig, 'configFile'>> & {
experimental: ExperimentalConfig & { experimental: ExperimentalConfig & {
// Normalized by config.ts: true and partial objects become resolved objects // Normalized by config.ts: true and partial objects become resolved objects
prefetchInlining?: PrefetchInliningConfig 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` // 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 // since development builds use `{distDir}/dev`. This is used to ensure that the bundler doesn't
...@@ -1014,6 +1016,13 @@ export interface ExperimentalConfig { ...@@ -1014,6 +1016,13 @@ export interface ExperimentalConfig {
*/ */
authInterrupts?: boolean 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. * Enables the use of the `"use cache"` directive.
* @deprecated use top-level `cacheComponents` instead * @deprecated use top-level `cacheComponents` instead
...@@ -1989,6 +1998,7 @@ export interface NextConfigRuntime { ...@@ -1989,6 +1998,7 @@ export interface NextConfigRuntime {
useFileSystemPublicRoutes: NextConfigComplete['useFileSystemPublicRoutes'] useFileSystemPublicRoutes: NextConfigComplete['useFileSystemPublicRoutes']
logging?: NextConfigComplete['logging'] logging?: NextConfigComplete['logging']
adapterPath?: NextConfigComplete['adapterPath'] adapterPath?: NextConfigComplete['adapterPath']
staticPageGenerationTimeout: NextConfigComplete['staticPageGenerationTimeout']
experimental: Pick< experimental: Pick<
NextConfigComplete['experimental'], NextConfigComplete['experimental'],
...@@ -2002,6 +2012,7 @@ export interface NextConfigRuntime { ...@@ -2002,6 +2012,7 @@ export interface NextConfigRuntime {
| 'inlineCss' | 'inlineCss'
| 'prefetchInlining' | 'prefetchInlining'
| 'authInterrupts' | 'authInterrupts'
| 'useCacheTimeout'
| 'clientTraceMetadata' | 'clientTraceMetadata'
| 'clientParamParsingOrigins' | 'clientParamParsingOrigins'
| 'allowedRevalidateHeaderKeys' | 'allowedRevalidateHeaderKeys'
...@@ -2051,64 +2062,63 @@ export function getNextConfigRuntime( ...@@ -2051,64 +2062,63 @@ export function getNextConfigRuntime(
return config return config
} }
let ex = config.experimental const ex = config.experimental
type Requiredish<T> = { type Requiredish<T> = {
[K in keyof Required<T>]: T[K] [K in keyof Required<T>]: T[K]
} }
let experimental = ex const experimental = {
? ({ ppr: ex.ppr,
ppr: ex.ppr, taint: ex.taint,
taint: ex.taint, serverActions: ex.serverActions,
serverActions: ex.serverActions, staleTimes: ex.staleTimes,
staleTimes: ex.staleTimes, dynamicOnHover: ex.dynamicOnHover,
dynamicOnHover: ex.dynamicOnHover, useOffline: ex.useOffline,
useOffline: ex.useOffline, optimisticRouting: ex.optimisticRouting,
optimisticRouting: ex.optimisticRouting, inlineCss: ex.inlineCss,
inlineCss: ex.inlineCss, prefetchInlining: ex.prefetchInlining,
prefetchInlining: ex.prefetchInlining, authInterrupts: ex.authInterrupts,
authInterrupts: ex.authInterrupts, useCacheTimeout: ex.useCacheTimeout,
clientTraceMetadata: ex.clientTraceMetadata, clientTraceMetadata: ex.clientTraceMetadata,
clientParamParsingOrigins: ex.clientParamParsingOrigins, clientParamParsingOrigins: ex.clientParamParsingOrigins,
allowedRevalidateHeaderKeys: ex.allowedRevalidateHeaderKeys, allowedRevalidateHeaderKeys: ex.allowedRevalidateHeaderKeys,
fetchCacheKeyPrefix: ex.fetchCacheKeyPrefix, fetchCacheKeyPrefix: ex.fetchCacheKeyPrefix,
isrFlushToDisk: ex.isrFlushToDisk, isrFlushToDisk: ex.isrFlushToDisk,
optimizeCss: ex.optimizeCss, optimizeCss: ex.optimizeCss,
nextScriptWorkers: ex.nextScriptWorkers, nextScriptWorkers: ex.nextScriptWorkers,
disableOptimizedLoading: ex.disableOptimizedLoading, disableOptimizedLoading: ex.disableOptimizedLoading,
largePageDataBytes: ex.largePageDataBytes, largePageDataBytes: ex.largePageDataBytes,
serverComponentsHmrCache: ex.serverComponentsHmrCache, serverComponentsHmrCache: ex.serverComponentsHmrCache,
caseSensitiveRoutes: ex.caseSensitiveRoutes, caseSensitiveRoutes: ex.caseSensitiveRoutes,
validateRSCRequestHeaders: ex.validateRSCRequestHeaders, validateRSCRequestHeaders: ex.validateRSCRequestHeaders,
sri: ex.sri, sri: ex.sri,
useSkewCookie: ex.useSkewCookie, useSkewCookie: ex.useSkewCookie,
preloadEntriesOnStart: ex.preloadEntriesOnStart, preloadEntriesOnStart: ex.preloadEntriesOnStart,
hideLogsAfterAbort: ex.hideLogsAfterAbort, hideLogsAfterAbort: ex.hideLogsAfterAbort,
removeUncaughtErrorAndRejectionListeners: removeUncaughtErrorAndRejectionListeners:
ex.removeUncaughtErrorAndRejectionListeners, ex.removeUncaughtErrorAndRejectionListeners,
imgOptConcurrency: ex.imgOptConcurrency, imgOptConcurrency: ex.imgOptConcurrency,
imgOptMaxInputPixels: ex.imgOptMaxInputPixels, imgOptMaxInputPixels: ex.imgOptMaxInputPixels,
imgOptSequentialRead: ex.imgOptSequentialRead, imgOptSequentialRead: ex.imgOptSequentialRead,
imgOptSkipMetadata: ex.imgOptSkipMetadata, imgOptSkipMetadata: ex.imgOptSkipMetadata,
imgOptTimeoutInSeconds: ex.imgOptTimeoutInSeconds, imgOptTimeoutInSeconds: ex.imgOptTimeoutInSeconds,
proxyClientMaxBodySize: ex.proxyClientMaxBodySize, proxyClientMaxBodySize: ex.proxyClientMaxBodySize,
proxyTimeout: ex.proxyTimeout, proxyTimeout: ex.proxyTimeout,
testProxy: ex.testProxy, testProxy: ex.testProxy,
runtimeServerDeploymentId: ex.runtimeServerDeploymentId, runtimeServerDeploymentId: ex.runtimeServerDeploymentId,
maxPostponedStateSize: ex.maxPostponedStateSize, maxPostponedStateSize: ex.maxPostponedStateSize,
cachedNavigations: ex.cachedNavigations, cachedNavigations: ex.cachedNavigations,
partialFallbacks: ex.partialFallbacks, partialFallbacks: ex.partialFallbacks,
exposeTestingApiInProductionBuild: ex.exposeTestingApiInProductionBuild, exposeTestingApiInProductionBuild: ex.exposeTestingApiInProductionBuild,
supportsImmutableAssets: ex.supportsImmutableAssets, supportsImmutableAssets: ex.supportsImmutableAssets,
useNodeStreams: ex.useNodeStreams, useNodeStreams: ex.useNodeStreams,
trustHostHeader: ex.trustHostHeader, trustHostHeader: ex.trustHostHeader,
isExperimentalCompile: ex.isExperimentalCompile, isExperimentalCompile: ex.isExperimentalCompile,
} satisfies Requiredish<NextConfigRuntime['experimental']>) } satisfies Requiredish<NextConfigRuntime['experimental']>
: {}
const runtimeConfig: Requiredish<NextConfigRuntime> = {
let runtimeConfig: Requiredish<NextConfigRuntime> = {
deploymentId: config.experimental.runtimeServerDeploymentId deploymentId: config.experimental.runtimeServerDeploymentId
? '' ? ''
: config.deploymentId, : config.deploymentId,
...@@ -2144,6 +2154,7 @@ export function getNextConfigRuntime( ...@@ -2144,6 +2154,7 @@ export function getNextConfigRuntime(
pageExtensions: config.pageExtensions, pageExtensions: config.pageExtensions,
useFileSystemPublicRoutes: config.useFileSystemPublicRoutes, useFileSystemPublicRoutes: config.useFileSystemPublicRoutes,
logging: config.logging, logging: config.logging,
staticPageGenerationTimeout: config.staticPageGenerationTimeout,
experimental, experimental,
} }
......
...@@ -1526,6 +1526,14 @@ function assignDefaultsAndValidate( ...@@ -1526,6 +1526,14 @@ function assignDefaultsAndValidate(
result.distDir = join(result.distDir, 'dev') 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 return result as NextConfigComplete
} }
......
...@@ -819,6 +819,9 @@ export default class DevServer extends Server { ...@@ -819,6 +819,9 @@ export default class DevServer extends Server {
nextConfigOutput: this.nextConfig.output, nextConfigOutput: this.nextConfig.output,
buildId: this.buildId, buildId: this.buildId,
authInterrupts: Boolean(this.nextConfig.experimental.authInterrupts), authInterrupts: Boolean(this.nextConfig.experimental.authInterrupts),
useCacheTimeout: this.nextConfig.experimental.useCacheTimeout,
staticPageGenerationTimeout:
this.nextConfig.staticPageGenerationTimeout,
sriEnabled: Boolean(this.nextConfig.experimental.sri?.algorithm), sriEnabled: Boolean(this.nextConfig.experimental.sri?.algorithm),
}) })
return pathsResult return pathsResult
......
...@@ -58,6 +58,8 @@ export async function loadStaticPaths({ ...@@ -58,6 +58,8 @@ export async function loadStaticPaths({
nextConfigOutput, nextConfigOutput,
buildId, buildId,
authInterrupts, authInterrupts,
useCacheTimeout,
staticPageGenerationTimeout,
sriEnabled, sriEnabled,
}: { }: {
dir: string dir: string
...@@ -81,6 +83,8 @@ export async function loadStaticPaths({ ...@@ -81,6 +83,8 @@ export async function loadStaticPaths({
nextConfigOutput: 'standalone' | 'export' | undefined nextConfigOutput: 'standalone' | 'export' | undefined
buildId: string buildId: string
authInterrupts: boolean authInterrupts: boolean
useCacheTimeout: number
staticPageGenerationTimeout: number
sriEnabled: boolean sriEnabled: boolean
}): Promise<StaticPathsResult> { }): Promise<StaticPathsResult> {
// this needs to be initialized before loadComponents otherwise // this needs to be initialized before loadComponents otherwise
...@@ -151,6 +155,8 @@ export async function loadStaticPaths({ ...@@ -151,6 +155,8 @@ export async function loadStaticPaths({
partialFallbacksEnabled: config.partialFallbacks, partialFallbacksEnabled: config.partialFallbacks,
buildId, buildId,
authInterrupts, authInterrupts,
useCacheTimeout,
staticPageGenerationTimeout,
rootParamKeys, rootParamKeys,
}) })
} else if (!components.getStaticPaths) { } else if (!components.getStaticPaths) {
......
import { getRequestMeta, type NextIncomingMessage } from '../request-meta' import { getRequestMeta, type NextIncomingMessage } from '../request-meta'
const PATCHED_SET_HEADER = Symbol('next.patchSetHeaderWithCookieSupport')
type PatchableResponse = { type PatchableResponse = {
setHeader(key: string, value: string | string[]): PatchableResponse setHeader(key: string, value: string | string[]): PatchableResponse
headersSent?: boolean
[PATCHED_SET_HEADER]?: true
} }
/** /**
...@@ -15,7 +19,16 @@ export function patchSetHeaderWithCookieSupport( ...@@ -15,7 +19,16 @@ export function patchSetHeaderWithCookieSupport(
req: NextIncomingMessage, req: NextIncomingMessage,
res: PatchableResponse res: PatchableResponse
) { ) {
if (res[PATCHED_SET_HEADER]) {
return
}
const setHeader = res.setHeader.bind(res) const setHeader = res.setHeader.bind(res)
Object.defineProperty(res, PATCHED_SET_HEADER, {
value: true,
})
res.setHeader = ( res.setHeader = (
name: string, name: string,
value: string | string[] value: string | string[]
......
...@@ -49,6 +49,7 @@ import { ...@@ -49,6 +49,7 @@ import {
getRequestMeta, getRequestMeta,
type NextIncomingMessage, type NextIncomingMessage,
} from '../request-meta' } from '../request-meta'
import { patchSetHeaderWithCookieSupport } from '../lib/patch-set-header'
import { normalizePagePath } from '../../shared/lib/page-path/normalize-page-path' import { normalizePagePath } from '../../shared/lib/page-path/normalize-page-path'
import { isStaticMetadataRoute } from '../../lib/metadata/is-metadata-route' import { isStaticMetadataRoute } from '../../lib/metadata/is-metadata-route'
import { IncrementalCache } from '../lib/incremental-cache' import { IncrementalCache } from '../lib/incremental-cache'
...@@ -647,6 +648,10 @@ export abstract class RouteModule< ...@@ -647,6 +648,10 @@ export abstract class RouteModule<
// edge runtime handles loading instrumentation at the edge adapter level // edge runtime handles loading instrumentation at the edge adapter level
if (process.env.NEXT_RUNTIME !== 'edge') { if (process.env.NEXT_RUNTIME !== 'edge') {
if (res) {
patchSetHeaderWithCookieSupport(req, res)
}
const { join, relative } = const { join, relative } =
require('node:path') as typeof import('node:path') require('node:path') as typeof import('node:path')
......
...@@ -521,12 +521,11 @@ export function createHeadInsertionTransformStream( ...@@ -521,12 +521,11 @@ export function createHeadInsertionTransformStream(
}) })
} }
function createClientResumeScriptInsertionTransformStream(): TransformStream< async function createClientResumeScriptInsertionTransformStream(): Promise<
Uint8Array, TransformStream<Uint8Array, Uint8Array>
Uint8Array
> { > {
const segmentPath = '/_full' const segmentPath = '/_full'
const cacheBustingHeader = computeCacheBustingSearchParam( const cacheBustingHeader = await computeCacheBustingSearchParam(
'1', // headers[NEXT_ROUTER_PREFETCH_HEADER] '1', // headers[NEXT_ROUTER_PREFETCH_HEADER]
'/_full', // headers[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER] '/_full', // headers[NEXT_ROUTER_SEGMENT_PREFETCH_HEADER]
undefined, // headers[NEXT_ROUTER_STATE_TREE_HEADER] undefined, // headers[NEXT_ROUTER_STATE_TREE_HEADER]
...@@ -586,9 +585,9 @@ function createClientResumeScriptInsertionTransformStream(): TransformStream< ...@@ -586,9 +585,9 @@ function createClientResumeScriptInsertionTransformStream(): TransformStream<
* element inside <head>. Used during instant navigation testing to set * element inside <head>. Used during instant navigation testing to set
* self.__next_instant_test before any async bootstrap scripts execute. * self.__next_instant_test before any async bootstrap scripts execute.
*/ */
export function createInstantTestScriptInsertionTransformStream( export async function createInstantTestScriptInsertionTransformStream(
requestId: string | null requestId: string | null
): TransformStream<Uint8Array, Uint8Array> { ): Promise<TransformStream<Uint8Array, Uint8Array>> {
// Kick off a fetch for the static RSC payload. This is the hydration // 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 // source for the locked static shell — same as the __NEXT_CLIENT_RESUME
// fetch used for fallback routes, but with NEXT_INSTANT_PREFETCH_HEADER // fetch used for fallback routes, but with NEXT_INSTANT_PREFETCH_HEADER
...@@ -598,7 +597,7 @@ export function createInstantTestScriptInsertionTransformStream( ...@@ -598,7 +597,7 @@ export function createInstantTestScriptInsertionTransformStream(
// as the feature flag (truthy = instant test mode). The client processes // as the feature flag (truthy = instant test mode). The client processes
// this as a fallback prerender payload for hydration. // this as a fallback prerender payload for hydration.
const segmentPath = '/_full' const segmentPath = '/_full'
const cacheBustingHeader = computeCacheBustingSearchParam( const cacheBustingHeader = await computeCacheBustingSearchParam(
'1', '1',
segmentPath, segmentPath,
undefined, undefined,
...@@ -1139,7 +1138,7 @@ export async function continueStaticFallbackPrerender( ...@@ -1139,7 +1138,7 @@ export async function continueStaticFallbackPrerender(
// Insert generated tags to head // Insert generated tags to head
createHeadInsertionTransformStream(getServerInsertedHTML), createHeadInsertionTransformStream(getServerInsertedHTML),
// Insert the client resume script into the head // Insert the client resume script into the head
createClientResumeScriptInsertionTransformStream(), await createClientResumeScriptInsertionTransformStream(),
// Transform metadata // Transform metadata
createMetadataTransformStream(getServerInsertedMetadata), createMetadataTransformStream(getServerInsertedMetadata),
// Insert the inlined data (Flight data, form state, etc.) stream into the HTML // Insert the inlined data (Flight data, form state, etc.) stream into the HTML
......
...@@ -38,6 +38,7 @@ import { ...@@ -38,6 +38,7 @@ import {
} from '../app-render/work-unit-async-storage.external' } from '../app-render/work-unit-async-storage.external'
import { import {
applyOwnerStack,
getRuntimeStage, getRuntimeStage,
makeDevtoolsIOAwarePromise, makeDevtoolsIOAwarePromise,
makeHangingPromise, makeHangingPromise,
...@@ -628,6 +629,28 @@ function assertDefaultCacheLife( ...@@ -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( function generateCacheEntryWithCacheContext(
workStore: WorkStore, workStore: WorkStore,
cacheContext: CacheContext, cacheContext: CacheContext,
...@@ -1051,18 +1074,20 @@ async function generateCacheEntryImpl( ...@@ -1051,18 +1074,20 @@ async function generateCacheEntryImpl(
) )
let stream: ReadableStream<Uint8Array> let stream: ReadableStream<Uint8Array>
let devTimeoutSignal: AbortSignal | undefined
let devTimeoutTimer: ReturnType<typeof setTimeout> | undefined
switch (outerWorkUnitStore.type) { switch (outerWorkUnitStore.type) {
case 'prerender-runtime': case 'prerender-runtime':
case 'prerender': case 'prerender':
const timeoutAbortController = new AbortController() const timeoutAbortController = new AbortController()
// If we're prerendering, we give you 50 seconds to fill a cache entry. const timer = setTimeout(
// Otherwise we assume you stalled on hanging input and de-opt. This needs () => {
// to be lower than just the general timeout of 60 seconds. workStore.invalidDynamicUsageError = timeoutError
const timer = setTimeout(() => { timeoutAbortController.abort(timeoutError)
workStore.invalidDynamicUsageError = timeoutError },
timeoutAbortController.abort(timeoutError) getUseCacheFillTimeoutMs(workStore, outerWorkUnitStore.type)
}, 50000) )
const dynamicAccessAbortSignal = const dynamicAccessAbortSignal =
dynamicAccessAsyncStorage.getStore()?.abortController.signal dynamicAccessAsyncStorage.getStore()?.abortController.signal
...@@ -1127,18 +1152,43 @@ async function generateCacheEntryImpl( ...@@ -1127,18 +1152,43 @@ async function generateCacheEntryImpl(
} }
break break
case 'request': case 'request':
// If we're filling caches for a staged render, make sure that // TODO: We should just check if the render is abandonable. This is
// it takes at least a task, so we'll always notice a cache miss between stages. // relevant in restart-on-cache-miss in general, so when we implement that
// // for cached navs, it'll also be needed in prod
// TODO(restart-on-cache-miss): This is suboptimal. if (process.env.__NEXT_DEV_SERVER && outerWorkUnitStore.cacheSignal) {
// Ideally we wouldn't need to restart for microtasky caches, // If we're filling caches for a staged render, make sure that it takes
// but the current logic for omitting short-lived caches only works correctly // at least a task, so we'll always notice a cache miss between stages.
// if we do a second render, so that's the best we can do until we refactor that. //
if ( // TODO(restart-on-cache-miss): This is suboptimal. Ideally we wouldn't
process.env.NODE_ENV === 'development' && // need to restart for microtasky caches, but the current logic for
outerWorkUnitStore.cacheSignal // 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)) 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 // fallthrough
case 'prerender-ppr': case 'prerender-ppr':
...@@ -1153,6 +1203,7 @@ async function generateCacheEntryImpl( ...@@ -1153,6 +1203,7 @@ async function generateCacheEntryImpl(
{ {
environmentName: 'Cache', environmentName: 'Cache',
filterStackFrame, filterStackFrame,
signal: devTimeoutSignal,
temporaryReferences, temporaryReferences,
onError: handleError, onError: handleError,
} }
...@@ -1171,7 +1222,11 @@ async function generateCacheEntryImpl( ...@@ -1171,7 +1222,11 @@ async function generateCacheEntryImpl(
innerCacheStore, innerCacheStore,
startTime, startTime,
errors errors
) ).finally(() => {
if (devTimeoutTimer !== undefined) {
clearTimeout(devTimeoutTimer)
}
})
if (process.env.NODE_ENV === 'development') { if (process.env.NODE_ENV === 'development') {
// Name the stream for React DevTools. // Name the stream for React DevTools.
...@@ -1308,6 +1363,7 @@ export async function cache( ...@@ -1308,6 +1363,7 @@ export async function cache(
const timeoutError = new UseCacheTimeoutError() const timeoutError = new UseCacheTimeoutError()
Error.captureStackTrace(timeoutError, cache) Error.captureStackTrace(timeoutError, cache)
applyOwnerStack(timeoutError)
const wrapAsInvalidDynamicUsageError = ( const wrapAsInvalidDynamicUsageError = (
error: Error, error: Error,
......
...@@ -302,11 +302,21 @@ export async function adapter( ...@@ -302,11 +302,21 @@ export async function adapter(
renderOpts: { renderOpts: {
cacheLifeProfiles: cacheLifeProfiles:
params.request.nextConfig?.experimental?.cacheLife, 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, cacheComponents: false,
experimental: { experimental: {
isRoutePPREnabled: false, isRoutePPREnabled: false,
authInterrupts: authInterrupts:
!!params.request.nextConfig?.experimental?.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, supportsDynamicResponse: true,
waitUntil, waitUntil,
......
...@@ -126,8 +126,16 @@ export class EdgeRouteModuleWrapper { ...@@ -126,8 +126,16 @@ export class EdgeRouteModuleWrapper {
cacheComponents: !!process.env.__NEXT_CACHE_COMPONENTS, cacheComponents: !!process.env.__NEXT_CACHE_COMPONENTS,
experimental: { experimental: {
authInterrupts: !!process.env.__NEXT_EXPERIMENTAL_AUTH_INTERRUPTS, 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, 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: { sharedContext: {
buildId: '', // TODO: Populate this properly. buildId: '', // TODO: Populate this properly.
......
import { hexHash } from '../../hash' 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, prefetchHeader: '1' | '2' | '0' | undefined,
segmentPrefetchHeader: string | string[] | undefined, segmentPrefetchHeader: string | string[] | undefined,
stateTreeHeader: string | string[] | undefined, stateTreeHeader: string | string[] | undefined,
nextUrlHeader: string | string[] | undefined nextUrlHeader: string | string[] | undefined
): string { ): string | null {
if ( if (
(prefetchHeader === undefined || prefetchHeader === '0') && (prefetchHeader === undefined || prefetchHeader === '0') &&
segmentPrefetchHeader === undefined && segmentPrefetchHeader === undefined &&
stateTreeHeader === undefined && stateTreeHeader === undefined &&
nextUrlHeader === 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 ''
} }
return hexHash(
[ return computeCacheBustingSearchParamFromInput(input)
prefetchHeader || '0', }
segmentPrefetchHeader || '0',
stateTreeHeader || '0', export function computeLegacyCacheBustingSearchParam(
nextUrlHeader || '0', prefetchHeader: '1' | '2' | '0' | undefined,
].join(',') 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 @@ ...@@ -27,6 +27,7 @@
}, },
"devDependencies": { "devDependencies": {
"react-refresh": "0.12.0", "react-refresh": "0.12.0",
"typescript": "catalog:",
"webpack": "^4 || ^5" "webpack": "^4 || ^5"
} }
} }
This diff is collapsed.
...@@ -6,6 +6,11 @@ packages: ...@@ -6,6 +6,11 @@ packages:
- 'turbopack/crates/*/js' - 'turbopack/crates/*/js'
- 'turbopack/crates/turbopack-tests/tests/execution' - 'turbopack/crates/turbopack-tests/tests/execution'
- 'turbopack/packages/*' - 'turbopack/packages/*'
catalog:
eslint: 9.37.0
jest: 29.7.0
prettier: 3.6.2
typescript: 6.0.2
updateNotifier: false updateNotifier: false
publicHoistPattern: publicHoistPattern:
- '*eslint*' - '*eslint*'
......
...@@ -223,14 +223,24 @@ describe('app dir - prefetching', () => { ...@@ -223,14 +223,24 @@ describe('app dir - prefetching', () => {
16, // PrefetchHint.IsRootLayout 16, // PrefetchHint.IsRootLayout
]) ])
) )
const response = await next.fetch(`/prefetch-auto/justputit?_rsc=dcqtr`, { const headers = {
headers: { rsc: '1',
rsc: '1', 'next-router-prefetch': '1',
'next-router-prefetch': '1', 'next-router-state-tree': stateTree,
'next-router-state-tree': stateTree, 'next-url': '/prefetch-auto/justputit',
'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() const prefetchResponse = await response.text()
expect(prefetchResponse).not.toContain('Page Data!') expect(prefetchResponse).not.toContain('Page Data!')
...@@ -267,7 +277,7 @@ describe('app dir - prefetching', () => { ...@@ -267,7 +277,7 @@ describe('app dir - prefetching', () => {
} }
const url = new URL('/prefetch-auto/justputit', 'http://localhost') const url = new URL('/prefetch-auto/justputit', 'http://localhost')
const cacheBustingParam = computeCacheBustingSearchParam( const cacheBustingParam = await computeCacheBustingSearchParam(
headers['next-router-prefetch'] ? '1' : '0', headers['next-router-prefetch'] ? '1' : '0',
undefined, undefined,
headers['next-router-state-tree'], headers['next-router-state-tree'],
......
import { nextTestSetup } from 'e2e-utils' 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', () => { describe('app dir - validation', () => {
const { next, skipped } = nextTestSetup({ const { next, skipped } = nextTestSetup({
...@@ -29,13 +32,13 @@ describe('app dir - validation', () => { ...@@ -29,13 +32,13 @@ describe('app dir - validation', () => {
const url2 = new URL('/', 'http://localhost') const url2 = new URL('/', 'http://localhost')
// Add cache busting search param for both requests // Add cache busting search param for both requests
const cacheBustingParam1 = computeCacheBustingSearchParam( const cacheBustingParam1 = await computeCacheBustingSearchParam(
undefined, undefined,
undefined, undefined,
stateTree1, stateTree1,
undefined undefined
) )
const cacheBustingParam2 = computeCacheBustingSearchParam( const cacheBustingParam2 = await computeCacheBustingSearchParam(
undefined, undefined,
undefined, undefined,
stateTree2, stateTree2,
...@@ -55,4 +58,51 @@ describe('app dir - validation', () => { ...@@ -55,4 +58,51 @@ describe('app dir - validation', () => {
const res2 = await next.fetch(url2.toString(), { headers: headers2 }) const res2 = await next.fetch(url2.toString(), { headers: headers2 })
expect(res2.status).toBe(200) 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 { nextTestSetup } from 'e2e-utils'
import { computeCacheBustingSearchParam } from 'next/dist/shared/lib/router/utils/cache-busting-search-param'
import cheerio from 'cheerio' import cheerio from 'cheerio'
import { fetchViaHTTP, findPort } from 'next-test-utils'
describe('cache-components', () => { describe('cache-components', () => {
const { next, isNextDev, skipped } = nextTestSetup({ const { next, isNextDev, isNextStart, skipped } = nextTestSetup({
files: __dirname, files: __dirname,
skipDeployment: true, skipDeployment: true,
}) })
...@@ -349,6 +353,144 @@ describe('cache-components', () => { ...@@ -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 () => { 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', {}) let html = await next.render('/cases/static-rsc-dynamic-client', {})
const $ = cheerio.load(html) const $ = cheerio.load(html)
......
...@@ -47,18 +47,18 @@ const pages: Page[] = [ ...@@ -47,18 +47,18 @@ const pages: Page[] = [
}, },
] ]
const addCacheBustingSearchParam = ( const addCacheBustingSearchParam = async (
pathname: string, pathname: string,
headers: Record<string, string | string[] | undefined> headers: Record<string, string | string[] | undefined>
) => { ) => {
const cacheKey = computeCacheBustingSearchParam( const cacheKey = await computeCacheBustingSearchParam(
headers['next-router-prefetch'] ? '1' : '0', headers['next-router-prefetch'] ? '1' : '0',
headers['next-router-segment-prefetch'], headers['next-router-segment-prefetch'],
headers['next-router-state-tree'], headers['next-router-state-tree'],
headers['next-url'] headers['next-url']
) )
if (cacheKey === null) { if (cacheKey.length === 0) {
return pathname return pathname
} }
...@@ -582,7 +582,7 @@ describe.skip('ppr-full', () => { ...@@ -582,7 +582,7 @@ describe.skip('ppr-full', () => {
rsc: '1', rsc: '1',
'next-router-prefetch': '1', 'next-router-prefetch': '1',
} }
const urlWithCacheBusting = addCacheBustingSearchParam( const urlWithCacheBusting = await addCacheBustingSearchParam(
pathname, pathname,
headers headers
) )
...@@ -619,7 +619,7 @@ describe.skip('ppr-full', () => { ...@@ -619,7 +619,7 @@ describe.skip('ppr-full', () => {
'next-router-prefetch': '1', 'next-router-prefetch': '1',
'X-Test-Input': unexpected, 'X-Test-Input': unexpected,
} }
const urlWithCacheBusting = addCacheBustingSearchParam( const urlWithCacheBusting = await addCacheBustingSearchParam(
pathname, pathname,
headers headers
) )
...@@ -639,7 +639,7 @@ describe.skip('ppr-full', () => { ...@@ -639,7 +639,7 @@ describe.skip('ppr-full', () => {
describe.each(pages)('for $pathname', ({ pathname, dynamic }) => { describe.each(pages)('for $pathname', ({ pathname, dynamic }) => {
it('should have correct headers', async () => { it('should have correct headers', async () => {
const headers = { rsc: '1' } const headers = { rsc: '1' }
const urlWithCacheBusting = addCacheBustingSearchParam( const urlWithCacheBusting = await addCacheBustingSearchParam(
pathname, pathname,
headers headers
) )
...@@ -671,7 +671,7 @@ describe.skip('ppr-full', () => { ...@@ -671,7 +671,7 @@ describe.skip('ppr-full', () => {
rsc: '1', rsc: '1',
'X-Test-Input': expected, 'X-Test-Input': expected,
} }
const urlWithCacheBusting = addCacheBustingSearchParam( const urlWithCacheBusting = await addCacheBustingSearchParam(
pathname, pathname,
headers headers
) )
...@@ -691,7 +691,7 @@ describe.skip('ppr-full', () => { ...@@ -691,7 +691,7 @@ describe.skip('ppr-full', () => {
rsc: '1', rsc: '1',
'X-Test-Input': unexpected, 'X-Test-Input': unexpected,
} }
const urlWithCacheBusting = addCacheBustingSearchParam( const urlWithCacheBusting = await addCacheBustingSearchParam(
pathname, pathname,
headers headers
) )
......
...@@ -30,7 +30,12 @@ describe('resume-data-cache', () => { ...@@ -30,7 +30,12 @@ describe('resume-data-cache', () => {
url.searchParams.set( url.searchParams.set(
'_rsc', '_rsc',
computeCacheBustingSearchParam('1', '/__PAGE__', undefined, undefined) await computeCacheBustingSearchParam(
'1',
'/__PAGE__',
undefined,
undefined
)
) )
const rsc = await next const rsc = await next
......
...@@ -476,7 +476,7 @@ describe('rewrite-headers', () => { ...@@ -476,7 +476,7 @@ describe('rewrite-headers', () => {
// Add cache busting param for RSC requests // Add cache busting param for RSC requests
if (headers.rsc === '1') { if (headers.rsc === '1') {
const cacheBustingParam = computeCacheBustingSearchParam( const cacheBustingParam = await computeCacheBustingSearchParam(
headers['next-router-prefetch'], headers['next-router-prefetch'],
headers['next-router-segment-prefetch'], headers['next-router-segment-prefetch'],
undefined, undefined,
......
...@@ -135,7 +135,7 @@ describe('middleware-static-rewrite', () => { ...@@ -135,7 +135,7 @@ describe('middleware-static-rewrite', () => {
it('should revalidate the overview page without replacing it with a 404', async () => { it('should revalidate the overview page without replacing it with a 404', async () => {
const url = new URL('/my-team', 'http://localhost') const url = new URL('/my-team', 'http://localhost')
const rsc = computeCacheBustingSearchParam( const rsc = await computeCacheBustingSearchParam(
'1', '1',
'/_head', '/_head',
undefined, 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 } ...@@ -44,7 +44,6 @@ either = { workspace = true }
hashbrown = { workspace = true, features = ["raw"] } hashbrown = { workspace = true, features = ["raw"] }
indexmap = { workspace = true } indexmap = { workspace = true }
lzzzz = { workspace = true, optional = true } lzzzz = { workspace = true, optional = true }
once_cell = { workspace = true }
parking_lot = { workspace = true } parking_lot = { workspace = true }
rand = { workspace = true } rand = { workspace = true }
ringmap = { workspace = true, features = ["serde"] } ringmap = { workspace = true, features = ["serde"] }
......
...@@ -13,7 +13,6 @@ anyhow = { workspace = true } ...@@ -13,7 +13,6 @@ anyhow = { workspace = true }
arbitrary = { version = "1.4.1", features = ["derive"] } arbitrary = { version = "1.4.1", features = ["derive"] }
bincode = { workspace = true } bincode = { workspace = true }
libfuzzer-sys = "0.4.9" libfuzzer-sys = "0.4.9"
once_cell = { workspace = true }
serde = { workspace = true } serde = { workspace = true }
tokio = { workspace = true, features = ["full"] } tokio = { workspace = true, features = ["full"] }
turbo-tasks = { workspace = true, features = ["non_operation_vc_strongly_consistent"] } turbo-tasks = { workspace = true, features = ["non_operation_vc_strongly_consistent"] }
......
use std::sync::Arc; use std::sync::{Arc, LazyLock};
use anyhow::Result; use anyhow::Result;
use arbitrary::Arbitrary; use arbitrary::Arbitrary;
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use once_cell::sync::Lazy;
use turbo_tasks::{self, NonLocalValue, State, TaskInput, TurboTasks, Vc, trace::TraceRawVcs}; use turbo_tasks::{self, NonLocalValue, State, TaskInput, TurboTasks, Vc, trace::TraceRawVcs};
use turbo_tasks_malloc::TurboMalloc; use turbo_tasks_malloc::TurboMalloc;
...@@ -81,7 +80,7 @@ impl<'a> Iterator for Iter<'a> { ...@@ -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() tokio::runtime::Builder::new_multi_thread()
.enable_all() .enable_all()
.on_thread_stop(|| { .on_thread_stop(|| {
......
use std::mem::take; use std::{cell::LazyCell, mem::take};
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use once_cell::unsync::Lazy;
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use smallvec::SmallVec; use smallvec::SmallVec;
use turbo_tasks::{ use turbo_tasks::{
...@@ -131,7 +130,7 @@ impl UpdateCellOperation { ...@@ -131,7 +130,7 @@ impl UpdateCellOperation {
#[cfg(feature = "trace_task_dirty")] #[cfg(feature = "trace_task_dirty")]
let has_updated_key_hashes = updated_key_hashes.is_some(); let has_updated_key_hashes = updated_key_hashes.is_some();
let updated_key_hashes_set = updated_key_hashes.map(|updated_key_hashes| { 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. // Collect dependent tasks only when not skipping invalidation.
......
...@@ -559,11 +559,14 @@ impl TurboFn<'_> { ...@@ -559,11 +559,14 @@ impl TurboFn<'_> {
let this = #converted_this; let this = #converted_this;
let persistence = #persistence; let persistence = #persistence;
let mut arg = turbo_tasks::StackDynTaskInputsSlot::new(inputs); let mut arg = turbo_tasks::StackDynTaskInputsSlot::new(inputs);
static TRAIT_METHOD: turbo_tasks::macro_helpers::Lazy<&'static turbo_tasks::TraitMethod> = static TRAIT_METHOD: &'static turbo_tasks::TraitMethod = &#trait_type_ident
turbo_tasks::macro_helpers::Lazy::new(|| #trait_type_ident.get(stringify!(#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( <#output as turbo_tasks::task::TaskOutput>::try_from_raw_vc(
turbo_tasks::trait_call( turbo_tasks::trait_call(
*TRAIT_METHOD, TRAIT_METHOD,
this, this,
&mut arg, &mut arg,
persistence, persistence,
......
...@@ -69,7 +69,6 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -69,7 +69,6 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
let trait_type_ident = get_trait_type_ident(trait_ident); let trait_type_ident = get_trait_type_ident(trait_ident);
let mut dynamic_trait_fns = Vec::new(); let mut dynamic_trait_fns = Vec::new();
let mut trait_methods: Vec<TokenStream2> = 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 default_methods: Vec<TokenStream2> = Vec::new();
let mut native_functions = Vec::new(); let mut native_functions = Vec::new();
let mut items: Vec<TokenStream2> = Vec::with_capacity(raw_items.len()); let mut items: Vec<TokenStream2> = Vec::with_capacity(raw_items.len());
...@@ -223,7 +222,7 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -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 method_name_str = syn::LitStr::new(&ident.to_string(), ident.span());
let index = trait_methods.len() as u8; let index = trait_methods.len() as u8;
trait_methods.push(quote! { trait_methods.push(quote! {
#method_name_str => turbo_tasks::TraitMethod { turbo_tasks::TraitMethod {
trait_type: &#trait_type_ident, trait_type: &#trait_type_ident,
trait_name: stringify!(#trait_ident), trait_name: stringify!(#trait_ident),
method_name: #method_name_str, method_name: #method_name_str,
...@@ -231,7 +230,6 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -231,7 +230,6 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
index: #index, index: #index,
}, },
}); });
method_names.push(quote! { #method_name_str });
default_methods.push(quote! { Some(&#native_function_ident) }); default_methods.push(quote! { Some(&#native_function_ident) });
native_functions.push(quote! { native_functions.push(quote! {
...@@ -261,7 +259,7 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -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 method_name_str = syn::LitStr::new(&ident.to_string(), ident.span());
let index = trait_methods.len() as u8; let index = trait_methods.len() as u8;
trait_methods.push(quote! { trait_methods.push(quote! {
#method_name_str => turbo_tasks::TraitMethod { turbo_tasks::TraitMethod {
trait_type: &#trait_type_ident, trait_type: &#trait_type_ident,
trait_name: stringify!(#trait_ident), trait_name: stringify!(#trait_ident),
method_name: #method_name_str, method_name: #method_name_str,
...@@ -269,7 +267,6 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -269,7 +267,6 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
index: #index, index: #index,
}, },
}); });
method_names.push(quote! { #method_name_str });
default_methods.push(quote! { None }); default_methods.push(quote! { None });
quote! { ; } quote! { ; }
}; };
...@@ -311,7 +308,7 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -311,7 +308,7 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
extended_supertraits.push(quote!(turbo_tasks::debug::ValueDebug)); 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 trait_name = global_name_for_type(quote! { dyn #trait_ident });
let expanded = quote! { let expanded = quote! {
#[must_use] #[must_use]
...@@ -324,23 +321,17 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -324,23 +321,17 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
#(#native_functions)* #(#native_functions)*
turbo_tasks::macro_helpers::turbo_register!( turbo_tasks::macro_helpers::turbo_register!(
Box<dyn #trait_ident> => #trait_type_ident: turbo_tasks::TraitType = { Box<dyn #trait_ident> => #trait_type_ident: turbo_tasks::TraitType =
use turbo_tasks::macro_helpers::{phf, phf::phf_map};
turbo_tasks::TraitType::new::<&'static dyn #trait_ident>( turbo_tasks::TraitType::new::<&'static dyn #trait_ident>(
stringify!(#trait_ident), stringify!(#trait_ident),
#trait_name, #trait_name,
phf_map! { &[#(#trait_methods)*],
#(#trait_methods)*
},
&[#(#method_names),*],
&[#(#default_methods),*] &[#(#default_methods),*]
) )
}
); );
impl turbo_tasks::macro_helpers::TraitVtablePrototype for Box<dyn #trait_ident> { impl turbo_tasks::macro_helpers::TraitVtablePrototype for Box<dyn #trait_ident> {
const LEN: usize = #num_methods; const LEN: usize = #num_methods;
const NAMES: &[&str] = &[#(#method_names),*];
const DEFAULTS: &[Option<&turbo_tasks::macro_helpers::NativeFunction>] = &[#(#default_methods),*]; const DEFAULTS: &[Option<&turbo_tasks::macro_helpers::NativeFunction>] = &[#(#default_methods),*];
} }
...@@ -354,8 +345,8 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -354,8 +345,8 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
// TODO: Remove this Lazy VTableRegistry once trait resolution is fully migrated // TODO: Remove this Lazy VTableRegistry once trait resolution is fully migrated
fn get_impl_vtables() -> &'static turbo_tasks::macro_helpers::VTableRegistry<Self::ValueTrait> { 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>> = static registry: ::std::sync::LazyLock<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))); ::std::sync::LazyLock::new(|| turbo_tasks::macro_helpers::VTableRegistry::new(turbo_tasks::registry::get_trait_type_id(&#trait_type_ident)));
&*registry &*registry
} }
......
...@@ -35,9 +35,7 @@ event-listener = "5.4.0" ...@@ -35,9 +35,7 @@ event-listener = "5.4.0"
futures = { workspace = true } futures = { workspace = true }
indexmap = { workspace = true, features = ["serde"] } indexmap = { workspace = true, features = ["serde"] }
inventory = { workspace = true } inventory = { workspace = true }
once_cell = { workspace = true }
parking_lot = { workspace = true, features = ["serde"]} parking_lot = { workspace = true, features = ["serde"]}
phf = { workspace = true }
pin-project-lite = { workspace = true } pin-project-lite = { workspace = true }
rayon = { workspace = true } rayon = { workspace = true }
regex = { workspace = true } regex = { workspace = true }
......
//! Runtime helpers for [turbo-tasks-macro]. //! Runtime helpers for [turbo-tasks-macro].
use std::sync::LazyLock;
pub use async_trait::async_trait; pub use async_trait::async_trait;
pub use bincode; pub use bincode;
pub use inventory; pub use inventory;
pub use once_cell::sync::{Lazy, OnceCell};
pub use phf;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
pub use shrink_to_fit; pub use shrink_to_fit;
pub use tracing; pub use tracing;
...@@ -27,7 +27,7 @@ pub use crate::{ ...@@ -27,7 +27,7 @@ pub use crate::{
registry::RegistryDef, registry::RegistryDef,
task::function::{into_task_fn, into_task_fn_with_this}, task::function::{into_task_fn, into_task_fn_with_this},
turbo_register, turbo_register,
value_type::{TraitVtablePrototype, build_trait_vtable}, value_type::{TraitVtablePrototype, build_trait_vtable, index_of_method_name},
}; };
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
...@@ -209,18 +209,19 @@ unsafe impl Sync for RawPtr {} ...@@ -209,18 +209,19 @@ unsafe impl Sync for RawPtr {}
unsafe impl Send for RawPtr {} unsafe impl Send for RawPtr {}
// Accumulate all trait impls by trait id // Accumulate all trait impls by trait id
static TRAIT_CAST_FNS: Lazy<FxDashMap<TraitTypeId, Vec<(ValueTypeId, RawPtr)>>> = Lazy::new(|| { static TRAIT_CAST_FNS: LazyLock<FxDashMap<TraitTypeId, Vec<(ValueTypeId, RawPtr)>>> =
let map: FxDashMap<TraitTypeId, Vec<(ValueTypeId, RawPtr)>> = FxDashMap::default(); LazyLock::new(|| {
for CollectableTraitCastFunctions(trait_id_fn, value_id_fn, cast_fn) in let map: FxDashMap<TraitTypeId, Vec<(ValueTypeId, RawPtr)>> = FxDashMap::default();
inventory::iter::<CollectableTraitCastFunctions> for CollectableTraitCastFunctions(trait_id_fn, value_id_fn, cast_fn) in
{ inventory::iter::<CollectableTraitCastFunctions>
map.entry(trait_id_fn()) {
.or_default() map.entry(trait_id_fn())
.value_mut() .or_default()
.push((value_id_fn(), RawPtr(*cast_fn))); .value_mut()
} .push((value_id_fn(), RawPtr(*cast_fn)));
map }
}); map
});
// Holds a raw pointer to a function that can perform a fat pointer cast // Holds a raw pointer to a function that can perform a fat pointer cast
pub struct CollectableTraitCastFunctions( pub struct CollectableTraitCastFunctions(
......
use std::{cell::SyncUnsafeCell, num::NonZeroU16}; use std::{cell::SyncUnsafeCell, num::NonZeroU16, sync::LazyLock};
use anyhow::Error; use anyhow::Error;
use once_cell::sync::Lazy;
use crate::{ use crate::{
TraitType, ValueType, TraitType, ValueType,
...@@ -134,16 +133,16 @@ fn init_registry<T: Registerable>(mut items: Vec<&'static T>) -> Box<[&'static T ...@@ -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 /// Get an item by its ID from a registry slice
#[inline] #[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] registry[*id as usize - 1]
} }
/// Get the ID for a registered item. Forces registry init if needed, which /// Get the ID for a registered item. Forces registry init if needed, which
/// assigns IDs to all items as a side effect. /// assigns IDs to all items as a side effect.
#[inline] #[inline]
fn get_id<T: Registerable>(registry: &Lazy<Box<[&'static T]>>, item: &'static T) -> T::Id { fn get_id<T: Registerable>(registry: &LazyLock<Box<[&'static T]>>, item: &'static T) -> T::Id {
Lazy::force(registry); LazyLock::force(registry);
// SAFETY: The ID write happens-before this read thanks to the fence inside of Lazy // 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 n = unsafe { std::ptr::read(item.ty().id.get()) };
let Some(id) = NonZeroU16::new(n) else { let Some(id) = NonZeroU16::new(n) else {
panic!( panic!(
...@@ -156,7 +155,10 @@ fn get_id<T: Registerable>(registry: &Lazy<Box<[&'static T]>>, item: &'static T) ...@@ -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 /// 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(); let len = registry.len();
if *id as usize <= len { if *id as usize <= len {
None None
...@@ -168,7 +170,7 @@ fn validate_id<T: Registerable>(registry: &Lazy<Box<[&'static T]>>, id: T::Id) - ...@@ -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( init_registry(
inventory::iter::<&'static NativeFunction> inventory::iter::<&'static NativeFunction>
.into_iter() .into_iter()
...@@ -191,7 +193,7 @@ pub fn validate_function_id(id: FunctionId) -> Option<Error> { ...@@ -191,7 +193,7 @@ pub fn validate_function_id(id: FunctionId) -> Option<Error> {
validate_id(&FUNCTIONS, id) 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( let items = init_registry(
inventory::iter::<&'static ValueType> inventory::iter::<&'static ValueType>
.into_iter() .into_iter()
...@@ -222,7 +224,7 @@ pub(crate) fn trait_type_count() -> usize { ...@@ -222,7 +224,7 @@ pub(crate) fn trait_type_count() -> usize {
TRAITS.len() TRAITS.len()
} }
static TRAITS: Lazy<Box<[&'static TraitType]>> = Lazy::new(|| { static TRAITS: LazyLock<Box<[&'static TraitType]>> = LazyLock::new(|| {
init_registry( init_registry(
inventory::iter::<&'static TraitType> inventory::iter::<&'static TraitType>
.into_iter() .into_iter()
......
...@@ -6,14 +6,13 @@ use std::{ ...@@ -6,14 +6,13 @@ use std::{
marker::PhantomData, marker::PhantomData,
panic::{self, AssertUnwindSafe, catch_unwind}, panic::{self, AssertUnwindSafe, catch_unwind},
sync::{ sync::{
Arc, Arc, LazyLock,
atomic::{AtomicUsize, Ordering}, atomic::{AtomicUsize, Ordering},
}, },
thread::{self, Thread, available_parallelism}, thread::{self, Thread, available_parallelism},
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use once_cell::sync::Lazy;
use parking_lot::{Condvar, Mutex}; use parking_lot::{Condvar, Mutex};
use tokio::{runtime::Handle, task::block_in_place}; use tokio::{runtime::Handle, task::block_in_place};
use tracing::{Span, info_span}; use tracing::{Span, info_span};
...@@ -22,7 +21,8 @@ use crate::{TurboTasksApi, manager::try_turbo_tasks, turbo_tasks_scope}; ...@@ -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 /// 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. /// 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 { enum WorkQueueJob {
Job(usize, Box<dyn FnOnce() + Send + 'static>), Job(usize, Box<dyn FnOnce() + Send + 'static>),
......
...@@ -255,8 +255,7 @@ impl TraitMethod { ...@@ -255,8 +255,7 @@ impl TraitMethod {
pub struct TraitType { pub struct TraitType {
pub ty: RegistryType, pub ty: RegistryType,
pub methods: phf::Map<&'static str, TraitMethod>, pub methods: &'static [TraitMethod],
pub method_names: &'static [&'static str],
pub default_methods: &'static [Option<&'static NativeFunction>], pub default_methods: &'static [Option<&'static NativeFunction>],
} }
...@@ -264,8 +263,8 @@ impl Debug for TraitType { ...@@ -264,8 +263,8 @@ impl Debug for TraitType {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut d = f.debug_struct("TraitType"); let mut d = f.debug_struct("TraitType");
d.field("name", &self.ty.name); d.field("name", &self.ty.name);
for (name, method) in self.methods.entries() { for method in self.methods.iter() {
d.field(name, method); d.field(method.method_name, method);
} }
d.finish() d.finish()
} }
...@@ -281,20 +280,22 @@ impl TraitType { ...@@ -281,20 +280,22 @@ impl TraitType {
pub const fn new<T: 'static>( pub const fn new<T: 'static>(
name: &'static str, name: &'static str,
global_name: &'static str, global_name: &'static str,
methods: phf::Map<&'static str, TraitMethod>, methods: &'static [TraitMethod],
method_names: &'static [&'static str],
default_methods: &'static [Option<&'static NativeFunction>], default_methods: &'static [Option<&'static NativeFunction>],
) -> Self { ) -> Self {
Self { Self {
ty: RegistryType::new::<T>(name, global_name), ty: RegistryType::new::<T>(name, global_name),
methods, methods,
method_names,
default_methods, default_methods,
} }
} }
#[cfg(test)]
pub fn get(&self, name: &str) -> &TraitMethod { 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); ...@@ -302,17 +303,20 @@ turbo_registry!("Trait", TraitType);
pub trait TraitVtablePrototype { pub trait TraitVtablePrototype {
const LEN: usize; const LEN: usize;
const NAMES: &'static [&'static str];
const DEFAULTS: &'static [Option<&'static NativeFunction>]; 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; let mut i = 0;
'outer: while i < array.len() { 'outer: while i < methods.len() {
if array[i].len() == name.len() { let entry = methods[i].method_name;
if entry.len() == name.len() {
let mut j = 0; let mut j = 0;
while j < name.len() { 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; i += 1;
continue 'outer; continue 'outer;
} }
...@@ -325,7 +329,10 @@ pub(crate) const fn index_of_name(array: &'static [&'static str], name: &'static ...@@ -325,7 +329,10 @@ pub(crate) const fn index_of_name(array: &'static [&'static str], name: &'static
panic!("Method not found!") 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)], overrides: &[(&'static str, &'static NativeFunction)],
) -> [&'static NativeFunction; LEN] { ) -> [&'static NativeFunction; LEN] {
let mut methods = [&crate::native_function::VTABLE_DEFAULT; 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>( ...@@ -340,7 +347,10 @@ pub const fn build_trait_vtable<B: TraitVtablePrototype, const LEN: usize>(
let mut i = 0; let mut i = 0;
while i < overrides.len() { while i < overrides.len() {
let (name, f) = overrides[i]; 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; i += 1;
} }
methods 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