diff --git a/packages/next/src/build/index.ts b/packages/next/src/build/index.ts index db3202272485..5772315f0f23 100644 --- a/packages/next/src/build/index.ts +++ b/packages/next/src/build/index.ts @@ -2680,9 +2680,12 @@ export default async function build( workerResult.prerenderedRoutes.length > 0 && // in PPR, we create fallback routes with empty params. // If at least one non-empty param exists, it must've been - // generated by `generateStaticParams`. + // generated by `generateStaticParams`. Static metadata files + // use a placeholder path with empty params instead. workerResult.prerenderedRoutes.some( - (route) => !isEmptyParams(route.params) + (route) => + isStaticMetadataFile(route.pathname) || + !isEmptyParams(route.params) ) if (isDynamic && !hasGenerateStaticParams) { if (config.output === 'export') { diff --git a/packages/next/src/client/image-component.tsx b/packages/next/src/client/image-component.tsx index c76554a18d1b..db8414274d5c 100644 --- a/packages/next/src/client/image-component.tsx +++ b/packages/next/src/client/image-component.tsx @@ -4,7 +4,6 @@ import React, { useRef, useEffect, useContext, - useMemo, useState, forwardRef, use, @@ -20,21 +19,14 @@ import type { OnLoadingComplete, PlaceholderValue, } from '../shared/lib/get-img-props' -import type { - ImageConfigComplete, - ImageLoaderProps, -} from '../shared/lib/image-config' -import { imageConfigDefault } from '../shared/lib/image-config' -import { ImageConfigContext } from '../shared/lib/image-config-context.shared-runtime' +import type { ImageLoaderProps } from '../shared/lib/image-config' import { RouterContext } from '../shared/lib/router-context.shared-runtime' +import { useImageConfig } from './use-image-config' // This is replaced by webpack alias import defaultLoader from 'next/dist/shared/lib/image-loader' import { useMergedRef } from './use-merged-ref' -// This is replaced by webpack define plugin -const configEnv = process.env.__NEXT_IMAGE_OPTS as any as ImageConfigComplete - if (typeof window === 'undefined') { ;(globalThis as any).__NEXT_IMAGE_IMPORTED = true } @@ -380,28 +372,7 @@ export const Image = forwardRef( // We're in the app directory if there is no pages router. const isAppRouter = !pagesRouter - const configContext = useContext(ImageConfigContext) - const config = useMemo(() => { - const c = configEnv || configContext || imageConfigDefault - - const allSizes = [...c.deviceSizes, ...c.imageSizes].sort((a, b) => a - b) - const deviceSizes = c.deviceSizes.sort((a, b) => a - b) - const qualities = c.qualities?.sort((a, b) => a - b) - return { - ...c, - allSizes, - deviceSizes, - qualities, - // During the SSR, configEnv (__NEXT_IMAGE_OPTS) does not include - // security sensitive configs like `localPatterns`, which is needed - // during the server render to ensure it's validated. Therefore use - // configContext, which holds the config from the server for validation. - localPatterns: - typeof window === 'undefined' - ? configContext?.localPatterns - : c.localPatterns, - } - }, [configContext]) + const config = useImageConfig() const { onLoad, onLoadingComplete } = props const onLoadRef = useRef(onLoad) diff --git a/packages/next/src/client/legacy/image.tsx b/packages/next/src/client/legacy/image.tsx index 339ca10ee82c..b88e83bb4d2d 100644 --- a/packages/next/src/client/legacy/image.tsx +++ b/packages/next/src/client/legacy/image.tsx @@ -4,23 +4,18 @@ import React, { useRef, useEffect, useCallback, - useContext, - useMemo, useState, type JSX, } from 'react' import * as ReactDOM from 'react-dom' import Head from '../../shared/lib/head' -import { - imageConfigDefault, - VALID_LOADERS, -} from '../../shared/lib/image-config' +import { VALID_LOADERS } from '../../shared/lib/image-config' import type { - ImageConfigComplete, + PreparedImageConfig, LoaderValue, } from '../../shared/lib/image-config' import { useIntersection } from '../use-intersection' -import { ImageConfigContext } from '../../shared/lib/image-config-context.shared-runtime' +import { useImageConfig } from '../use-image-config' import { warnOnce } from '../../shared/lib/utils/warn-once' import { normalizePathTrailingSlash } from '../normalize-trailing-slash' import { findClosestQuality } from '../../shared/lib/find-closest-quality' @@ -31,7 +26,6 @@ function normalizeSrc(src: string): string { } const supportsFloat = typeof ReactDOM.preload === 'function' -const configEnv = process.env.__NEXT_IMAGE_OPTS as any as ImageConfigComplete const loadedImageURLs = new Set() const allImgs = new Map< string, @@ -47,7 +41,7 @@ if (typeof window === 'undefined') { const VALID_LOADING_VALUES = ['lazy', 'eager', undefined] as const type LoadingValue = (typeof VALID_LOADING_VALUES)[number] -type ImageConfig = ImageConfigComplete & { allSizes: number[] } +type ImageConfig = PreparedImageConfig export type ImageLoader = (resolverProps: ImageLoaderProps) => string export type ImageLoaderProps = { @@ -223,7 +217,13 @@ function defaultLoader({ // We use dynamic require because this should only error in development const { hasRemoteMatch } = require('../../shared/lib/match-remote-pattern') as typeof import('../../shared/lib/match-remote-pattern') - if (!hasRemoteMatch(config.domains, config.remotePatterns, parsedSrc)) { + if ( + !hasRemoteMatch( + config.domains ?? [], + config.remotePatterns ?? [], + parsedSrc + ) + ) { throw new Error( `Invalid src prop (${src}) on \`next/image\`, hostname "${parsedSrc.hostname}" is not configured under images in your \`next.config.js\`\n` + `See more info: https://nextjs.org/docs/messages/next-image-unconfigured-host` @@ -701,26 +701,7 @@ export default function Image({ blurDataURL, ...all }: ImageProps) { - const configContext = useContext(ImageConfigContext) - const config: ImageConfig = useMemo(() => { - const c = configEnv || configContext || imageConfigDefault - const allSizes = [...c.deviceSizes, ...c.imageSizes].sort((a, b) => a - b) - const deviceSizes = c.deviceSizes.sort((a, b) => a - b) - const qualities = c.qualities?.sort((a, b) => a - b) - return { - ...c, - allSizes, - deviceSizes, - qualities, // During the SSR, configEnv (__NEXT_IMAGE_OPTS) does not include - // security sensitive configs like `localPatterns`, which is needed - // during the server render to ensure it's validated. Therefore use - // configContext, which holds the config from the server for validation. - localPatterns: - typeof window === 'undefined' - ? configContext?.localPatterns - : c.localPatterns, - } - }, [configContext]) + const config: ImageConfig = useImageConfig() let rest: Partial = all let layout: NonNullable = sizes ? 'responsive' : 'intrinsic' diff --git a/packages/next/src/client/use-image-config.ts b/packages/next/src/client/use-image-config.ts new file mode 100644 index 000000000000..64407312f622 --- /dev/null +++ b/packages/next/src/client/use-image-config.ts @@ -0,0 +1,18 @@ +'use client' + +import { useContext } from 'react' +import { + prepareImageConfig, + type ImageConfigForRendering, +} from '../shared/lib/image-config' +import { ImageConfigContext } from '../shared/lib/image-config-context.shared-runtime' + +// This is replaced by the bundler define plugin. +const configEnv = process.env.__NEXT_IMAGE_OPTS as any as + | ImageConfigForRendering + | undefined + +export function useImageConfig() { + const configContext = useContext(ImageConfigContext) + return prepareImageConfig(configEnv, configContext) +} diff --git a/packages/next/src/server/app-render/app-render.tsx b/packages/next/src/server/app-render/app-render.tsx index 69352981a3d3..45814ccc18fa 100644 --- a/packages/next/src/server/app-render/app-render.tsx +++ b/packages/next/src/server/app-render/app-render.tsx @@ -1128,6 +1128,8 @@ async function generateStagedDynamicFlightRenderResultNode( const staticStageByteLengthDeferred = createPromiseWithResolvers() let runtimePrefetchStream: ReadableStream | undefined + let startRuntimePrefetchRender: (() => Promise) | undefined + // Partial prefetching waits for cacheReady() before starting an embedded // runtime prefetch. Track React's native immediates so that this wait // includes Flight work that can discover more cache reads. Other prefetch @@ -1144,7 +1146,10 @@ async function generateStagedDynamicFlightRenderResultNode( // processing and increases the response payload size. if (prefetchMode === PrefetchingMode.Partial) { // Create a mutable cache that gets filled during the dynamic render. - const prerenderResumeDataCache = createPrerenderResumeDataCache() + const prerenderResumeDataCache = createPrerenderResumeDataCache( + // Prefill the mutable cache from the RDC if available. + requestStore.resumeDataCache ?? undefined + ) requestStore.resumeDataCache = prerenderResumeDataCache const cacheSignal = new CacheSignal(immediateTracker) @@ -1157,20 +1162,16 @@ async function generateStagedDynamicFlightRenderResultNode( // render has filled all caches. const runtimePrefetchTransform = new TransformStream() runtimePrefetchStream = runtimePrefetchTransform.readable - - // Wait for the dynamic render to fill caches, then run the final runtime - // prerender (fire-and-forget — does not block the response). - void cacheSignal - .cacheReady() - .then(() => - spawnRuntimePrefetchWithFilledCaches( - runtimePrefetchTransform.writable, - ctx, - prerenderResumeDataCache, - requestStore, - onError - ) + startRuntimePrefetchRender = async () => { + await cacheSignal.cacheReady() + return spawnRuntimePrefetchWithFilledCaches( + runtimePrefetchTransform.writable, + ctx, + prerenderResumeDataCache, + requestStore, + onError ) + } } const rscPayload = await workUnitAsyncStorage.run( @@ -1242,6 +1243,8 @@ async function generateStagedDynamicFlightRenderResultNode( () => stageController.advanceStage(RenderStage.Dynamic) ) + void startRuntimePrefetchRender?.() + return new FlightRenderResult(flightStream, { fetchMetrics: workStore.fetchMetrics, }) @@ -1649,7 +1652,10 @@ async function generateRuntimePrefetchResult( // We need to share caches between the prospective prerender and the final prerender, // but we're not going to persist this anywhere. - const prerenderResumeDataCache = createPrerenderResumeDataCache() + const prerenderResumeDataCache = createPrerenderResumeDataCache( + // Prefill the mutable cache from the RDC if available. + requestStore.resumeDataCache ?? undefined + ) const mode: RuntimePrerenderMode = isShellPrefetch ? { @@ -2757,20 +2763,8 @@ async function prepareAppPageRender( nextFontManifest, assetPrefix = '', enableTainting, - cacheComponents, - setIsrStatus, } = renderOpts - if (process.env.__NEXT_DEV_SERVER && setIsrStatus && !cacheComponents) { - // Reset the ISR status at start of request. - const { pathname } = new URL(req.url || '/', 'http://n') - setIsrStatus( - pathname, - // Only pages using the Node runtime can use ISR, Edge is always dynamic. - process.env.NEXT_RUNTIME === 'edge' ? false : undefined - ) - } - if ( // The type check here ensures that `req` is correctly typed, and the // environment variable check provides dead code elimination. @@ -3064,9 +3058,11 @@ async function renderAppPage( const { cachedNavigations } = renderOpts.experimental const { isHmrRefresh, + isPrefetchRequest, isRSCRequest, isRuntimePrefetchRequest, isAppShellPrefetchRequest, + isRouteTreePrefetchRequest, } = parsedRequestHeaders const isPossibleActionRequest = ctx.isPossibleServerAction @@ -3106,22 +3102,26 @@ async function renderAppPage( ) const requestStore = createRequestStore() - if ( + const setDevIsrStatus = process.env.__NEXT_DEV_SERVER && setIsrStatus && !cacheComponents && - // Only pages using the Node runtime can use ISR, so we only need to - // update the status for those. - // The type check here ensures that `req` is correctly typed, and the - // environment variable check provides dead code elimination. - process.env.NEXT_RUNTIME !== 'edge' && - isNodeNextRequest(req) - ) { - req.originalRequest.on('end', () => { - const { pathname } = new URL(req.url || '/', 'http://n') - const isStatic = !requestStore.usedDynamic && !workStore.forceDynamic - setIsrStatus(pathname, isStatic) - }) + !isPossibleActionRequest && + !isPrefetchRequest && + !isRuntimePrefetchRequest && + !isAppShellPrefetchRequest && + !isRouteTreePrefetchRequest + ? setIsrStatus + : undefined + + if (setDevIsrStatus) { + if (process.env.NEXT_RUNTIME === 'edge') { + // Edge routes cannot use ISR, so there is no dynamic transition to watch. + setDevIsrStatus(url.pathname, false) + } else { + // The indicator remains pending until the output has finished rendering. + setDevIsrStatus(url.pathname, undefined) + } } // MARK: RSC request @@ -3157,12 +3157,26 @@ async function renderAppPage( ) } else { // MARK: RSC dynamic - return generateDynamicFlightRenderResult( + const result = await generateDynamicFlightRenderResult( req, ctx, - requestStore, - undefined + requestStore ) + if (setDevIsrStatus && process.env.NEXT_RUNTIME !== 'edge') { + result.pipeThrough( + new TransformStream({ + // Only a normally completed output can classify the route. + // Stream errors and cancellation leave the indicator pending. + flush() { + setDevIsrStatus( + url.pathname, + !requestStore.usedDynamic && !workStore.forceDynamic + ) + }, + }) + ) + } + return result } } } @@ -3298,7 +3312,20 @@ async function renderAppPage( } // Create the new render result for the response. - return new RenderResult(stream, options) + const result = new RenderResult(stream, options) + if (setDevIsrStatus && process.env.NEXT_RUNTIME !== 'edge') { + result.pipeThrough( + new TransformStream({ + flush() { + setDevIsrStatus( + url.pathname, + !requestStore.usedDynamic && !workStore.forceDynamic + ) + }, + }) + ) + } + return result } catch (renderError) { // Returning a stream may precede SSR readiness, which finishes success. // Only failures finish here; a finally would seal successful renders early. @@ -4016,6 +4043,8 @@ async function renderToStream( createPromiseWithResolvers() let runtimePrefetchStream: ReadableStream | undefined + let startRuntimePrefetchRender: (() => Promise) | undefined + // Partial prefetching waits for cacheReady() before starting an // embedded runtime prefetch. Track React's native immediates so that // this wait includes Flight work that can discover more cache reads. @@ -4028,12 +4057,13 @@ async function renderToStream( // If the route should runtime-cache its navigation, spawn a runtime // prerender after the resume render fills caches. The result is // embedded in the initial RSC payload so the client can cache - // runtime-prefetchable content during hydration. This is enabled when - // Partial Prefetching is on for the route, either per segment (a - // `prefetch` of 'partial') or globally (the - // `partialPrefetching` config). + // runtime-prefetchable content during hydration. This is enabled for + // Partial Prefetching routes. if (prefetchMode === PrefetchingMode.Partial) { - const prerenderResumeDataCache = createPrerenderResumeDataCache() + const prerenderResumeDataCache = createPrerenderResumeDataCache( + // Prefill the mutable cache from the RDC if available. + requestStore.resumeDataCache ?? undefined + ) requestStore.resumeDataCache = prerenderResumeDataCache const cacheSignal = new CacheSignal(immediateTracker) @@ -4043,17 +4073,16 @@ async function renderToStream( const runtimePrefetchTransform = new TransformStream() runtimePrefetchStream = runtimePrefetchTransform.readable - void cacheSignal - .cacheReady() - .then(() => - spawnRuntimePrefetchWithFilledCaches( - runtimePrefetchTransform.writable, - ctx, - prerenderResumeDataCache, - requestStore, - serverComponentsErrorHandler - ) + startRuntimePrefetchRender = async () => { + await cacheSignal.cacheReady() + return spawnRuntimePrefetchWithFilledCaches( + runtimePrefetchTransform.writable, + ctx, + prerenderResumeDataCache, + requestStore, + serverComponentsErrorHandler ) + } } const RSCPayload = await workUnitAsyncStorage.run( @@ -4134,6 +4163,8 @@ async function renderToStream( () => stageController.advanceStage(RenderStage.Dynamic) ) + void startRuntimePrefetchRender?.() + reactServerResult = new ReactServerResult(flightStream) } else { // MARK: nodeStreams RSC diff --git a/packages/next/src/server/app-render/create-component-tree.tsx b/packages/next/src/server/app-render/create-component-tree.tsx index 8b535880257d..1ff6c0058bed 100644 --- a/packages/next/src/server/app-render/create-component-tree.tsx +++ b/packages/next/src/server/app-render/create-component-tree.tsx @@ -322,7 +322,6 @@ async function createComponentTreeInternal( workStore.dynamicShouldError = true } else if (dynamic === 'force-dynamic') { workStore.forceDynamic = true - if (isPrerendering) { const err = new DynamicServerError( `Page with \`dynamic = "force-dynamic"\` won't be rendered statically.` diff --git a/packages/next/src/server/app-render/dynamic-rendering.ts b/packages/next/src/server/app-render/dynamic-rendering.ts index ccff51f04158..33b681f371b8 100644 --- a/packages/next/src/server/app-render/dynamic-rendering.ts +++ b/packages/next/src/server/app-render/dynamic-rendering.ts @@ -22,6 +22,7 @@ import type { WorkStore } from './work-async-storage.external' import type { + RequestStore, WorkUnitStore, PrerenderStoreLegacy, PrerenderStoreModern, @@ -221,9 +222,7 @@ export function markCurrentScopeAsDynamic( throw err case 'request': - if (process.env.NODE_ENV !== 'production') { - workUnitStore.usedDynamic = true - } + markRequestStoreAsDynamic(workUnitStore) break case 'build-time-generator': break @@ -284,15 +283,19 @@ export function trackDynamicDataInDynamicRender(workUnitStore: WorkUnitStore) { case 'build-time-generator': break case 'request': - if (process.env.NODE_ENV !== 'production') { - workUnitStore.usedDynamic = true - } + markRequestStoreAsDynamic(workUnitStore) break default: workUnitStore satisfies never } } +function markRequestStoreAsDynamic(requestStore: RequestStore): void { + if (process.env.__NEXT_DEV_SERVER && requestStore.phase === 'render') { + requestStore.usedDynamic = true + } +} + function abortOnSynchronousDynamicDataAccess( route: string, expression: string, diff --git a/packages/next/src/server/app-render/work-unit-async-storage.external.ts b/packages/next/src/server/app-render/work-unit-async-storage.external.ts index 30ac46e21f5f..354a5a792364 100644 --- a/packages/next/src/server/app-render/work-unit-async-storage.external.ts +++ b/packages/next/src/server/app-render/work-unit-async-storage.external.ts @@ -117,7 +117,7 @@ export interface RequestStore extends CommonWorkUnitStore { validationSamples?: InstantValidationSamples validationSampleTracking?: InstantValidationSampleTracking | null - // DEV-only + // DEV-only. Whether a dynamic API was used during this render. usedDynamic?: boolean } diff --git a/packages/next/src/server/async-storage/request-store.ts b/packages/next/src/server/async-storage/request-store.ts index e3b7e72300b8..244334165565 100644 --- a/packages/next/src/server/async-storage/request-store.ts +++ b/packages/next/src/server/async-storage/request-store.ts @@ -347,6 +347,7 @@ export function createRequestStore(inputs: RequestStoreInputs): RequestStore { (globalThis as any).__serverComponentsHmrCache, hmrRefreshHash, stagedFallbackParams, + usedDynamic: false, } } diff --git a/packages/next/src/server/lib/router-utils/typegen.ts b/packages/next/src/server/lib/router-utils/typegen.ts index 664532cfd022..a2d37e98bd88 100644 --- a/packages/next/src/server/lib/router-utils/typegen.ts +++ b/packages/next/src/server/lib/router-utils/typegen.ts @@ -448,14 +448,17 @@ export function generateValidatorFile( .filter( (filePath) => filePath.endsWith('.ts') || filePath.endsWith('.tsx') ) - .filter( - // Don't include metadata routes or pages - // (e.g. /manifest.webmanifest) - (filePath) => - type !== 'AppPageConfig' || - filePath.endsWith('page.ts') || - filePath.endsWith('page.tsx') - ) + .filter((filePath) => { + // Metadata files have different exports from pages and route handlers. + if (type === 'AppPageConfig') { + return filePath.endsWith('page.ts') || filePath.endsWith('page.tsx') + } + if (type === 'RouteHandlerConfig') { + // Match custom page extensions too, such as route.api.ts. + return /(?:^|\/)route\.[^/]+$/.test(filePath) + } + return true + }) .map((filePath) => { // Keep the file extension for TypeScript imports to support node16 module resolution const importPath = filePath @@ -689,14 +692,17 @@ export function generateValidatorFileStrict( .filter( (filePath) => filePath.endsWith('.ts') || filePath.endsWith('.tsx') ) - .filter( - // Don't include metadata routes or pages - // (e.g. /manifest.webmanifest) - (filePath) => - type !== 'AppPageConfig' || - filePath.endsWith('page.ts') || - filePath.endsWith('page.tsx') - ) + .filter((filePath) => { + // Metadata files have different exports from pages and route handlers. + if (type === 'AppPageConfig') { + return filePath.endsWith('page.ts') || filePath.endsWith('page.tsx') + } + if (type === 'RouteHandlerConfig') { + // Match custom page extensions too, such as route.api.ts. + return /(?:^|\/)route\.[^/]+$/.test(filePath) + } + return true + }) .map((filePath) => { // Keep the file extension for TypeScript imports to support node16 module resolution const importPath = filePath diff --git a/packages/next/src/server/route-modules/app-page/module.ts b/packages/next/src/server/route-modules/app-page/module.ts index 30e0f197465a..f7a2126ee06f 100644 --- a/packages/next/src/server/route-modules/app-page/module.ts +++ b/packages/next/src/server/route-modules/app-page/module.ts @@ -174,7 +174,11 @@ export class AppPageRouteModule extends RouteModule< context.page, context.query, context.fallbackRouteParams, - context.renderOpts, + // HEAD responses do not consume the render stream, so they cannot + // establish a new status for the dev indicator. + process.env.__NEXT_DEV_SERVER && req.method === 'HEAD' + ? { ...context.renderOpts, setIsrStatus: undefined } + : context.renderOpts, context.serverComponentsHmrCache, context.sharedContext, context.routeMatch diff --git a/packages/next/src/server/route-modules/app-route/module.ts b/packages/next/src/server/route-modules/app-route/module.ts index 84f2db27eed4..e73f984d742a 100644 --- a/packages/next/src/server/route-modules/app-route/module.ts +++ b/packages/next/src/server/route-modules/app-route/module.ts @@ -1502,11 +1502,6 @@ function trackDynamic( throw err case 'request': - if (process.env.NODE_ENV !== 'production') { - // TODO: This is currently not really needed for route handlers, as it - // only controls the ISR status that's shown for pages. - workUnitStore.usedDynamic = true - } break case 'build-time-generator': break diff --git a/packages/next/src/server/use-cache/use-cache-wrapper.ts b/packages/next/src/server/use-cache/use-cache-wrapper.ts index 70ed6e52ea80..ce2798fc7128 100644 --- a/packages/next/src/server/use-cache/use-cache-wrapper.ts +++ b/packages/next/src/server/use-cache/use-cache-wrapper.ts @@ -2886,7 +2886,8 @@ export async function cache( debug?.( logPrefix, 'Resume Data Cache entry not found', - serializedCacheKey + serializedCacheKey, + `(${resumeDataCache.cache.size} entries)` ) if (cacheSignal) { diff --git a/packages/next/src/server/web/spec-extension/revalidate.ts b/packages/next/src/server/web/spec-extension/revalidate.ts index b12cbd75aaa4..c22ae14c198f 100644 --- a/packages/next/src/server/web/spec-extension/revalidate.ts +++ b/packages/next/src/server/web/spec-extension/revalidate.ts @@ -205,14 +205,8 @@ function revalidate( throw err case 'request': - if (process.env.NODE_ENV !== 'production') { - // TODO: This is most likely incorrect. It would lead to the ISR - // status being flipped when revalidating a static page with a server - // action. - workUnitStore.usedDynamic = true - // TODO(restart-on-cache-miss): we should do a sync IO error here in dev - // to match prerender behavior - } + // TODO(restart-on-cache-miss): we should do a sync IO error here in dev + // to match prerender behavior break default: workUnitStore satisfies never diff --git a/packages/next/src/shared/lib/get-img-props.ts b/packages/next/src/shared/lib/get-img-props.ts index ee9f277da020..1f197e0ef6a1 100644 --- a/packages/next/src/shared/lib/get-img-props.ts +++ b/packages/next/src/shared/lib/get-img-props.ts @@ -1,10 +1,9 @@ import { getAssetToken, getDeploymentId } from './deployment-id' import { getImageBlurSvg } from './image-blur-svg' -import { imageConfigDefault } from './image-config' import type { - ImageConfigComplete, ImageLoaderProps, ImageLoaderPropsWithConfig, + PreparedImageConfig, } from './image-config' import type { CSSProperties, JSX } from 'react' @@ -95,10 +94,7 @@ const INVALID_BACKGROUND_SIZE_VALUES = [ undefined, ] type LoadingValue = (typeof VALID_LOADING_VALUES)[number] -type ImageConfig = ImageConfigComplete & { - allSizes: number[] - output?: 'standalone' | 'export' -} +type ImageConfig = PreparedImageConfig export type ImageLoader = (p: ImageLoaderProps) => string @@ -313,7 +309,7 @@ export function getImgProps( }: ImageProps, _state: { defaultLoader: ImageLoaderWithConfig - imgConf: ImageConfigComplete + imgConf: ImageConfig showAltText?: boolean blurComplete?: boolean } @@ -326,17 +322,7 @@ export function getImgProps( fill: boolean } } { - const { imgConf, showAltText, blurComplete, defaultLoader } = _state - let config: ImageConfig - let c = imgConf || imageConfigDefault - if ('allSizes' in c) { - config = c as ImageConfig - } else { - const allSizes = [...c.deviceSizes, ...c.imageSizes].sort((a, b) => a - b) - const deviceSizes = c.deviceSizes.sort((a, b) => a - b) - const qualities = c.qualities?.sort((a, b) => a - b) - config = { ...c, allSizes, deviceSizes, qualities } - } + const { imgConf: config, showAltText, blurComplete, defaultLoader } = _state if (typeof defaultLoader === 'undefined') { throw new Error( diff --git a/packages/next/src/shared/lib/image-config.ts b/packages/next/src/shared/lib/image-config.ts index 112e98d8ed17..d83fcac71f56 100644 --- a/packages/next/src/shared/lib/image-config.ts +++ b/packages/next/src/shared/lib/image-config.ts @@ -146,6 +146,94 @@ export type ImageConfigComplete = { export type ImageConfig = Partial +type RequiredImageConfigForRendering = Pick< + ImageConfigComplete, + | 'deviceSizes' + | 'imageSizes' + | 'loader' + | 'path' + | 'dangerouslyAllowSVG' + | 'unoptimized' +> + +export type ImageConfigForRendering = RequiredImageConfigForRendering & + Partial< + Pick< + ImageConfigComplete, + 'qualities' | 'domains' | 'remotePatterns' | 'localPatterns' + > + > & { + output?: 'standalone' | 'export' + } + +export type PreparedImageConfig = RequiredImageConfigForRendering & { + allSizes: number[] + qualities: ImageConfigComplete['qualities'] + domains: ImageConfigComplete['domains'] | undefined + remotePatterns: ImageConfigComplete['remotePatterns'] | undefined + localPatterns: ImageConfigComplete['localPatterns'] + output: 'standalone' | 'export' | undefined +} + +const missingImageConfig = {} +const preparedImageConfigs = new WeakMap< + object, + WeakMap +>() + +/** + * Prepare owned, sorted image options once for each env/context pair. + * Changed options require new objects because results are cached by identity. + */ +export function prepareImageConfig( + envConfig?: ImageConfigForRendering, + contextConfig?: ImageConfigComplete +): PreparedImageConfig { + const envKey = envConfig ?? missingImageConfig + const contextKey = contextConfig ?? missingImageConfig + let preparedByContext = preparedImageConfigs.get(envKey) + if (preparedByContext) { + const cached = preparedByContext.get(contextKey) + if (cached) { + return cached + } + } + + const source: ImageConfigForRendering = + envConfig || contextConfig || imageConfigDefault + const deviceSizes = [...source.deviceSizes].sort((a, b) => a - b) + const imageSizes = [...source.imageSizes] + const prepared: PreparedImageConfig = { + deviceSizes, + imageSizes, + allSizes: [...deviceSizes, ...imageSizes].sort((a, b) => a - b), + qualities: + source.qualities === undefined + ? undefined + : [...source.qualities].sort((a, b) => a - b), + path: source.path, + loader: source.loader, + dangerouslyAllowSVG: source.dangerouslyAllowSVG, + unoptimized: source.unoptimized, + domains: source.domains, + remotePatterns: source.remotePatterns, + // The inlined browser options supply their own patterns. During SSR the + // context supplies security-sensitive patterns omitted from the inline data. + localPatterns: + typeof window === 'undefined' && contextConfig !== undefined + ? contextConfig.localPatterns + : source.localPatterns, + output: source.output, + } + + if (!preparedByContext) { + preparedByContext = new WeakMap() + preparedImageConfigs.set(envKey, preparedByContext) + } + preparedByContext.set(contextKey, prepared) + return prepared +} + export const imageConfigDefault: ImageConfigComplete = { deviceSizes: [640, 750, 828, 1080, 1200, 1920, 2048, 3840], imageSizes: [32, 48, 64, 96, 128, 256, 384], diff --git a/packages/next/src/shared/lib/image-external-get-image-props.ts b/packages/next/src/shared/lib/image-external-get-image-props.ts index 5fb525ff2a1b..104d8dfe85a3 100644 --- a/packages/next/src/shared/lib/image-external-get-image-props.ts +++ b/packages/next/src/shared/lib/image-external-get-image-props.ts @@ -1,4 +1,7 @@ -import type { ImageConfigComplete } from './image-config' +import { + prepareImageConfig, + type ImageConfigForRendering, +} from './image-config' import type { ImageProps } from './get-img-props' import { getImgProps } from './get-img-props' @@ -9,6 +12,11 @@ import { getImgProps } from './get-img-props' import * as defaultLoaderModule from 'next/dist/shared/lib/image-loader' const defaultLoader = Reflect.get(defaultLoaderModule, 'default') +// The bundler replaces this with fixed options. Public getImageProps has no +// context, so prepare those options once when the module is loaded. +const imageConfig = prepareImageConfig( + process.env.__NEXT_IMAGE_OPTS as any as ImageConfigForRendering | undefined +) /** * For more advanced use cases, you can call `getImageProps()` @@ -20,8 +28,7 @@ const defaultLoader = Reflect.get(defaultLoaderModule, 'default') export function getImageProps(imgProps: ImageProps) { const { props } = getImgProps(imgProps, { defaultLoader, - // This is replaced by webpack define plugin - imgConf: process.env.__NEXT_IMAGE_OPTS as any as ImageConfigComplete, + imgConf: imageConfig, }) // Normally we don't care about undefined props because we pass to JSX, // but this exported function could be used by the end user for anything diff --git a/test/development/dev-indicator/app/app/static-indicator/dynamic/page.tsx b/test/development/dev-indicator/app/app/static-indicator/dynamic/page.tsx index ef6849bce549..61e4f8c8e0b3 100644 --- a/test/development/dev-indicator/app/app/static-indicator/dynamic/page.tsx +++ b/test/development/dev-indicator/app/app/static-indicator/dynamic/page.tsx @@ -1,9 +1,15 @@ import { connection } from 'next/server' import { setTimeout } from 'timers/promises' +import { ActionButton } from '../static/action-button' export default async function Page() { await connection() await setTimeout(100) - return

This is a dynamic app router page.

+ return ( + <> +

This is a dynamic app router page.

+ + + ) } diff --git a/test/development/dev-indicator/app/app/static-indicator/gated-client/client.tsx b/test/development/dev-indicator/app/app/static-indicator/gated-client/client.tsx new file mode 100644 index 000000000000..0cc8f9d8626b --- /dev/null +++ b/test/development/dev-indicator/app/app/static-indicator/gated-client/client.tsx @@ -0,0 +1,19 @@ +'use client' + +import { use } from 'react' +import { unstable_noStore } from 'next/cache' + +let gate: Promise | undefined + +export function ClientDynamic() { + if (typeof window === 'undefined') { + if (!gate) { + const origin = process.env['DEV_INDICATOR_GATE_ORIGIN'] + if (!origin) throw new Error('Missing gate origin') + gate = fetch(`${origin}/__gate/client`, { cache: 'force-cache' }) + } + use(gate) + unstable_noStore() + } + return

Client SSR used noStore.

+} diff --git a/test/development/dev-indicator/app/app/static-indicator/gated-client/page.tsx b/test/development/dev-indicator/app/app/static-indicator/gated-client/page.tsx new file mode 100644 index 000000000000..9e6aced7cb27 --- /dev/null +++ b/test/development/dev-indicator/app/app/static-indicator/gated-client/page.tsx @@ -0,0 +1,10 @@ +import { Suspense } from 'react' +import { ClientDynamic } from './client' + +export default function Page() { + return ( + Loading client gate...

}> + +
+ ) +} diff --git a/test/development/dev-indicator/app/app/static-indicator/gated-static/page.tsx b/test/development/dev-indicator/app/app/static-indicator/gated-static/page.tsx new file mode 100644 index 000000000000..06f820b1737e --- /dev/null +++ b/test/development/dev-indicator/app/app/static-indicator/gated-static/page.tsx @@ -0,0 +1,8 @@ +export default async function Page() { + const origin = process.env['DEV_INDICATOR_GATE_ORIGIN'] + if (!origin) throw new Error('Missing gate origin') + + await fetch(`${origin}/__gate/static`, { cache: 'force-cache' }) + + return

The static gate was released.

+} diff --git a/test/development/dev-indicator/app/app/static-indicator/gated/page.tsx b/test/development/dev-indicator/app/app/static-indicator/gated/page.tsx new file mode 100644 index 000000000000..4dc0b0e19e8e --- /dev/null +++ b/test/development/dev-indicator/app/app/static-indicator/gated/page.tsx @@ -0,0 +1,10 @@ +import { headers } from 'next/headers' + +export default async function Page() { + const host = (await headers()).get('host') + await fetch(`http://${host}/__gate/dynamic`, { + cache: 'force-cache', + }) + + return

The gate was released.

+} diff --git a/test/development/dev-indicator/app/app/static-indicator/static/action-button.tsx b/test/development/dev-indicator/app/app/static-indicator/static/action-button.tsx new file mode 100644 index 000000000000..edb99efac41d --- /dev/null +++ b/test/development/dev-indicator/app/app/static-indicator/static/action-button.tsx @@ -0,0 +1,24 @@ +'use client' + +import { useState, useTransition } from 'react' +import { readRequestHeaders } from './actions' + +export function ActionButton() { + const [result, setResult] = useState('') + const [isPending, startTransition] = useTransition() + + return ( + <> + +

{isPending ? 'Pending action' : result}

+ + ) +} diff --git a/test/development/dev-indicator/app/app/static-indicator/static/actions.ts b/test/development/dev-indicator/app/app/static-indicator/static/actions.ts new file mode 100644 index 000000000000..d5af325553fb --- /dev/null +++ b/test/development/dev-indicator/app/app/static-indicator/static/actions.ts @@ -0,0 +1,8 @@ +'use server' + +import { headers } from 'next/headers' + +export async function readRequestHeaders(): Promise { + await headers() + return 'Action complete' +} diff --git a/test/development/dev-indicator/app/app/static-indicator/static/page.tsx b/test/development/dev-indicator/app/app/static-indicator/static/page.tsx index 58bb18055915..2e193584ea3a 100644 --- a/test/development/dev-indicator/app/app/static-indicator/static/page.tsx +++ b/test/development/dev-indicator/app/app/static-indicator/static/page.tsx @@ -1,3 +1,12 @@ -export default function Page() { - return

This is a static app router page.

+import { connection } from 'next/server' // eslint-disable-line @typescript-eslint/no-unused-vars +import { ActionButton } from './action-button' + +export default async function Page() { + // await connection() + return ( + <> +

This is a static app router page.

+ + + ) } diff --git a/test/development/dev-indicator/app/not-found.tsx b/test/development/dev-indicator/app/not-found.tsx new file mode 100644 index 000000000000..2468908d4b90 --- /dev/null +++ b/test/development/dev-indicator/app/not-found.tsx @@ -0,0 +1,3 @@ +export default function NotFound() { + return
This static page was not found.
+} diff --git a/test/development/dev-indicator/dev-indicator.test.ts b/test/development/dev-indicator/dev-indicator.test.ts index fd3724640dac..6f319cd31955 100644 --- a/test/development/dev-indicator/dev-indicator.test.ts +++ b/test/development/dev-indicator/dev-indicator.test.ts @@ -1,5 +1,5 @@ import { nextTestSetup } from 'e2e-utils' -import { waitForStaticIndicator } from 'next-test-utils' +import { gate, retry, waitForStaticIndicator } from 'next-test-utils' const withCacheComponents = process.env.__NEXT_CACHE_COMPONENTS === 'true' @@ -91,6 +91,48 @@ describe('dev indicator - route type', () => { }) describe('with App Router', () => { + it('classifies a completed static not-found page', async () => { + const path = '/app/no-such-page' + const response = await next.fetch(path) + expect(response.status).toBe(404) + + const browser = await next.browser(path) + const hasCacheComponents = await gate( + (conditions) => conditions.cacheComponents + ) + + expect(await browser.elementByCss('#not-found-page').text()).toBe( + 'This static page was not found.' + ) + await waitForStaticIndicator( + browser, + hasCacheComponents ? undefined : 'Static' + ) + }) + + it.each([ + ['static', 'Static'], + ['dynamic', 'Dynamic'], + ] as const)( + 'preserves the %s route indicator after a Server Action', + async (route, routeType) => { + const browser = await next.browser(`/app/static-indicator/${route}`) + const hasCacheComponents = await gate( + (conditions) => conditions.cacheComponents + ) + const expected = hasCacheComponents ? undefined : routeType + await waitForStaticIndicator(browser, expected) + + await browser.elementByCss('main button').click() + await retry(async () => { + expect(await browser.elementByCss('#action-result').text()).toBe( + 'Action complete' + ) + }) + await waitForStaticIndicator(browser, expected) + } + ) + describe('when loading a dynamic page', () => { if (withCacheComponents) { describe('with Cache Components enabled', () => { @@ -105,6 +147,12 @@ describe('dev indicator - route type', () => { await browser.elementByCss("[href='/pages']").click() + await retry(async () => { + expect(await browser.elementByCss('main > p').text()).toBe( + 'hello world' + ) + }) + await waitForStaticIndicator(browser, 'Static') }) }) @@ -145,6 +193,12 @@ describe('dev indicator - route type', () => { await browser.elementByCss("[href='/pages/gssp']").click() + await retry(async () => { + expect(await browser.elementByCss('main > p').text()).toBe( + 'hello world' + ) + }) + await waitForStaticIndicator(browser, 'Dynamic') }) }) @@ -172,3 +226,230 @@ describe('dev indicator - route type', () => { }) }) }) + +describe('dev indicator after a custom server consumes the request', () => { + const { next, skipped } = nextTestSetup({ + files: __dirname, + startCommand: 'node server.js', + serverReadyPattern: /- Local:/, + skipDeployment: true, + }) + + if (skipped) return + + async function expectPendingRoute( + browser: Awaited>, + hasCacheComponents: boolean + ) { + await browser.locateDevToolsIndicator().click() + await retry(async () => { + const pendingRoute = browser.locator( + 'nextjs-portal .dev-tools-indicator-item[title="Loading..."]' + ) + const routeType = browser.locator( + 'nextjs-portal [data-nextjs-route-type]' + ) + if (hasCacheComponents) { + expect(await pendingRoute.count()).toBe(0) + } else { + expect(await pendingRoute.count()).toBe(1) + expect(await pendingRoute.innerText()).toContain('Route') + } + expect(await routeType.count()).toBe(0) + }) + } + + it('keeps a dynamic document Pending until its output finishes', async () => { + let browser: Awaited> | undefined + const hasCacheComponents = await gate( + (conditions) => conditions.cacheComponents + ) + try { + browser = await next.browser('/app/static-indicator/gated', { + waitUntil: 'commit', + waitHydration: false, + }) + + await retry(async () => { + expect((await next.fetch('/__gate-arrived')).status).toBe(200) + expect(await browser.locator('main > p').textContent()).toBe( + 'Loading...' + ) + }) + await expectPendingRoute(browser, hasCacheComponents) + } finally { + expect((await next.fetch('/__release-gate')).status).toBe(200) + } + + await retry(async () => { + expect(await browser!.elementByCss('#gate-released').text()).toBe( + 'The gate was released.' + ) + }) + if (!hasCacheComponents) { + await retry(async () => { + expect( + await browser! + .locator('nextjs-portal [data-nextjs-route-type]') + .innerText() + ).toContain('Dynamic') + }) + } + }) + + it('keeps a static document Pending until its output finishes', async () => { + let browser: Awaited> | undefined + const hasCacheComponents = await gate( + (conditions) => conditions.cacheComponents + ) + try { + browser = await next.browser('/app/static-indicator/gated-static', { + waitUntil: 'commit', + waitHydration: false, + }) + await retry(async () => { + expect((await next.fetch('/__gate-arrived')).status).toBe(200) + expect(await browser.locator('main > p').textContent()).toBe( + 'Loading...' + ) + }) + await expectPendingRoute(browser, hasCacheComponents) + } finally { + expect((await next.fetch('/__release-gate')).status).toBe(200) + } + + expect(await browser!.elementByCss('#static-gate-released').text()).toBe( + 'The static gate was released.' + ) + if (!hasCacheComponents) { + await retry(async () => { + expect( + await browser! + .locator('nextjs-portal [data-nextjs-route-type]') + .innerText() + ).toContain('Static') + }) + } + }) + + it('waits for Client Component SSR usage before classifying', async () => { + let browser: Awaited> | undefined + const hasCacheComponents = await gate( + (conditions) => conditions.cacheComponents + ) + try { + browser = await next.browser('/app/static-indicator/gated-client', { + waitUntil: 'commit', + waitHydration: false, + }) + await retry(async () => { + expect((await next.fetch('/__gate-arrived')).status).toBe(200) + expect( + await browser.locator('#client-gate-pending').textContent() + ).toBe('Loading client gate...') + expect(await browser.locator('#client-dynamic').count()).toBe(0) + }) + await expectPendingRoute(browser, hasCacheComponents) + } finally { + expect((await next.fetch('/__release-gate')).status).toBe(200) + } + + expect(await browser!.elementByCss('#client-dynamic').text()).toBe( + 'Client SSR used noStore.' + ) + if (!hasCacheComponents) { + await retry(async () => { + expect( + await browser! + .locator('nextjs-portal [data-nextjs-route-type]') + .innerText() + ).toContain('Dynamic') + }) + } + }) + + it('classifies App Router document loads and navigation after request-body consumption', async () => { + const browser = await next.browser('/app/static-indicator/dynamic') + const hasCacheComponents = await gate( + (conditions) => conditions.cacheComponents + ) + await waitForStaticIndicator( + browser, + hasCacheComponents ? undefined : 'Dynamic' + ) + + await browser.elementByCss("[href='/app/static-indicator/static']").click() + await waitForStaticIndicator( + browser, + hasCacheComponents ? undefined : 'Static' + ) + }) + + it('reclassifies an App Router page after HMR and restores Static', async () => { + const browser = await next.browser('/app/static-indicator/static') + const hasCacheComponents = await gate( + (conditions) => conditions.cacheComponents + ) + await waitForStaticIndicator( + browser, + hasCacheComponents ? undefined : 'Static' + ) + + const original = await next.readFile( + 'app/app/static-indicator/static/page.tsx' + ) + await next.patchFile( + 'app/app/static-indicator/static/page.tsx', + original + .replace('// await connection()', 'await connection()') + .replace( + 'This is a static app router page.', + 'This page used connection().' + ) + ) + + try { + await retry(async () => { + expect(await browser.elementByCss('main > p').text()).toBe( + 'This page used connection().' + ) + }) + await waitForStaticIndicator( + browser, + hasCacheComponents ? undefined : 'Dynamic' + ) + } finally { + await next.patchFile('app/app/static-indicator/static/page.tsx', original) + } + + await retry(async () => { + expect(await browser.elementByCss('main > p').text()).toBe( + 'This is a static app router page.' + ) + }) + await waitForStaticIndicator( + browser, + hasCacheComponents ? undefined : 'Static' + ) + + if (!hasCacheComponents) { + await next.patchFile( + 'app/app/static-indicator/static/page.tsx', + `export const dynamic = 'force-dynamic'\n${original.replace('This is a static app router page.', 'This page is forced dynamic.')}` + ) + try { + await retry(async () => { + expect(await browser.elementByCss('main > p').text()).toBe( + 'This page is forced dynamic.' + ) + }) + await waitForStaticIndicator(browser, 'Dynamic') + } finally { + await next.patchFile( + 'app/app/static-indicator/static/page.tsx', + original + ) + } + } + }) +}) diff --git a/test/development/dev-indicator/server.js b/test/development/dev-indicator/server.js new file mode 100644 index 000000000000..9110a843c19d --- /dev/null +++ b/test/development/dev-indicator/server.js @@ -0,0 +1,41 @@ +const http = require('http') +const { once } = require('events') +const next = require('next') + +const app = next({ dev: true, dir: __dirname }) +const handle = app.getRequestHandler() +const port = Number(process.env.PORT || 3000) +let gateResponse + +app.prepare().then(() => { + const server = http.createServer(async (req, res) => { + if (req.url?.startsWith('/__gate/')) { + gateResponse = res + return + } + if (req.url === '/__gate-arrived') { + res.writeHead(gateResponse ? 200 : 425).end() + return + } + if (req.url === '/__release-gate') { + gateResponse?.end('released') + gateResponse = undefined + res.end('released') + return + } + + if (req.method === 'GET' && req.url?.startsWith('/app/static-indicator/')) { + const consumed = once(req, 'end') + req.resume() + await consumed + } + + await handle(req, res) + }) + + server.listen(port, () => { + const address = server.address() + process.env.DEV_INDICATOR_GATE_ORIGIN = `http://localhost:${address.port}` + console.log(`- Local: http://localhost:${address.port}`) + }) +}) diff --git a/test/e2e/app-dir/metadata-static-file-gsp/metadata-static-file-gsp.test.ts b/test/e2e/app-dir/metadata-static-file-gsp/metadata-static-file-gsp.test.ts index c0b67b179deb..21fae81cf04a 100644 --- a/test/e2e/app-dir/metadata-static-file-gsp/metadata-static-file-gsp.test.ts +++ b/test/e2e/app-dir/metadata-static-file-gsp/metadata-static-file-gsp.test.ts @@ -11,5 +11,9 @@ describe('metadata-static-file-gsp', () => { it('should build and serve a static metadata file colocated with generateStaticParams', async () => { await next.render('/results/two') + + const response = await next.fetch('/results/-/opengraph-image.png') + expect(response.status).toBe(200) + expect(response.headers.get('content-type')).toBe('image/png') }) }) diff --git a/test/e2e/app-dir/resume-data-cache/app/fetch-cache/initial/page.tsx b/test/e2e/app-dir/resume-data-cache/app/fetch-cache/initial/page.tsx new file mode 100644 index 000000000000..9958246e4ef5 --- /dev/null +++ b/test/e2e/app-dir/resume-data-cache/app/fetch-cache/initial/page.tsx @@ -0,0 +1,34 @@ +import { Suspense } from 'react' +import { connection } from 'next/server' +import { fetchRandomWithForceCache } from '../../../utils' + +async function DynamicComponent() { + await connection() + return null +} + +const KEY = 'test-fetch-cache-initial' + +async function getRandomNumber() { + const res = await fetchRandomWithForceCache({ + key: KEY, + // no tag - this entry is not meant to be revalidated + tag: undefined, + }) + const dateHeader = res.headers.get('date') + const timestamp = dateHeader ? new Date(dateHeader).getTime() : 'time_unknown' + const text = await res.text() + return `fetch-random-${timestamp}-${text}` +} + +export default async function Page() { + const randomNumber = await getRandomNumber() + return ( +
+

{randomNumber}

+ + + +
+ ) +} diff --git a/test/e2e/app-dir/resume-data-cache/app/fetch-cache/revalidation/page.tsx b/test/e2e/app-dir/resume-data-cache/app/fetch-cache/revalidation/page.tsx new file mode 100644 index 000000000000..902c66840cb2 --- /dev/null +++ b/test/e2e/app-dir/resume-data-cache/app/fetch-cache/revalidation/page.tsx @@ -0,0 +1,30 @@ +import { Suspense } from 'react' +import { connection } from 'next/server' +import { fetchRandomWithForceCache } from '../../../utils' + +async function DynamicComponent() { + await connection() + return null +} + +const CACHE_TAG = 'test-fetch-cache-revalidation' + +async function getRandomNumber() { + const res = await fetchRandomWithForceCache({ tag: CACHE_TAG }) + const dateHeader = res.headers.get('date') + const timestamp = dateHeader ? new Date(dateHeader).getTime() : 'time_unknown' + const text = await res.text() + return `fetch-random-${timestamp}-${text}` +} + +export default async function Page() { + const randomNumber = await getRandomNumber() + return ( +
+

{randomNumber}

+ + + +
+ ) +} diff --git a/test/e2e/app-dir/resume-data-cache/app/next-phase.ts b/test/e2e/app-dir/resume-data-cache/app/next-phase.ts new file mode 100644 index 000000000000..d56e262567ae --- /dev/null +++ b/test/e2e/app-dir/resume-data-cache/app/next-phase.ts @@ -0,0 +1,7 @@ +const { PHASE_PRODUCTION_BUILD } = require('next/constants') + +export function getPhase() { + return process.env.NEXT_PHASE === PHASE_PRODUCTION_BUILD + ? 'buildtime' + : 'runtime' +} diff --git a/test/e2e/app-dir/resume-data-cache/app/revalidate-action/page.tsx b/test/e2e/app-dir/resume-data-cache/app/revalidate-action/page.tsx index 6ea5230e7290..0603b4ff9042 100644 --- a/test/e2e/app-dir/resume-data-cache/app/revalidate-action/page.tsx +++ b/test/e2e/app-dir/resume-data-cache/app/revalidate-action/page.tsx @@ -2,11 +2,13 @@ import { Suspense } from 'react' import { connection } from 'next/server' import { cacheTag } from 'next/cache' import { RevalidateButton } from './revalidate-button' +import { tasky } from '../../utils' async function getCachedRandom() { 'use cache' cacheTag('revalidate-action-test') - return Math.random() + await tasky() + return `cache-random-${Math.random()}` } async function DynamicContent() { diff --git a/test/e2e/app-dir/resume-data-cache/app/revalidate-fetch-action/page.tsx b/test/e2e/app-dir/resume-data-cache/app/revalidate-fetch-action/page.tsx index 43a1b6977e86..2f676317b6e4 100644 --- a/test/e2e/app-dir/resume-data-cache/app/revalidate-fetch-action/page.tsx +++ b/test/e2e/app-dir/resume-data-cache/app/revalidate-fetch-action/page.tsx @@ -1,6 +1,7 @@ import { Suspense } from 'react' import { connection } from 'next/server' import { RevalidateButton } from './revalidate-button' +import { fetchRandomWithForceCache } from '../../utils' async function DynamicContent() { // Make the page dynamic/PPR by accessing connection() @@ -12,10 +13,9 @@ async function DynamicContent() { export default async function Page() { // Use fetch with cache and tags - const cachedValue = await fetch( - 'https://next-data-api-endpoint.vercel.app/api/random', - { cache: 'force-cache', next: { tags: ['revalidate-fetch-action-test'] } } - ).then((res) => res.text()) + const cachedValue = await fetchRandomWithForceCache({ + tag: 'revalidate-fetch-action-test', + }).then(async (res) => `fetch-random-${await res.text()}`) return (
diff --git a/test/e2e/app-dir/resume-data-cache/app/revalidate/route.ts b/test/e2e/app-dir/resume-data-cache/app/revalidate/route.ts index 5457a2ec26a2..d94eafab431b 100644 --- a/test/e2e/app-dir/resume-data-cache/app/revalidate/route.ts +++ b/test/e2e/app-dir/resume-data-cache/app/revalidate/route.ts @@ -1,6 +1,13 @@ import { revalidateTag } from 'next/cache' -export function POST() { - revalidateTag('test', 'minutes') +export function POST(req: Request) { + const tag = new URL(req.url).searchParams.get('tag') + if (!tag) { + return new Response('Expected "tag" search param to be set', { + status: 400, + }) + } + + revalidateTag(tag, 'minutes') return new Response(null, { status: 200 }) } diff --git a/test/e2e/app-dir/resume-data-cache/app/server-action/page.tsx b/test/e2e/app-dir/resume-data-cache/app/server-action/page.tsx index 54a566219497..1d93a734db2e 100644 --- a/test/e2e/app-dir/resume-data-cache/app/server-action/page.tsx +++ b/test/e2e/app-dir/resume-data-cache/app/server-action/page.tsx @@ -8,7 +8,7 @@ async function getCachedRandomNumber() { await new Promise((resolve) => setTimeout(resolve, 100)) - return Math.random() + return `cache-random-${Math.random()}` } async function getUncachedRandomNumber() { diff --git a/test/e2e/app-dir/resume-data-cache/app/use-cache/initial/page.tsx b/test/e2e/app-dir/resume-data-cache/app/use-cache/initial/page.tsx new file mode 100644 index 000000000000..998ed1753e62 --- /dev/null +++ b/test/e2e/app-dir/resume-data-cache/app/use-cache/initial/page.tsx @@ -0,0 +1,29 @@ +import React, { Suspense } from 'react' +import { connection } from 'next/server' + +import { getPhase } from '../../next-phase' +import { tasky } from '../../../utils' + +async function getRandomNumber() { + 'use cache' + // no cacheTag - this cache is not meant to be revalidated + await tasky() + return `cache-random-${getPhase()}-${Date.now()}-${Math.random()}` +} + +async function DynamicComponent() { + await connection() + return null +} + +export default async function Page() { + const randomNumber = await getRandomNumber() + return ( +
+

{randomNumber}

+ + + +
+ ) +} diff --git a/test/e2e/app-dir/resume-data-cache/app/page.tsx b/test/e2e/app-dir/resume-data-cache/app/use-cache/revalidation/page.tsx similarity index 60% rename from test/e2e/app-dir/resume-data-cache/app/page.tsx rename to test/e2e/app-dir/resume-data-cache/app/use-cache/revalidation/page.tsx index efe4d038cb1d..ec211671edcd 100644 --- a/test/e2e/app-dir/resume-data-cache/app/page.tsx +++ b/test/e2e/app-dir/resume-data-cache/app/use-cache/revalidation/page.tsx @@ -2,11 +2,16 @@ import React, { Suspense } from 'react' import { connection } from 'next/server' import { cacheTag } from 'next/cache' +import { getPhase } from '../../next-phase' +import { tasky } from '../../../utils' + +const CACHE_TAG = 'test-use-cache-revalidation' async function getRandomNumber() { 'use cache' - cacheTag('test') - return Math.random() + cacheTag(CACHE_TAG) + await tasky() + return `cache-random-${getPhase()}-${Date.now()}-${Math.random()}` } async function DynamicComponent() { @@ -16,17 +21,12 @@ async function DynamicComponent() { export default async function Page() { const randomNumber = await getRandomNumber() - const anotherRandomNumber = await fetch( - 'https://next-data-api-endpoint.vercel.app/api/random', - { cache: 'force-cache', next: { tags: ['test'] } } - ).then((res) => res.text()) return ( - <> +

{randomNumber}

-

{anotherRandomNumber}

- +
) } diff --git a/test/e2e/app-dir/resume-data-cache/resume-data-cache.partial-prefetching.test.ts b/test/e2e/app-dir/resume-data-cache/resume-data-cache.partial-prefetching.test.ts new file mode 100644 index 000000000000..a787db790e39 --- /dev/null +++ b/test/e2e/app-dir/resume-data-cache/resume-data-cache.partial-prefetching.test.ts @@ -0,0 +1,2 @@ +process.env.__NEXT_PARTIAL_PREFETCHING = 'true' +require('./resume-data-cache.test') diff --git a/test/e2e/app-dir/resume-data-cache/resume-data-cache.test.ts b/test/e2e/app-dir/resume-data-cache/resume-data-cache.test.ts index 1ab29e2de737..8a349c56436b 100644 --- a/test/e2e/app-dir/resume-data-cache/resume-data-cache.test.ts +++ b/test/e2e/app-dir/resume-data-cache/resume-data-cache.test.ts @@ -1,144 +1,191 @@ -import { nextTestSetup } from 'e2e-utils' -import { retry } from 'next-test-utils' +import { NextInstance, nextTestSetup } from 'e2e-utils' +import { assertNoConsoleErrors, gate, retry } from 'next-test-utils' import { computeCacheBustingSearchParam } from 'next/dist/shared/lib/router/utils/cache-busting-search-param' +import cheerio from 'cheerio' +// RDC and resume renders do not function in the same way in dev. +// This test suite asserts on production behavior. +// @force-gate !dev describe('resume-data-cache', () => { - const { next, isNextDev } = nextTestSetup({ + const { next } = nextTestSetup({ files: __dirname, }) - if (isNextDev) { - it('is skipped', () => {}) - return - } - - it.each([ - { name: 'use cache', id: 'random-number' }, - { name: 'fetch cache', id: 'another-random-number' }, - ])( - 'should have consistent data between static and dynamic renders with $name', - async ({ id }) => { - // First render the page statically, getting the random number from the - // HTML. - let $ = await next.render$('/') - const first = $(`p#${id}`).text() - - // Then get the Prefetch RSC and validate that it also contains the same - // random number. - await retry(async () => { - const url = new URL('/', 'http://localhost') - - url.searchParams.set( - '_rsc', - await computeCacheBustingSearchParam( - '1', - '/__PAGE__', - undefined, - undefined - ) - ) - - const rsc = await next - .fetch(url.toString(), { - headers: { - RSC: '1', - 'Next-Router-Prefetch': '1', - 'Next-Router-Segment-Prefetch': '/__PAGE__', - }, - }) - .then((res) => res.text()) - expect(rsc).toContain(first) - }) + describe('should have consistent data between static and dynamic renders', () => { + type TestCase = { + name: string + path: string + valuePattern: RegExp + } - // Then get the dynamic RSC and validate that it also contains the same - // random number. - await retry(async () => { - const rsc = await next - .fetch('/', { - headers: { - RSC: '1', - }, - }) - .then((res) => res.text()) - expect(rsc).toContain(first) - }) + const htmlId = 'random-number' + + const testInitial = async ({ name, path, valuePattern }: TestCase) => { + // First, extract the random number from the prerender. + const initialRSC = await fetchStaticPageSegment(next, path).then((res) => + res.text() + ) + const initialValue = getValueFromText(initialRSC, valuePattern) + console.log(`${name} - value from prerender: ${initialValue}`) + + // Dynamic HTML should use the same value. + const html = await next.render(path) + const valueFromHTML = cheerio.load(html)(`p#${htmlId}`).text() + expect(valueFromHTML).toBe(initialValue) + // The value should be consistent across shell and resume. + expectConsistentCacheValue(html, valuePattern, initialValue) + + // Dynamic RSC should use the same value. + const rsc = await fetchRSC(next, path).then((res) => res.text()) + expect(rsc).toContain(initialValue) + // We should not have multiple values for the cache in RSC. + expectConsistentCacheValue(rsc, valuePattern, initialValue) + } - // Then revalidate the page. Note: Dynamic RSC requests don't trigger + const testRevalidation = async ({ + name, + path, + cacheTag, + valuePattern, + }: TestCase & { cacheTag: string }) => { + const initialValue = await getPrerenderedValueFromPageSegment( + next, + path, + valuePattern + ) + console.log(`${name} - initial value: ${initialValue}`) + + // Then revalidate the cached data. Note: Dynamic RSC requests don't trigger // actual revalidation - they only mark tags as needing revalidation. // The actual revalidation only occurs when accessing a static resource again. - await next.fetch('/revalidate', { method: 'POST' }) + await next.fetch(`/revalidate?tag=${cacheTag}`, { method: 'POST' }) // Then get the dynamic RSC again and validate that it still contains the // same random number. The first request will get the stale data, but the // second request will get the fresh data as it'll eventually have // revalidated. - const rsc = await next - .fetch('/', { - headers: { - RSC: '1', - }, + const rsc = await fetchRSC(next, path).then((res) => res.text()) + // TODO: this seems to be race-y in deploy + if (!(await gate('deploy'))) { + expect(rsc).toContain(initialValue) + } + + // In deploy mode, revalidation seems to take a while to propagate + const POST_REVALIDATION_RETRY = 10_000 + + // When the revalidation prerender completes, segment prefetches should start + // containing a new value. + const revalidatedValue = await retry( + async () => { + const rsc = await fetchStaticPageSegment(next, path).then((res) => + res.text() + ) + expect(rsc).not.toContain(initialValue) + return getValueFromText(rsc, valuePattern) + }, + POST_REVALIDATION_RETRY, + undefined, + `${name} - segment prefetch should change` + ) + console.log(`${name} - revalidated value: ${revalidatedValue}`) + + // Dynamic HTML should use the revalidated value. + await retry( + async () => { + const html = await next.render(path) + const valueFromHTML = cheerio.load(html)(`p#${htmlId}`).text() + expect(valueFromHTML).toBe(revalidatedValue) + // The value should be consistent across shell and resume. + expectConsistentCacheValue(html, valuePattern, revalidatedValue) + }, + POST_REVALIDATION_RETRY, + undefined, + `${name} - dynamic HTML should contain the revalidated cache value` + ) + + // Dynamic RSC requests should also use the revalidated value. + await retry( + async () => { + const rsc = await fetchRSC(next, path).then((res) => res.text()) + expect(rsc).toContain(revalidatedValue) + // We should not have multiple values for the cache in RSC. + expectConsistentCacheValue(rsc, valuePattern, revalidatedValue) + }, + POST_REVALIDATION_RETRY, + undefined, + `${name} - dynamic RSC should contain the revalidated cache value` + ) + } + + describe('use cache', () => { + const valuePattern = /cache-random-.+?-\d+-\d+\.\d+/ + + it('initial', async () => { + await testInitial({ + name: 'use cache - initial', + path: '/use-cache/initial', + valuePattern, }) - .then((res) => res.text()) - expect(rsc).toContain(first) - - // We then expect after the background revalidation has been completed, - // the dynamic RSC to get the fresh data. - await retry(async () => { - const rsc = await next - .fetch('/', { - headers: { - RSC: '1', - }, - }) - .then((res) => res.text()) - expect(rsc).not.toContain(first) }) - // This proves that the dynamic RSC was able to use the resume data cache - // (RDC) from the static render to ensure that the data is consistent - // between the static and dynamic renders. Let's now try to render the - // page statically and see that the random number changes. - - $ = await next.render$('/') - const random2 = $(`p#${id}`).text() - expect(random2).not.toBe(first) - - // Then get the Prefetch RSC and validate that it also contains the new - // random number. - await retry(async () => { - const rsc = await next - .fetch('/', { - headers: { - RSC: '1', - 'Next-Router-Prefetch': '1', - 'Next-Router-Segment-Prefetch': '/__PAGE__', - }, - }) - .then((res) => res.text()) - expect(rsc).toContain(random2) + // TODO: propagation of RDC after revalidation seems very flaky in deploy mode, + // so we're skipping this test in deploy until we figure out why + // @force-gate !deploy + it('after revalidation', async () => { + await testRevalidation({ + name: 'use cache - revalidation', + path: '/use-cache/revalidation', + cacheTag: 'test-use-cache-revalidation', + valuePattern, + }) }) + }) + + describe('fetch cache', () => { + const valuePattern = /fetch-random-\d+-\d+\.\d+/ - // Then get the dynamic RSC again and validate that it also contains the - // new random number. - await retry(async () => { - const rsc = await next - .fetch('/', { - headers: { - RSC: '1', - }, - }) - .then((res) => res.text()) - expect(rsc).toContain(random2) + it('initial', async () => { + await testInitial({ + name: 'fetch cache - initial', + path: '/fetch-cache/initial', + valuePattern, + }) }) - // This proves that the dynamic RSC was able to use the resume data cache - // (RDC) from the static render to ensure that the data is consistent - // between the static and dynamic renders. - } - ) + // TODO: propagation of RDC after revalidation seems very flaky in deploy mode, + // so we're skipping this test in deploy until we figure out why + // @force-gate !deploy + it('after revalidation', async () => { + await testRevalidation({ + name: 'fetch cache - revalidation', + path: '/fetch-cache/revalidation', + cacheTag: 'test-fetch-cache-revalidation', + valuePattern, + }) + }) + }) + }) it('should use RDC for server action re-renders', async () => { - const browser = await next.browser('/server-action') + const url = '/server-action' + const valuePattern = /cache-random-\d+\.\d+/ + + const prerenderedCachedValue = await getPrerenderedValueFromPageSegment( + next, + url, + valuePattern + ) + + // The values of the cache entry should be consistent across shell/resume. + expectConsistentCacheValue( + await next.render(url), + valuePattern, + prerenderedCachedValue + ) + + const browser = await next.browser(url, { + pushErrorAsConsoleLog: true, + }) // Get the initial values const initialCachedValue = await browser @@ -148,6 +195,12 @@ describe('resume-data-cache', () => { .elementByCss('#uncached-random') .text() + expect(initialCachedValue).toBe(prerenderedCachedValue) + + // There should be no hydration errors due to a buildtime cache being + // replaced by a new runtime cache + await assertNoConsoleErrors(browser) + await browser.elementByCss('#refresh-button').click() // Wait for the action to complete and verify: @@ -174,8 +227,25 @@ describe('resume-data-cache', () => { // This test verifies that when a server action calls updateTag(), // the subsequent re-render sees fresh data instead of stale RDC data. // This is the "read your own writes" behavior for 'use cache'. - - const browser = await next.browser('/revalidate-action') + const url = '/revalidate-action' + const valuePattern = /cache-random-\d+\.\d+/ + + const prerenderedCachedValue = await getPrerenderedValueFromPageSegment( + next, + url, + valuePattern + ) + + // The values of the cache entry should be consistent across shell/resume. + expectConsistentCacheValue( + await next.render(url), + valuePattern, + prerenderedCachedValue + ) + + const browser = await next.browser(url, { + pushErrorAsConsoleLog: true, + }) // Get the initial cached value from the page render const initialCachedValue = await browser @@ -185,6 +255,12 @@ describe('resume-data-cache', () => { .elementByCss('#uncached-value') .text() + expect(initialCachedValue).toBe(prerenderedCachedValue) + + // There should be no hydration errors due to a buildtime cache being + // replaced by a new runtime cache + await assertNoConsoleErrors(browser) + // Click the revalidate button to trigger the server action await browser.elementByCss('#revalidate-button').click() @@ -215,8 +291,24 @@ describe('resume-data-cache', () => { // This test verifies that when a server action calls updateTag(), // the subsequent re-render sees fresh data instead of stale RDC data. // This is the "read your own writes" behavior for fetch cache. - - const browser = await next.browser('/revalidate-fetch-action') + const url = '/revalidate-fetch-action' + const valuePattern = /fetch-random-\d+\.\d+/ + const prerenderedCachedValue = await getPrerenderedValueFromPageSegment( + next, + url, + valuePattern + ) + + // The values of the cache entry should be consistent across shell/resume. + expectConsistentCacheValue( + await next.render(url), + valuePattern, + prerenderedCachedValue + ) + + const browser = await next.browser(url, { + pushErrorAsConsoleLog: true, + }) // Get the initial cached value from the page render const initialCachedValue = await browser @@ -226,6 +318,12 @@ describe('resume-data-cache', () => { .elementByCss('#uncached-value') .text() + expect(initialCachedValue).toBe(prerenderedCachedValue) + + // There should be no hydration errors due to a buildtime cache being + // replaced by a new runtime cache + await assertNoConsoleErrors(browser) + // Click the revalidate button to trigger the server action await browser.elementByCss('#revalidate-button').click() @@ -252,3 +350,102 @@ describe('resume-data-cache', () => { }) }) }) + +function expectConsistentCacheValue( + text: string, + valuePattern: RegExp, + expectedValue: string +) { + expect(getUniqueRegexMatches(text, valuePattern)).toEqual([expectedValue]) +} + +function getUniqueRegexMatches(text: string, pattern: RegExp) { + const uniqueMatches = new Set() + for (const match of text.matchAll( + new RegExp(pattern.source, pattern.flags + 'g') + )) { + // `matchAll` returns a match array for each occurence. + // We don't care about groups, so just take the whole match. + uniqueMatches.add(match[0]) + } + return [...uniqueMatches] +} + +async function getPrerenderedValueFromPageSegment( + next: NextInstance, + url: string, + valuePattern: RegExp +) { + const prerendered = await fetchStaticPageSegment(next, url).then((res) => + res.text() + ) + return getValueFromText(prerendered, valuePattern) +} + +function getValueFromText(text: string, valuePattern: RegExp) { + const values = getUniqueRegexMatches(text, valuePattern) + if (values.length === 0) { + throw new Error( + `Value pattern not found in prerender: ${valuePattern.source}\n\nText:\n\n{text}` + ) + } + if (values.length > 1) { + throw new Error( + `Found multiple values matching pattern: ${valuePattern.source}\n\nText:\n${text}` + ) + } + return values[0] +} + +async function fetchRSC(next: NextInstance, url: string) { + return await fetchWithRSCCacheBuster(next, url, { + headers: { + RSC: '1', + }, + }) +} + +async function fetchStaticPageSegment(next: NextInstance, url: string) { + const pathname = new URL(url, 'http://__n').pathname + return await fetchWithRSCCacheBuster(next, url, { + headers: { + RSC: '1', + 'Next-Router-Prefetch': '1', + 'Next-Router-Segment-Prefetch': pathname + '/__PAGE__', + }, + }) +} + +async function fetchWithRSCCacheBuster( + next: NextInstance, + rawUrl: string, + opts: RequestInit +): Promise { + // convert to Headers to normalize header casing + const headers = new Headers(opts.headers ?? {}) + + const urlObject = new URL(rawUrl, next.url) + urlObject.searchParams.set( + '_rsc', + await computeCacheBustingSearchParam( + (headers.get('next-router-prefetch') ?? undefined) as Parameters< + typeof computeCacheBustingSearchParam + >[0], + headers.get('next-router-segment-prefetch') ?? undefined, + headers.get('next-router-state-tree') ?? undefined, + headers.get('next-url') ?? undefined + ) + ) + const url = urlObject.href + const response = await next.fetch(url, { + ...opts, + headers, + }) + if (response.url !== url) { + console.error('URL mismatch with response (likely redirect)', { + url, + 'response.url': response.url, + }) + } + return response +} diff --git a/test/e2e/app-dir/resume-data-cache/utils.tsx b/test/e2e/app-dir/resume-data-cache/utils.tsx new file mode 100644 index 000000000000..dfae180cc66f --- /dev/null +++ b/test/e2e/app-dir/resume-data-cache/utils.tsx @@ -0,0 +1,25 @@ +export async function tasky() { + // make cache-misses noticeable + await new Promise((resolve) => setTimeout(resolve)) +} + +/** + * A wrapper meant to ensure fetches with independent tags are cached separately + * so that tests don't interfere with each others' fetch caches. + * */ +export async function fetchRandomWithForceCache( + opts: { tag: string } | { key: string; tag: undefined } +) { + let key: string + let tag: string | undefined + if ('key' in opts) { + key = opts.key + tag = undefined + } else { + key = tag = opts.tag + } + return fetch( + `https://next-data-api-endpoint.vercel.app/api/random?key=${encodeURIComponent(key)}`, + { cache: 'force-cache', next: { tags: tag !== undefined ? [tag] : [] } } + ) +} diff --git a/test/e2e/app-dir/segment-cache/cached-navigations/cached-navigations-partial-prefetching.test.ts b/test/e2e/app-dir/segment-cache/cached-navigations/cached-navigations-partial-prefetching.test.ts index 90cbb92ef937..b1153c0373a4 100644 --- a/test/e2e/app-dir/segment-cache/cached-navigations/cached-navigations-partial-prefetching.test.ts +++ b/test/e2e/app-dir/segment-cache/cached-navigations/cached-navigations-partial-prefetching.test.ts @@ -6,16 +6,12 @@ import { createRouterAct } from 'router-act' // The `partial-prefetching` fixture enables Partial Prefetching globally via // the next-config `partialPrefetching: true`, which opts every route into // runtime Cached Navigations even without a per-segment `prefetch` config. +// @force-gate !dev describe('cached navigations - global partialPrefetching', () => { - const { next, isNextDev } = nextTestSetup({ + const { next } = nextTestSetup({ files: path.join(__dirname, 'partial-prefetching'), }) - if (isNextDev) { - it('is skipped', () => {}) - return - } - it('runtime-caches a route that has no per-segment prefetch config', async () => { let page: Playwright.Page const browser = await next.browser('/', { @@ -90,4 +86,107 @@ describe('cached navigations - global partialPrefetching', () => { 'Dynamic content' ) }) + + it('cache values are consistent across the HTML shell, static prefetches, and cached navigations', async () => { + const href = '/cache-from-rdc' + const htmlId = 'cached-data' + + let page: Playwright.Page + const browser = await next.browser('/', { + beforePageLoad(p: Playwright.Page) { + page = p + }, + }) + const act = createRouterAct(page, { includeAppShellRequests: true }) + + // Reveal a link to the page. This should result in a static prefetch. + await act(async () => { + const linkToggle = await browser.elementByCss( + `[data-prefetch="auto"] input[data-link-accordion="${href}"]` + ) + await linkToggle.click() + }, [ + { + includes: 'cache-timestamp:', + kind: 'static', + }, + ]) + + //=========================== + // Test client navigation + //=========================== + + // Navigate to the page. + const cachedValueFromPrefetch = await act(async () => { + await browser + .elementByCss(`[data-prefetch="auto"] a[href="${href}"]`) + .click() + + const cachedValueFromPrefetch = await browser.elementById(htmlId).text() + return cachedValueFromPrefetch + }, [{ includes: 'Navigation-only runtime data' }]) + + expect(await browser.elementById('runtime-data').text()).toBe( + 'Navigation-only runtime data' + ) + + // The navigation response should also contain the same cache value. + expect(await browser.elementById(htmlId).text()).toBe( + cachedValueFromPrefetch + ) + + // Navigate back to the index page. + await act(() => browser.elementByCss('a[href="/"]').click(), 'no-requests') + // Then, navigate to the page again (without a prefetch). + // All the UI is cacheable, so it should be re-used from the client + // navigation, and we should navigate without any extra requests. + await act(async () => { + await browser + .elementByCss(`[data-prefetch="false"] a[href="${href}"]`) + .click() + }, 'no-requests') + + expect(await browser.elementById('runtime-data').text()).toBe( + 'Navigation-only runtime data' + ) + + // The cached client navigation should have the same cache value. + expect(await browser.elementById(htmlId).text()).toBe( + cachedValueFromPrefetch + ) + + //=========================== + // Test initial load + //=========================== + + await browser.refresh() + + expect(await browser.elementById('runtime-data').text()).toBe( + 'Navigation-only runtime data' + ) + // The initial load should have the same cache value. + expect(await browser.elementById(htmlId).text()).toBe( + cachedValueFromPrefetch + ) + + // Navigate to the index page. + await act(() => browser.elementByCss('a[href="/"]').click()) + // Then, navigate back to the page without a prefetch. + // All the UI is cacheable, so it should be re-used from the initial + // load, and we should navigate without any extra requests. + await act(async () => { + await browser + .elementByCss(`[data-prefetch="false"] a[href="${href}"]`) + .click() + }, 'no-requests') + + expect(await browser.elementById('runtime-data').text()).toBe( + 'Navigation-only runtime data' + ) + + // The cached navigation should have the same cache value. + expect(await browser.elementById(htmlId).text()).toBe( + cachedValueFromPrefetch + ) + }) }) diff --git a/test/e2e/app-dir/segment-cache/cached-navigations/cached-navigations.test.ts b/test/e2e/app-dir/segment-cache/cached-navigations/cached-navigations.test.ts index 8d6c0ffe0684..f84d780c33d0 100644 --- a/test/e2e/app-dir/segment-cache/cached-navigations/cached-navigations.test.ts +++ b/test/e2e/app-dir/segment-cache/cached-navigations/cached-navigations.test.ts @@ -1468,4 +1468,153 @@ describe('cached navigations', () => { it('runtime-caches a route with prefetch = "partial"', async () => { await expectRuntimeCachedOnSecondNavigation('/prefetch-partial') }) + + it('cache values are consistent across the HTML shell, static prefetches, and cached navigations', async () => { + const href = '/cache-from-rdc' + const htmlId = 'cached-data' + + let page: Playwright.Page + const browser = await next.browser('/', { + beforePageLoad(p: Playwright.Page) { + page = p + }, + }) + const act = createRouterAct(page, { includeAppShellRequests: true }) + + const getRenderId = async (): Promise => { + return await browser.elementById('render-id').text() + } + + // Reveal a link to the page. This should result in a static prefetch. + await act(async () => { + const linkToggle = await browser.elementByCss( + `[data-prefetch="auto"] input[data-link-accordion="${href}"]` + ) + await linkToggle.click() + }, [ + { + includes: 'cache-timestamp:', + kind: 'static', + }, + ]) + + //=========================== + // Test client navigation + //=========================== + + // Navigate to the page. + const { cachedValueFromPrefetch, prefetchRenderId } = await act( + async () => { + await browser + .elementByCss(`[data-prefetch="auto"] a[href="${href}"]`) + .click() + + const cachedValueFromPrefetch = await browser.elementById(htmlId).text() + const prefetchRenderId = await getRenderId() + return { cachedValueFromPrefetch, prefetchRenderId } + }, + { includes: 'Dynamic data' } + ) + { + expect(await browser.elementById('dynamic-data').text()).toBe( + 'Dynamic data' + ) + + // The navigation response should also contain the same cache value. + expect(await browser.elementById(htmlId).text()).toBe( + cachedValueFromPrefetch + ) + const clientNavRenderId = await getRenderId() + + // Navigate back to the index page. + await act( + () => browser.elementByCss('a[href="/"]').click(), + 'no-requests' + ) + // Then, navigate to the page again (without a prefetch). + // We should re-use the cacheable part of the UI from the navigation. + await act( + async () => { + await browser + .elementByCss(`[data-prefetch="false"] a[href="${href}"]`) + .click() + + // Make sure we're showing the content from the cached navigation, + // not the static prefetch -- otherwise, the cache consistency check + // would be meaningless. + const visibleRenderId = await getRenderId() + expect(visibleRenderId).not.toBe(prefetchRenderId) + expect(visibleRenderId).toBe(clientNavRenderId) + + // The cacheable UI extracted from the navigation (shown while navigating) + // should have the same cache value. + expect(await browser.elementById(htmlId).text()).toBe( + cachedValueFromPrefetch + ) + }, + { includes: 'Dynamic data' } + ) + + expect(await browser.elementById('dynamic-data').text()).toBe( + 'Dynamic data' + ) + + // The second navigation result should have the same cache value. + expect(await browser.elementById(htmlId).text()).toBe( + cachedValueFromPrefetch + ) + // We have uncached data, so this was a fresh render. + expect(await getRenderId()).not.toBe(clientNavRenderId) + } + + //=========================== + // Test initial load + //=========================== + { + await browser.refresh() + + expect(await browser.elementById('dynamic-data').text()).toBe( + 'Dynamic data' + ) + // The initial load should have the same cache value. + expect(await browser.elementById(htmlId).text()).toBe( + cachedValueFromPrefetch + ) + const initialLoadRenderId = await getRenderId() + + // Navigate to the index page. + await act(() => browser.elementByCss('a[href="/"]').click()) + // Then, navigate back to the page without a prefetch. + // We should re-use the cacheable part of the UI from the navigation. + await act( + async () => { + await browser + .elementByCss(`[data-prefetch="false"] a[href="${href}"]`) + .click() + + // Make sure we're showing the content from the cached navigation. + const visibleRenderId = await getRenderId() + expect(visibleRenderId).toBe(initialLoadRenderId) + + // The cacheable UI extracted from the navigation (shown while navigating) + // should have the same cache value. + expect(await browser.elementById(htmlId).text()).toBe( + cachedValueFromPrefetch + ) + }, + { includes: 'Dynamic data' } + ) + + expect(await browser.elementById('dynamic-data').text()).toBe( + 'Dynamic data' + ) + + // The second navigation result should have the same cache value. + expect(await browser.elementById(htmlId).text()).toBe( + cachedValueFromPrefetch + ) + // We have uncached data, so this was a fresh render. + expect(await getRenderId()).not.toBe(initialLoadRenderId) + } + }) }) diff --git a/test/e2e/app-dir/segment-cache/cached-navigations/default/app/cache-from-rdc/page.tsx b/test/e2e/app-dir/segment-cache/cached-navigations/default/app/cache-from-rdc/page.tsx new file mode 100644 index 000000000000..b942f4d9671a --- /dev/null +++ b/test/e2e/app-dir/segment-cache/cached-navigations/default/app/cache-from-rdc/page.tsx @@ -0,0 +1,39 @@ +import Link from 'next/link' +import { connection } from 'next/server' +import { Suspense } from 'react' + +export default async function Page() { + const cachedTimestamp = await getCachedTimestamp() + return ( +
+

{cachedTimestamp}

+ +

+ {Math.floor(performance.timeOrigin + performance.now())} +

+ + + Go back to index page + + + Loading non-prerenderable data...
}> + {(async () => { + // Use cookies() after navigation() so that the page + // doesn't require a runtime shell. + await connection() + return

Dynamic data

+ })()} + + + ) +} + +async function getCachedTimestamp() { + 'use cache' + await new Promise((resolve) => setTimeout(resolve)) + return `cache-timestamp: ${Date.now()}` +} diff --git a/test/e2e/app-dir/segment-cache/cached-navigations/default/app/page.tsx b/test/e2e/app-dir/segment-cache/cached-navigations/default/app/page.tsx index b6f0d5ed4c62..73e086fdd3bc 100644 --- a/test/e2e/app-dir/segment-cache/cached-navigations/default/app/page.tsx +++ b/test/e2e/app-dir/segment-cache/cached-navigations/default/app/page.tsx @@ -1,4 +1,5 @@ import Link from 'next/link' +import { LinkAccordion } from '../components/link-accordion' export default function Home() { return ( @@ -39,6 +40,20 @@ export default function Home() { + +

Cache value consistency

+
    +
  • + + /cache-from-rdc (prefetch="auto") + +
  • +
  • + + /cache-from-rdc (prefetch="false") + +
  • +
) } diff --git a/test/e2e/app-dir/segment-cache/cached-navigations/default/components/link-accordion.tsx b/test/e2e/app-dir/segment-cache/cached-navigations/default/components/link-accordion.tsx index fd8f6781732e..f91028acf911 100644 --- a/test/e2e/app-dir/segment-cache/cached-navigations/default/components/link-accordion.tsx +++ b/test/e2e/app-dir/segment-cache/cached-navigations/default/components/link-accordion.tsx @@ -1,24 +1,26 @@ 'use client' -import Link, { LinkProps } from 'next/link' +import Link, { type LinkProps } from 'next/link' import { useState } from 'react' export function LinkAccordion({ href, children, - prefetch, + prefetch = 'auto', }: { href: string children: React.ReactNode prefetch?: LinkProps['prefetch'] }) { const [isVisible, setIsVisible] = useState(false) + const prefetchAttr = prefetch === null ? 'auto' : `${prefetch}` return ( <> setIsVisible(!isVisible)} + data-prefetch={prefetchAttr} data-link-accordion={href} /> {isVisible ? ( @@ -26,7 +28,7 @@ export function LinkAccordion({ {children} ) : ( - `${children} (link is hidden)` + <>{children} (link is hidden) )} ) diff --git a/test/e2e/app-dir/segment-cache/cached-navigations/partial-prefetching/app/cache-from-rdc/page.tsx b/test/e2e/app-dir/segment-cache/cached-navigations/partial-prefetching/app/cache-from-rdc/page.tsx new file mode 100644 index 000000000000..163c9bf17626 --- /dev/null +++ b/test/e2e/app-dir/segment-cache/cached-navigations/partial-prefetching/app/cache-from-rdc/page.tsx @@ -0,0 +1,37 @@ +import { navigation } from 'next/cache' +import { cookies } from 'next/headers' +import Link from 'next/link' +import { Suspense } from 'react' + +export default async function Page() { + const cachedTimestamp = await getCachedTimestamp() + return ( +
+

{cachedTimestamp}

+ + + Go back to index page + + + Loading runtime data...}> + {(async () => { + // Use cookies() after navigation() so that the page + // doesn't require a runtime shell. + await navigation() + await cookies() + return

Navigation-only runtime data

+ })()} +
+
+ ) +} + +async function getCachedTimestamp() { + 'use cache' + await new Promise((resolve) => setTimeout(resolve)) + return `cache-timestamp: ${Date.now()}` +} diff --git a/test/e2e/app-dir/segment-cache/cached-navigations/partial-prefetching/app/page.tsx b/test/e2e/app-dir/segment-cache/cached-navigations/partial-prefetching/app/page.tsx index 694d9f02cd4f..06b6429f89bc 100644 --- a/test/e2e/app-dir/segment-cache/cached-navigations/partial-prefetching/app/page.tsx +++ b/test/e2e/app-dir/segment-cache/cached-navigations/partial-prefetching/app/page.tsx @@ -1,12 +1,11 @@ import Link from 'next/link' +import { LinkAccordion } from '../components/link-accordion' export default function Home() { return (

Home

-

- Links with prefetch=false -

+

Runtime prefetchable

  • @@ -14,6 +13,20 @@ export default function Home() {
+ +

Cache value consistency

+
    +
  • + + /cache-from-rdc (prefetch="auto") + +
  • +
  • + + /cache-from-rdc (prefetch="false") + +
  • +
) } diff --git a/test/e2e/app-dir/segment-cache/cached-navigations/partial-prefetching/components/link-accordion.tsx b/test/e2e/app-dir/segment-cache/cached-navigations/partial-prefetching/components/link-accordion.tsx new file mode 100644 index 000000000000..f91028acf911 --- /dev/null +++ b/test/e2e/app-dir/segment-cache/cached-navigations/partial-prefetching/components/link-accordion.tsx @@ -0,0 +1,35 @@ +'use client' + +import Link, { type LinkProps } from 'next/link' +import { useState } from 'react' + +export function LinkAccordion({ + href, + children, + prefetch = 'auto', +}: { + href: string + children: React.ReactNode + prefetch?: LinkProps['prefetch'] +}) { + const [isVisible, setIsVisible] = useState(false) + const prefetchAttr = prefetch === null ? 'auto' : `${prefetch}` + return ( + <> + setIsVisible(!isVisible)} + data-prefetch={prefetchAttr} + data-link-accordion={href} + /> + {isVisible ? ( + + {children} + + ) : ( + <>{children} (link is hidden) + )} + + ) +} diff --git a/test/e2e/app-dir/segment-cache/prefetch-runtime/app/(default)/cache-from-rdc/page.tsx b/test/e2e/app-dir/segment-cache/prefetch-runtime/app/(default)/cache-from-rdc/page.tsx new file mode 100644 index 000000000000..1d11e3b4616b --- /dev/null +++ b/test/e2e/app-dir/segment-cache/prefetch-runtime/app/(default)/cache-from-rdc/page.tsx @@ -0,0 +1,57 @@ +import { connection } from 'next/server' +import { cookies } from 'next/headers' +import { Suspense } from 'react' + +type SearchParams = Record + +export default async function Page({ + searchParams, +}: { + searchParams: Promise +}) { + const cachedTimestamp = await getCachedTimestamp() + return ( +
+

+ This page uses a cache that is included both in the HTML shell and in + runtime shells/prefetches. The values should consistent everywhere + thanks to the RDC. +

+

{cachedTimestamp}

+ Loading...}> + + + + Loading dynamic data...}> + {connection().then(() => ( +

Dynamic data

+ ))} +
+
+ ) +} + +async function Runtime({ + searchParams, +}: { + searchParams: Promise +}) { + // Force the page to use a runtime shell/prefetch + await cookies() + return ( +
+ + Loading...
}> + {searchParams.then(() => ( +

Search params data

+ ))} + + + ) +} + +async function getCachedTimestamp() { + 'use cache' + await new Promise((resolve) => setTimeout(resolve)) + return `cache-timestamp: ${Date.now()}` +} diff --git a/test/e2e/app-dir/segment-cache/prefetch-runtime/app/(default)/page.tsx b/test/e2e/app-dir/segment-cache/prefetch-runtime/app/(default)/page.tsx index 17e8f4608ca0..43d08969a55c 100644 --- a/test/e2e/app-dir/segment-cache/prefetch-runtime/app/(default)/page.tsx +++ b/test/e2e/app-dir/segment-cache/prefetch-runtime/app/(default)/page.tsx @@ -291,6 +291,12 @@ export default async function Page() {
  • +
  • + +
  • +
  • + +
  • ) diff --git a/test/e2e/app-dir/segment-cache/prefetch-runtime/prefetch-runtime.test.ts b/test/e2e/app-dir/segment-cache/prefetch-runtime/prefetch-runtime.test.ts index 92b6b34cb8eb..a3d39e09b2bf 100644 --- a/test/e2e/app-dir/segment-cache/prefetch-runtime/prefetch-runtime.test.ts +++ b/test/e2e/app-dir/segment-cache/prefetch-runtime/prefetch-runtime.test.ts @@ -1088,6 +1088,106 @@ describe('runtime prefetching', () => { }) }) + describe('cache values are consistent across HTML shell and runtime requests', () => { + const href = '/cache-from-rdc' + const htmlId = 'cached-data' + + const getCacheValueFromInitialHTML = async (): Promise => { + const $ = await next.render$(href) + const text = $(`#${htmlId}`).text() + if (!text) { + throw new Error('Cached value not found in HTML') + } + return text + } + + it('runtime shell', async () => { + let page: Playwright.Page + const browser = await next.browser('/', { + beforePageLoad(p: Playwright.Page) { + page = p + }, + }) + const act = createRouterAct(page, { includeAppShellRequests: true }) + + const cacheValueFromHTMLShell = await getCacheValueFromInitialHTML() + + // Reveal the link to trigger a runtime prefetch for the page + await act(async () => { + const linkToggle = await browser.elementByCss( + `input[data-prefetch="auto"][data-link-accordion="${href}"]` + ) + await linkToggle.click() + }, [ + { + includes: 'Cookie data', + kind: 'runtime', + }, + ]) + + // Navigate to the page. + await act(async () => { + await browser.elementByCss(`a[href="${href}"]`).click() + + // The runtime shell should contain the same cache value as the HTML. + expect(await browser.elementById(htmlId).text()).toBe( + cacheValueFromHTMLShell + ) + }, [{ includes: 'Dynamic data' }]) + + // The navigation response should also contain the same cache value. + expect(await browser.elementById(htmlId).text()).toBe( + cacheValueFromHTMLShell + ) + }) + + it('runtime prefetch', async () => { + let page: Playwright.Page + const browser = await next.browser('/', { + beforePageLoad(p: Playwright.Page) { + page = p + }, + }) + const act = createRouterAct(page, { includeAppShellRequests: true }) + + const cacheValueFromHTMLShell = await getCacheValueFromInitialHTML() + + // Reveal the link to trigger a runtime prefetch for the page + await act(async () => { + const linkToggle = await browser.elementByCss( + `input[data-prefetch="true"][data-link-accordion="${href}"]` + ) + await linkToggle.click() + }, [ + // Shell + { + includes: 'Cookie data', + kind: 'runtime', + }, + // Prefetch + { + includes: 'Search params data', + kind: 'runtime', + }, + ]) + + // Navigate to the page. + await act(async () => { + await browser.elementByCss(`a[href="${href}"]`).click() + + // The runtime prefetch should contain the same cache value as the HTML. + expect(await browser.elementById(htmlId).text()).toBe( + cacheValueFromHTMLShell + ) + }, [{ includes: 'Dynamic data' }]) + + // The navigation response should also contain the same cache value. + expect(await browser.elementById(htmlId).text()).toBe( + cacheValueFromHTMLShell + ) + }) + }) + describe('errors', () => { it.each([ { diff --git a/test/e2e/app-dir/typed-routes-validator/app/icon.tsx b/test/e2e/app-dir/typed-routes-validator/app/icon.tsx new file mode 100644 index 000000000000..03f7110b25e1 --- /dev/null +++ b/test/e2e/app-dir/typed-routes-validator/app/icon.tsx @@ -0,0 +1,8 @@ +import { ImageResponse } from 'next/og' + +export const size = { width: 32, height: 32 } +export const contentType = 'image/png' + +export default function Icon() { + return new ImageResponse(
    A
    , size) +} diff --git a/test/e2e/app-dir/typed-routes-validator/typed-routes-validator.test.ts b/test/e2e/app-dir/typed-routes-validator/typed-routes-validator.test.ts index 09533b7b47eb..67c69077293f 100644 --- a/test/e2e/app-dir/typed-routes-validator/typed-routes-validator.test.ts +++ b/test/e2e/app-dir/typed-routes-validator/typed-routes-validator.test.ts @@ -152,8 +152,13 @@ describe('typed-routes-validator', () => { /Type error: Type 'typeof import\(.*' does not satisfy the expected type 'AppPageConfig= MIN_PREFETCHABLE_STALE, expire >= // MIN_PRERENDERABLE_EXPIRE): stays in the static shell. cacheLife({ stale: 60, revalidate: 100, expire: 1000 }) - return Math.random() + + await tasky() + return `prerenderable: ${Date.now()}` } -async function ShortStaleCache() { +async function NonPrefetchableCache() { 'use cache' // Long expire (>= MIN_PRERENDERABLE_EXPIRE, 5min) and a non-zero revalidate, // but a stale time below MIN_PREFETCHABLE_STALE (30s). This isolates the @@ -17,18 +20,17 @@ async function ShortStaleCache() { // because of its short stale time, not because of a short expire or // revalidate: 0. cacheLife({ stale: 18, revalidate: 100, expire: 1000 }) - return

    {new Date().toISOString()}

    + return

    {`non-prefetchable: ${Date.now()}`}

    } export default async function Page() { - const x = await getCachedRandom() - + const cachedNow = await getCachedNow() return ( - <> -

    {x}

    - Loading...

    }> - +
    +

    {cachedNow}

    + Loading...

    }> +
    - +
    ) } diff --git a/test/e2e/app-dir/use-cache/app/(partially-static)/cache-life-non-prerenderable/page.tsx b/test/e2e/app-dir/use-cache/app/(partially-static)/cache-life-non-prerenderable/page.tsx new file mode 100644 index 000000000000..009980320296 --- /dev/null +++ b/test/e2e/app-dir/use-cache/app/(partially-static)/cache-life-non-prerenderable/page.tsx @@ -0,0 +1,44 @@ +import { cacheLife } from 'next/cache' +import { connection } from 'next/server' +import { Suspense } from 'react' +import { tasky } from '../../../utils' + +async function getCachedNow() { + 'use cache' + // prerenderable and prefetchable. + cacheLife('frequent') + + await tasky() + return `prerenderable: ${Date.now()}` +} + +async function NonPrerenderableCache() { + 'use cache' + // Not prerenderable and not prefetchable. + cacheLife({ + revalidate: 99, + expire: 299, // < MIN_PRERENDERABLE_EXPIRE + stale: 18, // < MIN_PREFETCHABLE_STALE + }) + return

    {`non-prerenderable: ${Date.now()}`}

    +} + +async function Dynamic() { + await connection() + return null +} + +export default async function Page() { + const cachedNow = await getCachedNow() + return ( +
    +

    {cachedNow}

    + Loading...

    }> + +
    + + + +
    + ) +} diff --git a/test/e2e/app-dir/use-cache/app/(partially-static)/cache-life-with-dynamic/page.tsx b/test/e2e/app-dir/use-cache/app/(partially-static)/cache-life-with-dynamic/page.tsx deleted file mode 100644 index 5c092a146186..000000000000 --- a/test/e2e/app-dir/use-cache/app/(partially-static)/cache-life-with-dynamic/page.tsx +++ /dev/null @@ -1,36 +0,0 @@ -import { cacheLife } from 'next/cache' -import { connection } from 'next/server' -import { Suspense } from 'react' - -async function getCachedRandom() { - 'use cache' - cacheLife('frequent') - return Math.random() -} - -async function DynamicCache() { - 'use cache' - cacheLife({ revalidate: 99, expire: 299, stale: 18 }) - return

    {new Date().toISOString()}

    -} - -async function Dynamic() { - await connection() - return null -} - -export default async function Page() { - const x = await getCachedRandom() - - return ( - <> -

    {x}

    - Loading...

    }> - -
    - - - - - ) -} diff --git a/test/e2e/app-dir/use-cache/app/(partially-static)/short-lived-caches/page.tsx b/test/e2e/app-dir/use-cache/app/(partially-static)/short-lived-caches/page.tsx index 5701ce0bd639..1eef4ab8949f 100644 --- a/test/e2e/app-dir/use-cache/app/(partially-static)/short-lived-caches/page.tsx +++ b/test/e2e/app-dir/use-cache/app/(partially-static)/short-lived-caches/page.tsx @@ -1,34 +1,38 @@ import { connection } from 'next/server' import { cacheLife } from 'next/cache' import { Suspense } from 'react' +import { tasky } from '../../../utils' async function revalidateZero() { 'use cache: remote' cacheLife({ revalidate: 0 }) - return new Date().toISOString() + await tasky() + return Date.now() } async function lowExpire() { 'use cache: remote' cacheLife({ expire: 5 }) - return new Date().toISOString() + await tasky() + return Date.now() } async function OuterCacheNoExplicit() { 'use cache: remote' // No explicit cacheLife - this would error during prerendering, but is // allowed at request time (after connection()). - + await tasky() return ( <>

    revalidate=0:{' '} - {await revalidateZero()} + {`bare-revalidate-zero: ${await revalidateZero()}`}

    - expire=5: {await lowExpire()} + expire=5:{' '} + {`bare-low-expire: ${await lowExpire()}`}

    ) @@ -38,16 +42,16 @@ async function OuterCacheExplicitShort() { 'use cache: remote' // Explicit short cacheLife - excluded from prerender, becomes a dynamic hole. cacheLife({ revalidate: 0, expire: 5 }) - + await tasky() return ( <>

    Explicit revalidate=0:{' '} - {await revalidateZero()} + {`explicit-revalidate-zero: ${await revalidateZero()}`}

    Explicit expire=5:{' '} - {await lowExpire()} + {`explicit-low-expire: ${await lowExpire()}`}

    ) @@ -58,16 +62,16 @@ async function OuterCacheExplicitLong() { // Explicit long cacheLife - included in prerender despite short-lived inner // caches. cacheLife('default') - + await tasky() return ( <>

    Explicit long (revalidate=0 inner):{' '} - {await revalidateZero()} + {`explicit-long-revalidate-zero: ${await revalidateZero()}`}

    Explicit long (expire=5 inner):{' '} - {await lowExpire()} + {`explicit-long-low-expire: ${await lowExpire()}`}

    ) @@ -81,7 +85,7 @@ async function Dynamic() { export default async function Page() { return ( - <> +

    Static content

    Loading...

    }> @@ -90,6 +94,6 @@ export default async function Page() {
    - +
    ) } diff --git a/test/e2e/app-dir/use-cache/use-cache.partial-prefetching.test.ts b/test/e2e/app-dir/use-cache/use-cache.partial-prefetching.test.ts new file mode 100644 index 000000000000..72c8660577d4 --- /dev/null +++ b/test/e2e/app-dir/use-cache/use-cache.partial-prefetching.test.ts @@ -0,0 +1,3 @@ +process.env.__NEXT_CACHE_COMPONENTS = 'true' +process.env.__NEXT_PARTIAL_PREFETCHING = 'true' +require('./use-cache.test') diff --git a/test/e2e/app-dir/use-cache/use-cache.test.ts b/test/e2e/app-dir/use-cache/use-cache.test.ts index 307b758bf02d..4202aef76e24 100644 --- a/test/e2e/app-dir/use-cache/use-cache.test.ts +++ b/test/e2e/app-dir/use-cache/use-cache.test.ts @@ -4,6 +4,7 @@ import { waitForNoErrorToast, retry, waitFor, + gate, } from 'next-test-utils' import type { Playwright } from 'e2e-utils' import stripAnsi from 'strip-ansi' @@ -565,8 +566,8 @@ describe('use-cache', () => { '/cache-fetch-no-store', '/cache-life', // Without cache components this page is fully static. With cache - // components its short-stale cache becomes a dynamic hole. - !withCacheComponents && '/cache-life-short-stale', + // components its non-prefetchable cache becomes a dynamic hole. + !withCacheComponents && '/cache-life-non-prefetchable', '/cache-tag', '/directive-in-node-modules/with-handler', '/directive-in-node-modules/without-handler', @@ -609,11 +610,11 @@ describe('use-cache', () => { if (withCacheComponents) { expect( - routes['/cache-life-with-dynamic'].initialRevalidateSeconds + routes['/cache-life-non-prerenderable'].initialRevalidateSeconds ).toBe(100) - expect(routes['/cache-life-with-dynamic'].initialExpireSeconds).toBe( - 300 - ) + expect( + routes['/cache-life-non-prerenderable'].initialExpireSeconds + ).toBe(300) } // default expireTime @@ -632,7 +633,9 @@ describe('use-cache', () => { if (withCacheComponents) { const cacheLifeWithDynamicMeta = JSON.parse( - await next.readFile('.next/server/app/cache-life-with-dynamic.meta') + await next.readFile( + '.next/server/app/cache-life-non-prerenderable.meta' + ) ) expect(cacheLifeWithDynamicMeta.headers['x-nextjs-stale-time']).toBe( '30' @@ -658,44 +661,112 @@ describe('use-cache', () => { }) if (withCacheComponents) { - it('should omit dynamic caches from prerendered shells', async () => { - const browser = await next.browser('/cache-life-with-dynamic', { + it('should omit non-prerenderable caches from prerendered shells', async () => { + const cacheValues = { + prerenderable: /(? document.documentElement.outerHTML + ) + + // Both caches should have a consistent value across the HTML document + // (note: this includes the HTML shell, streamed resume HTML, and the RSC payload) + expect( + getUniqueRegexMatches(html, cacheValues.prerenderable) + ).toHaveLength(1) + expect( + getUniqueRegexMatches(html, cacheValues['non-prerenderable']) + ).toHaveLength(1) + + expect(await browser.elementById('prerenderable').text()).toMatch( + cacheValues.prerenderable + ) + // The un-hydrated HTML should be showing a fallback for the non-prerenderable cache. + expect(await browser.elementById('non-prerenderable').text()).toBe( + 'Loading...' + ) }) - it('should omit caches with a short stale time from prerendered shells', async () => { + it('should omit non-prefetchable caches from prerendered shells', async () => { + const cacheValues = { + prerenderable: /prerenderable: \d+/, + 'non-prefetchable': /non-prefetchable: \d+/, + } + // Disable JS as a hack to see what's included in the static shell - let browser = await next.browser('/cache-life-short-stale', { - disableJavaScript: true, - }) + { + const browser = await next.browser('/cache-life-non-prefetchable', { + disableJavaScript: true, + }) - expect(await browser.elementById('x').text()).toBeTruthy() - // We expect the cache to be excluded, so it's showing a suspense fallback - expect(await browser.elementById('y').text()).toBe('Loading...') + const html = await browser.eval( + () => document.documentElement.outerHTML + ) - // If we let JS run, we should see the cache's actual value - browser = await next.browser('/cache-life-short-stale', { - pushErrorAsConsoleLog: true, - }) + // Both caches should have a consistent value across the HTML document + // (note: this includes the HTML shell, streamed resume HTML, and the RSC payload) + expect( + getUniqueRegexMatches(html, cacheValues.prerenderable) + ).toHaveLength(1) + expect( + getUniqueRegexMatches(html, cacheValues['non-prefetchable']) + ).toHaveLength(1) + + expect(await browser.elementById('prerenderable').text()).toMatch( + cacheValues.prerenderable + ) + // We expect the non-prefetchable cache to be excluded from the HTML shell, + // so should showing a suspense fallback + expect(await browser.elementById('non-prefetchable').text()).toBe( + 'Loading...' + ) - await retry(async () => { - expect(await browser.elementById('y').text()).toBeDateString() - }) + await browser.close() + } + + // If we let JS run, we should see the cache's actual value + // (NOTE: can't enable JS without recreating `browser()`) + { + const browser = await next.browser('/cache-life-non-prefetchable', { + pushErrorAsConsoleLog: true, + }) + expect(await browser.elementById('prerenderable').text()).toMatch( + cacheValues.prerenderable + ) + await retry(async () => { + expect( + await browser.elementById('non-prefetchable').text() + ).toMatch(cacheValues['non-prefetchable']) + }) - await assertNoConsoleErrors(browser) + await assertNoConsoleErrors(browser) + } }) } - it('should not have hydration errors when resuming a partial shell with dynamic caches', async () => { - const browser = await next.browser('/cache-life-with-dynamic', { + it('should not have hydration errors when resuming a partial shell with non-prerenderable caches', async () => { + const cacheValues = { + prerenderable: /(? { - expect(await browser.elementById('y').text()).not.toBe('Loading...') + expect(await browser.elementById('non-prerenderable').text()).toMatch( + cacheValues['non-prerenderable'] + ) }) // There should be no hydration errors due to a buildtime date being @@ -1269,7 +1340,7 @@ describe('use-cache', () => { if (withCacheComponents) { it('can resume a cached generateMetadata function', async () => { // In dev the initial request fills the caches while streaming the - // response. The second request will have filled caches and server a + // response. The second request will have filled caches and serves a // prod-like shell. if (isNextDev) { await next.fetch('/generate-metadata-resume/nested') @@ -1580,46 +1651,99 @@ describe('use-cache', () => { } it('should allow nested short-lived caches after connection()', async () => { + const cacheValues = { + 'revalidate-zero': /bare-revalidate-zero: \d+/, + 'low-expire': /bare-low-expire: \d+/, + + 'explicit-revalidate-zero': /explicit-revalidate-zero: \d+/, + 'explicit-low-expire': /explicit-low-expire: \d+/, + + 'explicit-long-revalidate-zero': /explicit-long-revalidate-zero: \d+/, + 'explicit-long-low-expire': /explicit-long-low-expire: \d+/, + } + + // In dev the initial request fills the caches while streaming the + // response. The second request will have filled caches and serves a + // prod-like shell. + if (isNextDev) { + await next.fetch('/short-lived-caches') + } + // Check the prerendered shell (no JS). - let browser = await next.browser('/short-lived-caches', { - disableJavaScript: true, - }) + { + const browser = await next.browser('/short-lived-caches', { + disableJavaScript: true, + }) + + const html = await browser.eval(() => document.documentElement.outerHTML) + + // Static content should be in the shell. + expect(await browser.elementById('static').text()).toBe('Static content') - // Static content should be in the shell. - expect(await browser.elementById('static').text()).toBe('Static content') + // Explicit long cacheLife should be in the shell despite short-lived inner + // caches. + const expectIncluded = async (id: keyof typeof cacheValues) => { + // Look for elements within
    to exlude streaming fizz chunks. + // NOTE: content being present in the shell is only reliable in cacheComponents + if (await gate('cacheComponents')) { + expect(await browser.elementByCss(`main #${id}`).text()).toMatch( + cacheValues[id] + ) + } + // The cache will be present in HTML shell, streaming HTML, and the RSC payload. + // It should have consistent values across all of those. + expect(getUniqueRegexMatches(html, cacheValues[id])).toHaveLength(1) + } + await expectIncluded('explicit-long-revalidate-zero') + await expectIncluded('explicit-long-low-expire') + + // For the other caches, the outer cache was omitted from the prerender, so they + // should not be visible in the HTML shell. + const expectExcluded = async (id: keyof typeof cacheValues) => { + // Look for elements within
    to exlude streaming fizz chunks. + // NOTE: content being present in the shell is only reliable in cacheComponents + if (await gate('cacheComponents')) { + expect(await browser.locator(`main #${id}`).count()).toBe(0) + } + // The cache will be present in streaming HTML and the RSC payload. + // It should have consistent values across both of those. + expect(getUniqueRegexMatches(html, cacheValues[id])).toHaveLength(1) + } + await expectExcluded('revalidate-zero') + await expectExcluded('low-expire') + await expectExcluded('explicit-revalidate-zero') + await expectExcluded('explicit-low-expire') - // Explicit long cacheLife should be in the shell despite short-lived inner - // caches. - expect( - await browser.elementById('explicit-long-revalidate-zero').text() - ).toBeDateString() - expect( - await browser.elementById('explicit-long-low-expire').text() - ).toBeDateString() + await browser.close() + } // Now check with JS enabled to verify dynamic content loads. - browser = await next.browser('/short-lived-caches', { - pushErrorAsConsoleLog: true, - }) + { + const browser = await next.browser('/short-lived-caches', { + pushErrorAsConsoleLog: true, + }) - // Dynamic content should eventually render. - await retry(async () => { - // No explicit outer cacheLife (after connection()). - expect( - await browser.elementById('revalidate-zero').text() - ).toBeDateString() - expect(await browser.elementById('low-expire').text()).toBeDateString() + // Dynamic content should eventually render. + await retry(async () => { + // No explicit outer cacheLife (after connection()). + expect(await browser.elementById('revalidate-zero').text()).toMatch( + cacheValues['revalidate-zero'] + ) + expect(await browser.elementById('low-expire').text()).toMatch( + cacheValues['low-expire'] + ) - // Explicit short cacheLife - excluded from prerender. - expect( - await browser.elementById('explicit-revalidate-zero').text() - ).toBeDateString() - expect( - await browser.elementById('explicit-low-expire').text() - ).toBeDateString() - }) + // Explicit short cacheLife - excluded from prerender. + expect( + await browser.elementById('explicit-revalidate-zero').text() + ).toMatch(cacheValues['explicit-revalidate-zero']) + expect(await browser.elementById('explicit-low-expire').text()).toMatch( + cacheValues['explicit-low-expire'] + ) + }) - await assertNoConsoleErrors(browser) + await assertNoConsoleErrors(browser) + } }) it('should dedupe shared inner caches across different outer caches', async () => { @@ -1828,3 +1952,15 @@ function extractResumeDataCacheFromPostponedState( undefined ) } + +function getUniqueRegexMatches(text: string, pattern: RegExp) { + const uniqueMatches = new Set() + for (const match of text.matchAll( + new RegExp(pattern.source, pattern.flags + 'g') + )) { + // `matchAll` returns a match array for each occurence. + // We don't care about groups, so just take the whole match. + uniqueMatches.add(match[0]) + } + return [...uniqueMatches] +} diff --git a/test/e2e/app-dir/use-cache/utils.ts b/test/e2e/app-dir/use-cache/utils.ts new file mode 100644 index 000000000000..18bf37dcbcf1 --- /dev/null +++ b/test/e2e/app-dir/use-cache/utils.ts @@ -0,0 +1,4 @@ +export async function tasky() { + // make cache-misses noticeable + await new Promise((resolve) => setTimeout(resolve)) +} diff --git a/test/e2e/next-image-new/image-from-node-modules/image-from-node-modules.test.ts b/test/e2e/next-image-new/image-from-node-modules/image-from-node-modules.test.ts index a322849011e1..b33357d4b6e8 100644 --- a/test/e2e/next-image-new/image-from-node-modules/image-from-node-modules.test.ts +++ b/test/e2e/next-image-new/image-from-node-modules/image-from-node-modules.test.ts @@ -18,6 +18,14 @@ describe('Image Component from node_modules', () => { expect(srcset).toMatch('1234') }) + it('renders an external image during server-side rendering', async () => { + const response = await next.fetch('/ssr') + expect(response.status).toBe(200) + const html = await response.text() + expect(html).toContain('id="ssr-image-from-node-modules"') + expect(html).toContain('w=1234') + }) + it('should warn when using images.domains config', async () => { expect(next.cliOutput).toContain( '`images.domains` is deprecated in favor of `images.remotePatterns`. Please update next.config.js to protect your application from malicious users.' diff --git a/test/e2e/next-image-new/image-from-node-modules/next.config.js b/test/e2e/next-image-new/image-from-node-modules/next.config.js index 72a887fec554..7947aed21bf7 100644 --- a/test/e2e/next-image-new/image-from-node-modules/next.config.js +++ b/test/e2e/next-image-new/image-from-node-modules/next.config.js @@ -1,6 +1,7 @@ module.exports = { + serverExternalPackages: ['my-cool-image'], images: { domains: ['i.imgur.com'], - deviceSizes: [1234], + deviceSizes: [1234, 640], }, } diff --git a/test/e2e/next-image-new/image-from-node-modules/pages/ssr.js b/test/e2e/next-image-new/image-from-node-modules/pages/ssr.js new file mode 100644 index 000000000000..8353c98a8054 --- /dev/null +++ b/test/e2e/next-image-new/image-from-node-modules/pages/ssr.js @@ -0,0 +1,16 @@ +import MyCoolImage from 'my-cool-image' + +export function getServerSideProps() { + return { props: {} } +} + +export default function Page() { + return ( + + ) +} diff --git a/test/lib/gate/conditions.ts b/test/lib/gate/conditions.ts index 1979d01c10bc..52e16f4b5439 100644 --- a/test/lib/gate/conditions.ts +++ b/test/lib/gate/conditions.ts @@ -176,6 +176,10 @@ export const conditions: Record = { 'Cache Components are enabled for the fixture', (config) => config.cacheComponents ), + partialPrefetchingGlobal: lazyCondition( + 'Partial Prefetching is enabled globally for the fixture (does not check `export const prefetch = "partial"`)', + (config) => config.partialPrefetching + ), ppr: lazyCondition( 'partial prerendering is enabled (implied by `cacheComponents`)', (config) => config.experimental?.ppr