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

Mark revalidate: 0 on fetch or use cache as never resolving promise in Dynamic I/O (#71285)

This marks it as dynamic but not "sync abort" since these are not sync
APIs.

A new heuristic here is that if the `expire` time is low, then we also
treat that as dynamic for the purpose of prerendering since it's not
worth including if it's supposed to change frequently but it is still
possible to temporarily cache it or client-cache it.
parent 4b9ef1eb
...@@ -116,7 +116,7 @@ export function getFirstDynamicReason( ...@@ -116,7 +116,7 @@ export function getFirstDynamicReason(
*/ */
export function markCurrentScopeAsDynamic( export function markCurrentScopeAsDynamic(
store: WorkStore, store: WorkStore,
workUnitStore: undefined | WorkUnitStore, workUnitStore: undefined | Exclude<WorkUnitStore, PrerenderStoreModern>,
expression: string expression: string
): void { ): void {
if (workUnitStore) { if (workUnitStore) {
...@@ -143,17 +143,7 @@ export function markCurrentScopeAsDynamic( ...@@ -143,17 +143,7 @@ export function markCurrentScopeAsDynamic(
} }
if (workUnitStore) { if (workUnitStore) {
if (workUnitStore.type === 'prerender') { if (workUnitStore.type === 'prerender-ppr') {
// We're prerendering the RSC stream with dynamicIO enabled and we need to abort the
// current render because something dynamic is being used.
// This won't throw so we still need to fall through to determine if/how we handle
// this specific dynamic request.
abortAndThrowOnSynchronousDynamicDataAccess(
store.route,
expression,
workUnitStore
)
} else if (workUnitStore.type === 'prerender-ppr') {
postponeWithTracking( postponeWithTracking(
store.route, store.route,
expression, expression,
......
...@@ -12,6 +12,7 @@ import { ...@@ -12,6 +12,7 @@ import {
NEXT_CACHE_TAG_MAX_LENGTH, NEXT_CACHE_TAG_MAX_LENGTH,
} from '../../lib/constants' } from '../../lib/constants'
import { markCurrentScopeAsDynamic } from '../app-render/dynamic-rendering' import { markCurrentScopeAsDynamic } from '../app-render/dynamic-rendering'
import { makeHangingPromise } from '../dynamic-rendering-utils'
import type { FetchMetric } from '../base-http' import type { FetchMetric } from '../base-http'
import { createDedupeFetch } from './dedupe-fetch' import { createDedupeFetch } from './dedupe-fetch'
import type { import type {
...@@ -185,6 +186,15 @@ export function createPatchedFetcher( ...@@ -185,6 +186,15 @@ export function createPatchedFetcher(
const workStore = workAsyncStorage.getStore() const workStore = workAsyncStorage.getStore()
const workUnitStore = workUnitAsyncStorage.getStore() const workUnitStore = workUnitAsyncStorage.getStore()
// During static generation we track cache reads so we can reason about when they fill
let cacheSignal =
workUnitStore && workUnitStore.type === 'prerender'
? workUnitStore.cacheSignal
: null
if (cacheSignal) {
cacheSignal.beginRead()
}
const result = getTracer().trace( const result = getTracer().trace(
isInternal ? NextNodeServerSpan.internalFetch : AppRenderSpan.fetch, isInternal ? NextNodeServerSpan.internalFetch : AppRenderSpan.fetch,
{ {
...@@ -373,6 +383,23 @@ export function createPatchedFetcher( ...@@ -373,6 +383,23 @@ export function createPatchedFetcher(
revalidateStore && revalidateStore &&
revalidateStore.revalidate === 0) revalidateStore.revalidate === 0)
if (
hasNoExplicitCacheConfig &&
workUnitStore !== undefined &&
workUnitStore.type === 'prerender'
) {
// If we have no cache config, and we're in Dynamic I/O prerendering, it'll be a dynamic call.
// We don't have to issue that dynamic call.
if (cacheSignal) {
cacheSignal.endRead()
cacheSignal = null
}
return makeHangingPromise<Response>(
workUnitStore.renderSignal,
'fetch()'
)
}
switch (pageFetchCacheMode) { switch (pageFetchCacheMode) {
case 'force-no-store': { case 'force-no-store': {
cacheReason = 'fetchCache = force-no-store' cacheReason = 'fetchCache = force-no-store'
...@@ -454,12 +481,23 @@ export function createPatchedFetcher( ...@@ -454,12 +481,23 @@ export function createPatchedFetcher(
// If we were setting the revalidate value to 0, we should try to // If we were setting the revalidate value to 0, we should try to
// postpone instead first. // postpone instead first.
if (finalRevalidate === 0) { if (finalRevalidate === 0) {
if (workUnitStore && workUnitStore.type === 'prerender') {
if (cacheSignal) {
cacheSignal.endRead()
cacheSignal = null
}
return makeHangingPromise<Response>(
workUnitStore.renderSignal,
'fetch()'
)
} else {
markCurrentScopeAsDynamic( markCurrentScopeAsDynamic(
workStore, workStore,
workUnitStore, workUnitStore,
`revalidate: 0 fetch ${input} ${workStore.route}` `revalidate: 0 fetch ${input} ${workStore.route}`
) )
} }
}
if (revalidateStore) { if (revalidateStore) {
revalidateStore.revalidate = finalRevalidate revalidateStore.revalidate = finalRevalidate
...@@ -576,7 +614,6 @@ export function createPatchedFetcher( ...@@ -576,7 +614,6 @@ export function createPatchedFetcher(
if (workUnitStore && workUnitStore.type === 'prerender') { if (workUnitStore && workUnitStore.type === 'prerender') {
// We are prerendering at build time or revalidate time with dynamicIO so we need to // We are prerendering at build time or revalidate time with dynamicIO so we need to
// buffer the response so we can guarantee it can be read in a microtask // buffer the response so we can guarantee it can be read in a microtask
const bodyBuffer = await res.arrayBuffer() const bodyBuffer = await res.arrayBuffer()
const fetchedData = { const fetchedData = {
...@@ -778,12 +815,23 @@ export function createPatchedFetcher( ...@@ -778,12 +815,23 @@ export function createPatchedFetcher(
if (cache === 'no-store') { if (cache === 'no-store') {
// If enabled, we should bail out of static generation. // If enabled, we should bail out of static generation.
if (workUnitStore && workUnitStore.type === 'prerender') {
if (cacheSignal) {
cacheSignal.endRead()
cacheSignal = null
}
return makeHangingPromise<Response>(
workUnitStore.renderSignal,
'fetch()'
)
} else {
markCurrentScopeAsDynamic( markCurrentScopeAsDynamic(
workStore, workStore,
workUnitStore, workUnitStore,
`no-store fetch ${input} ${workStore.route}` `no-store fetch ${input} ${workStore.route}`
) )
} }
}
const hasNextConfig = 'next' in init const hasNextConfig = 'next' in init
const { next = {} } = init const { next = {} } = init
...@@ -794,12 +842,19 @@ export function createPatchedFetcher( ...@@ -794,12 +842,19 @@ export function createPatchedFetcher(
) { ) {
if (next.revalidate === 0) { if (next.revalidate === 0) {
// If enabled, we should bail out of static generation. // If enabled, we should bail out of static generation.
if (workUnitStore && workUnitStore.type === 'prerender') {
return makeHangingPromise<Response>(
workUnitStore.renderSignal,
'fetch()'
)
} else {
markCurrentScopeAsDynamic( markCurrentScopeAsDynamic(
workStore, workStore,
workUnitStore, workUnitStore,
`revalidate: 0 fetch ${input} ${workStore.route}` `revalidate: 0 fetch ${input} ${workStore.route}`
) )
} }
}
if (!workStore.forceStatic || next.revalidate !== 0) { if (!workStore.forceStatic || next.revalidate !== 0) {
revalidateStore.revalidate = next.revalidate revalidateStore.revalidate = next.revalidate
...@@ -879,23 +934,17 @@ export function createPatchedFetcher( ...@@ -879,23 +934,17 @@ export function createPatchedFetcher(
} }
) )
if ( if (cacheSignal) {
workUnitStore &&
workUnitStore.type === 'prerender' &&
workUnitStore.cacheSignal
) {
// During static generation we track cache reads so we can reason about when they fill
const cacheSignal = workUnitStore.cacheSignal
cacheSignal.beginRead()
try { try {
return await result return await result
} finally { } finally {
if (cacheSignal) {
cacheSignal.endRead() cacheSignal.endRead()
} }
} else {
return result
} }
} }
return result
}
// Attach the necessary properties to the patched fetch function. // Attach the necessary properties to the patched fetch function.
// We don't use this to determine if the fetch function has been patched, // We don't use this to determine if the fetch function has been patched,
......
...@@ -21,6 +21,8 @@ import type { ...@@ -21,6 +21,8 @@ import type {
import { workUnitAsyncStorage } from '../app-render/work-unit-async-storage.external' import { workUnitAsyncStorage } from '../app-render/work-unit-async-storage.external'
import { runInCleanSnapshot } from '../app-render/clean-async-snapshot.external' import { runInCleanSnapshot } from '../app-render/clean-async-snapshot.external'
import { makeHangingPromise } from '../dynamic-rendering-utils'
import { cacheScopeAsyncLocalStorage } from '../async-storage/cache-scope.external' import { cacheScopeAsyncLocalStorage } from '../async-storage/cache-scope.external'
import type { ClientReferenceManifest } from '../../build/webpack/plugins/flight-manifest-plugin' import type { ClientReferenceManifest } from '../../build/webpack/plugins/flight-manifest-plugin'
...@@ -33,6 +35,9 @@ import type { CacheScopeStore } from '../async-storage/cache-scope.external' ...@@ -33,6 +35,9 @@ import type { CacheScopeStore } from '../async-storage/cache-scope.external'
const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge' const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge'
// If the expire time is less than .
const DYNAMIC_EXPIRE = 300
type CacheEntry = { type CacheEntry = {
value: ReadableStream value: ReadableStream
timestamp: number timestamp: number
...@@ -427,6 +432,20 @@ async function loadCacheEntry( ...@@ -427,6 +432,20 @@ async function loadCacheEntry(
const currentTime = performance.timeOrigin + performance.now() const currentTime = performance.timeOrigin + performance.now()
if ( if (
workUnitStore !== undefined &&
workUnitStore.type === 'prerender' &&
entry !== undefined &&
(entry.revalidate === 0 || entry.expire < DYNAMIC_EXPIRE)
) {
// In a Dynamic I/O prerender, if the cache entry has revalidate: 0 or if the
// expire time is under 5 minutes, then we consider this cache entry dynamic
// as it's not worth generating static pages for such data. It's better to leave
// a PPR hole that can be filled in dynamically with a potentially cached entry.
if (cacheSignal) {
cacheSignal.endRead()
}
return makeHangingPromise(workUnitStore.renderSignal, 'dynamic "use cache"')
} else if (
entry === undefined || entry === undefined ||
currentTime > entry.timestamp + entry.expire * 1000 || currentTime > entry.timestamp + entry.expire * 1000 ||
(workStore.isStaticGeneration && (workStore.isStaticGeneration &&
......
...@@ -30,6 +30,10 @@ export function unstable_noStore() { ...@@ -30,6 +30,10 @@ export function unstable_noStore() {
return return
} else { } else {
store.isUnstableNoStore = true store.isUnstableNoStore = true
if (workUnitStore && workUnitStore.type === 'prerender') {
// unstable_noStore() is a noop in Dynamic I/O.
} else {
markCurrentScopeAsDynamic(store, workUnitStore, callingExpression) markCurrentScopeAsDynamic(store, workUnitStore, callingExpression)
} }
}
} }
...@@ -288,8 +288,7 @@ describe('dynamic-io', () => { ...@@ -288,8 +288,7 @@ describe('dynamic-io', () => {
}) })
} }
if (WITH_PPR) { it('should fully prerender pages that use `unstable_noStore()`', async () => {
it('should partially prerender pages that use `unstable_noStore()`', async () => {
let $ = await next.render$('/cases/dynamic_api_no_store', {}) let $ = await next.render$('/cases/dynamic_api_no_store', {})
if (isNextDev) { if (isNextDev) {
expect($('#layout').text()).toBe('at runtime') expect($('#layout').text()).toBe('at runtime')
...@@ -301,20 +300,6 @@ describe('dynamic-io', () => { ...@@ -301,20 +300,6 @@ describe('dynamic-io', () => {
expect($('#inner').text()).toBe('at buildtime') expect($('#inner').text()).toBe('at buildtime')
} }
}) })
} else {
it('should not prerender pages that use `unstable_noStore()`', async () => {
let $ = await next.render$('/cases/dynamic_api_no_store', {})
if (isNextDev) {
expect($('#layout').text()).toBe('at runtime')
expect($('#page').text()).toBe('at runtime')
expect($('#inner').text()).toBe('at runtime')
} else {
expect($('#layout').text()).toBe('at runtime')
expect($('#page').text()).toBe('at runtime')
expect($('#inner').text()).toBe('at runtime')
}
})
}
if (WITH_PPR) { if (WITH_PPR) {
it('should partially prerender pages that use `searchParams` in Server Components', async () => { it('should partially prerender pages that use `searchParams` in Server Components', async () => {
...@@ -450,15 +435,9 @@ describe('dynamic-io', () => { ...@@ -450,15 +435,9 @@ describe('dynamic-io', () => {
expect($('#page-slot').text()).toBe('at runtime') expect($('#page-slot').text()).toBe('at runtime')
expect($('#page-children').text()).toBe('at runtime') expect($('#page-children').text()).toBe('at runtime')
} else { } else {
if (WITH_PPR) {
expect($('#layout').text()).toBe('at buildtime') expect($('#layout').text()).toBe('at buildtime')
expect($('#page-slot').text()).toBe('at runtime') expect($('#page-slot').text()).toBe('at buildtime')
expect($('#page-children').text()).toBe('at buildtime') expect($('#page-children').text()).toBe('at buildtime')
} else {
expect($('#layout').text()).toBe('at runtime')
expect($('#page-slot').text()).toBe('at runtime')
expect($('#page-children').text()).toBe('at runtime')
}
} }
$ = await next.render$('/cases/parallel/cookies', {}) $ = await next.render$('/cases/parallel/cookies', {})
......
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