Unverified Commit cc3eb6f0 authored by Sebastian Markbåge's avatar Sebastian Markbåge Committed by GitHub

Add pathname to PrerenderStore (#70889)

This lets us use this for implicit tags when a full Request is not
available. Required for #70819.

This should probably be more normalized but I only pass the path name
instead of the full url to avoid leaking more dynamic information to the
prerender.

This does not yet solve the case where we have nested caches (which we
don't in unstable_cache but in use cache). In that case it's more
complex because for an outer hit we'd need to add it to the inner cache
entries.
parent a5d21902
...@@ -1128,7 +1128,7 @@ async function renderToHTMLOrFlightImpl( ...@@ -1128,7 +1128,7 @@ async function renderToHTMLOrFlightImpl(
]) ])
} }
addImplicitTags(workStore, requestStore, undefined) addImplicitTags(workStore, requestStore, undefined, undefined)
if (workStore.tags) { if (workStore.tags) {
metadata.fetchTags = workStore.tags.join(',') metadata.fetchTags = workStore.tags.join(',')
...@@ -1237,7 +1237,7 @@ async function renderToHTMLOrFlightImpl( ...@@ -1237,7 +1237,7 @@ async function renderToHTMLOrFlightImpl(
]) ])
} }
addImplicitTags(workStore, requestStore, undefined) addImplicitTags(workStore, requestStore, undefined, undefined)
if (workStore.tags) { if (workStore.tags) {
metadata.fetchTags = workStore.tags.join(',') metadata.fetchTags = workStore.tags.join(',')
...@@ -1852,6 +1852,7 @@ async function prerenderToStream( ...@@ -1852,6 +1852,7 @@ async function prerenderToStream(
const cacheSignal = new CacheSignal() const cacheSignal = new CacheSignal()
const prospectiveRenderPrerenderStore: PrerenderStore = { const prospectiveRenderPrerenderStore: PrerenderStore = {
type: 'prerender', type: 'prerender',
pathname: ctx.requestStore.url.pathname,
cacheSignal, cacheSignal,
// During the prospective render we don't want to synchronously abort on dynamic access // During the prospective render we don't want to synchronously abort on dynamic access
// because it could prevent us from discovering all caches in siblings. So we omit the controller // because it could prevent us from discovering all caches in siblings. So we omit the controller
...@@ -1943,6 +1944,7 @@ async function prerenderToStream( ...@@ -1943,6 +1944,7 @@ async function prerenderToStream(
const finalRenderPrerenderStore: PrerenderStore = { const finalRenderPrerenderStore: PrerenderStore = {
type: 'prerender', type: 'prerender',
pathname: ctx.requestStore.url.pathname,
// During the final prerender we don't need to track cache access so we omit the signal // During the final prerender we don't need to track cache access so we omit the signal
cacheSignal: null, cacheSignal: null,
// During the final render we do want to abort synchronously on dynamic access so we // During the final render we do want to abort synchronously on dynamic access so we
...@@ -1998,6 +2000,7 @@ async function prerenderToStream( ...@@ -1998,6 +2000,7 @@ async function prerenderToStream(
const SSRController = new AbortController() const SSRController = new AbortController()
const ssrPrerenderStore: PrerenderStore = { const ssrPrerenderStore: PrerenderStore = {
type: 'prerender', type: 'prerender',
pathname: ctx.requestStore.url.pathname,
// For HTML Generation we don't need to track cache reads (RSC only) // For HTML Generation we don't need to track cache reads (RSC only)
cacheSignal: null, cacheSignal: null,
// We expect the SSR render to complete in a single Task and need to be able to synchronously abort // We expect the SSR render to complete in a single Task and need to be able to synchronously abort
...@@ -2202,6 +2205,7 @@ async function prerenderToStream( ...@@ -2202,6 +2205,7 @@ async function prerenderToStream(
const cacheSignal = new CacheSignal() const cacheSignal = new CacheSignal()
const prospectiveRenderPrerenderStore: PrerenderStore = { const prospectiveRenderPrerenderStore: PrerenderStore = {
type: 'prerender', type: 'prerender',
pathname: ctx.requestStore.url.pathname,
cacheSignal, cacheSignal,
// When PPR is off we can synchronously abort the prospective render because we will // When PPR is off we can synchronously abort the prospective render because we will
// always hit this path on the final render and thus we can skip the final render and just // always hit this path on the final render and thus we can skip the final render and just
...@@ -2280,6 +2284,7 @@ async function prerenderToStream( ...@@ -2280,6 +2284,7 @@ async function prerenderToStream(
const finalRenderPrerenderStore: PrerenderStore = { const finalRenderPrerenderStore: PrerenderStore = {
type: 'prerender', type: 'prerender',
pathname: ctx.requestStore.url.pathname,
// During the final prerender we don't need to track cache access so we omit the signal // During the final prerender we don't need to track cache access so we omit the signal
cacheSignal: null, cacheSignal: null,
controller: flightController, controller: flightController,
...@@ -2289,6 +2294,7 @@ async function prerenderToStream( ...@@ -2289,6 +2294,7 @@ async function prerenderToStream(
const SSRController = new AbortController() const SSRController = new AbortController()
const ssrPrerenderStore: PrerenderStore = { const ssrPrerenderStore: PrerenderStore = {
type: 'prerender', type: 'prerender',
pathname: ctx.requestStore.url.pathname,
// For HTML Generation we don't need to track cache reads (RSC only) // For HTML Generation we don't need to track cache reads (RSC only)
cacheSignal: null, cacheSignal: null,
// We expect the SSR render to complete in a single Task and need to be able to synchronously abort // We expect the SSR render to complete in a single Task and need to be able to synchronously abort
...@@ -2469,6 +2475,7 @@ async function prerenderToStream( ...@@ -2469,6 +2475,7 @@ async function prerenderToStream(
) )
const reactServerPrerenderStore: PrerenderStore = { const reactServerPrerenderStore: PrerenderStore = {
type: 'prerender', type: 'prerender',
pathname: ctx.requestStore.url.pathname,
cacheSignal: null, cacheSignal: null,
controller: null, controller: null,
dynamicTracking, dynamicTracking,
...@@ -2496,6 +2503,7 @@ async function prerenderToStream( ...@@ -2496,6 +2503,7 @@ async function prerenderToStream(
const ssrPrerenderStore: PrerenderStore = { const ssrPrerenderStore: PrerenderStore = {
type: 'prerender', type: 'prerender',
pathname: ctx.requestStore.url.pathname,
cacheSignal: null, cacheSignal: null,
controller: null, controller: null,
dynamicTracking, dynamicTracking,
...@@ -2653,6 +2661,7 @@ async function prerenderToStream( ...@@ -2653,6 +2661,7 @@ async function prerenderToStream(
} else { } else {
const prerenderLegacyStore: PrerenderStore = { const prerenderLegacyStore: PrerenderStore = {
type: 'prerender-legacy', type: 'prerender-legacy',
pathname: ctx.requestStore.url.pathname,
} }
// This is a regular static generation. We don't do dynamic tracking because we rely on // This is a regular static generation. We don't do dynamic tracking because we rely on
// the old-school dynamic error handling to bail out of static generation // the old-school dynamic error handling to bail out of static generation
......
...@@ -18,6 +18,7 @@ import { prerenderAsyncStorage } from './prerender-async-storage-instance' with ...@@ -18,6 +18,7 @@ import { prerenderAsyncStorage } from './prerender-async-storage-instance' with
*/ */
export type PrerenderStoreModern = { export type PrerenderStoreModern = {
type: 'prerender' type: 'prerender'
pathname: string | undefined
/** /**
* This is the AbortController passed to React. It can be used to abort the prerender * This is the AbortController passed to React. It can be used to abort the prerender
* if we encounter conditions that do not require further rendering * if we encounter conditions that do not require further rendering
...@@ -38,6 +39,7 @@ export type PrerenderStoreModern = { ...@@ -38,6 +39,7 @@ export type PrerenderStoreModern = {
export type PrerenderStoreLegacy = { export type PrerenderStoreLegacy = {
type: 'prerender-legacy' type: 'prerender-legacy'
pathname: string | undefined
} }
export type PrerenderStore = PrerenderStoreLegacy | PrerenderStoreModern export type PrerenderStore = PrerenderStoreLegacy | PrerenderStoreModern
......
...@@ -18,6 +18,7 @@ import type { ...@@ -18,6 +18,7 @@ import type {
RequestAsyncStorage, RequestAsyncStorage,
RequestStore, RequestStore,
} from '../../client/components/request-async-storage.external' } from '../../client/components/request-async-storage.external'
import type { PrerenderStore } from '../app-render/prerender-async-storage.external'
import { import {
cacheAsyncStorage, cacheAsyncStorage,
type CacheStore, type CacheStore,
...@@ -144,6 +145,7 @@ const getDerivedTags = (pathname: string): string[] => { ...@@ -144,6 +145,7 @@ const getDerivedTags = (pathname: string): string[] => {
export function addImplicitTags( export function addImplicitTags(
workStore: WorkStore, workStore: WorkStore,
requestStore: RequestStore | undefined, requestStore: RequestStore | undefined,
prerenderStore: PrerenderStore | undefined,
cacheStore: CacheStore | undefined cacheStore: CacheStore | undefined
) { ) {
const newTags: string[] = [] const newTags: string[] = []
...@@ -174,10 +176,17 @@ export function addImplicitTags( ...@@ -174,10 +176,17 @@ export function addImplicitTags(
newTags.push(tag) newTags.push(tag)
} }
const renderedPathname =
requestStore !== undefined
? requestStore.url.pathname
: prerenderStore !== undefined
? prerenderStore.pathname
: undefined
// Add the tags from the pathname. If the route has unknown params, we don't // Add the tags from the pathname. If the route has unknown params, we don't
// want to add the pathname as a tag, as it will be invalid. // want to add the pathname as a tag, as it will be invalid.
if (requestStore?.url.pathname && !hasFallbackRouteParams) { if (renderedPathname && !hasFallbackRouteParams) {
const tag = `${NEXT_CACHE_IMPLICIT_TAG_ID}${requestStore.url.pathname}` const tag = `${NEXT_CACHE_IMPLICIT_TAG_ID}${renderedPathname}`
if ( if (
!cacheStore || !cacheStore ||
(cacheStore.type !== 'cache' && cacheStore.type !== 'unstable-cache') (cacheStore.type !== 'cache' && cacheStore.type !== 'unstable-cache')
...@@ -263,6 +272,7 @@ export function createPatchedFetcher( ...@@ -263,6 +272,7 @@ export function createPatchedFetcher(
const hideSpan = process.env.NEXT_OTEL_FETCH_DISABLED === '1' const hideSpan = process.env.NEXT_OTEL_FETCH_DISABLED === '1'
const workStore = workAsyncStorage.getStore() const workStore = workAsyncStorage.getStore()
const prerenderStore = prerenderAsyncStorage.getStore()
const result = getTracer().trace( const result = getTracer().trace(
isInternal ? NextNodeServerSpan.internalFetch : AppRenderSpan.fetch, isInternal ? NextNodeServerSpan.internalFetch : AppRenderSpan.fetch,
...@@ -345,6 +355,7 @@ export function createPatchedFetcher( ...@@ -345,6 +355,7 @@ export function createPatchedFetcher(
const implicitTags = addImplicitTags( const implicitTags = addImplicitTags(
workStore, workStore,
requestStore, requestStore,
prerenderStore,
cacheStore cacheStore
) )
...@@ -772,7 +783,6 @@ export function createPatchedFetcher( ...@@ -772,7 +783,6 @@ export function createPatchedFetcher(
// We sometimes use the cache to dedupe fetches that do not specify a cache configuration // We sometimes use the cache to dedupe fetches that do not specify a cache configuration
// In these cases we want to make sure we still exclude them from prerenders if dynamicIO is on // In these cases we want to make sure we still exclude them from prerenders if dynamicIO is on
// so we introduce an artificial Task boundary here. // so we introduce an artificial Task boundary here.
const prerenderStore = prerenderAsyncStorage.getStore()
if (prerenderStore) { if (prerenderStore) {
await waitAtLeastOneReactRenderTask() await waitAtLeastOneReactRenderTask()
} }
...@@ -953,7 +963,6 @@ export function createPatchedFetcher( ...@@ -953,7 +963,6 @@ export function createPatchedFetcher(
} }
) )
const prerenderStore = prerenderAsyncStorage.getStore()
if ( if (
prerenderStore && prerenderStore &&
prerenderStore.type === 'prerender' && prerenderStore.type === 'prerender' &&
......
...@@ -336,6 +336,7 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -336,6 +336,7 @@ export class AppRouteRouteModule extends RouteModule<
let dynamicTracking = createDynamicTrackingState(undefined) let dynamicTracking = createDynamicTrackingState(undefined)
const prospectiveRoutePrerenderStore: PrerenderStore = { const prospectiveRoutePrerenderStore: PrerenderStore = {
type: 'prerender', type: 'prerender',
pathname: requestStore.url.pathname,
cacheSignal, cacheSignal,
// During prospective render we don't use a controller // During prospective render we don't use a controller
// because we need to let all caches fill. // because we need to let all caches fill.
...@@ -403,6 +404,7 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -403,6 +404,7 @@ export class AppRouteRouteModule extends RouteModule<
const finalRoutePrerenderStore: PrerenderStore = { const finalRoutePrerenderStore: PrerenderStore = {
type: 'prerender', type: 'prerender',
pathname: requestStore.url.pathname,
cacheSignal: null, cacheSignal: null,
controller, controller,
dynamicTracking, dynamicTracking,
...@@ -466,6 +468,7 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -466,6 +468,7 @@ export class AppRouteRouteModule extends RouteModule<
res = await prerenderAsyncStorage.run( res = await prerenderAsyncStorage.run(
{ {
type: 'prerender-legacy', type: 'prerender-legacy',
pathname: requestStore.url.pathname,
}, },
handler, handler,
request, request,
...@@ -522,7 +525,7 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -522,7 +525,7 @@ export class AppRouteRouteModule extends RouteModule<
...Object.values(workStore.pendingRevalidates || {}), ...Object.values(workStore.pendingRevalidates || {}),
]) ])
addImplicitTags(workStore, requestStore, undefined) addImplicitTags(workStore, requestStore, undefined, undefined)
;(context.renderOpts as any).fetchTags = workStore.tags?.join(',') ;(context.renderOpts as any).fetchTags = workStore.tags?.join(',')
// It's possible cookies were set in the handler, so we need // It's possible cookies were set in the handler, so we need
......
...@@ -196,6 +196,7 @@ export function unstable_cache<T extends Callback>( ...@@ -196,6 +196,7 @@ export function unstable_cache<T extends Callback>(
const implicitTags = addImplicitTags( const implicitTags = addImplicitTags(
workStore, workStore,
requestStore, requestStore,
prerenderStore,
cacheStore cacheStore
) )
...@@ -305,7 +306,8 @@ export function unstable_cache<T extends Callback>( ...@@ -305,7 +306,8 @@ export function unstable_cache<T extends Callback>(
// @TODO check on this API. addImplicitTags mutates the store and returns the implicit tags. The naming // @TODO check on this API. addImplicitTags mutates the store and returns the implicit tags. The naming
// of this function is potentially a little confusing // of this function is potentially a little confusing
const implicitTags = const implicitTags =
workStore && addImplicitTags(workStore, requestStore, cacheStore) workStore &&
addImplicitTags(workStore, requestStore, prerenderStore, cacheStore)
const cacheEntry = await incrementalCache.get(cacheKey, { const cacheEntry = await incrementalCache.get(cacheKey, {
kind: IncrementalCacheKind.FETCH, kind: IncrementalCacheKind.FETCH,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment