Unverified Commit c05a1192 authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

PPR Fetch Fix (#57327)

parent 02fad829
import type { StaticGenerationStore } from './static-generation-async-storage.external'
export function maybePostpone(
staticGenerationStore: StaticGenerationStore,
reason: string
) {
// If we aren't performing a static generation or we aren't using PPR then
// we don't need to postpone.
if (
!staticGenerationStore.isStaticGeneration ||
!staticGenerationStore.experimental.ppr
) {
return
}
// App Route's cannot be postponed, so we only postpone if it's a page. If the
// postpone API is available, use it now.
const React = require('react') as typeof import('react')
if (typeof React.unstable_postpone !== 'function') return
React.unstable_postpone(reason)
}
import { DynamicServerError } from './hooks-server-context' import { DynamicServerError } from './hooks-server-context'
import { maybePostpone } from './maybe-postpone'
import { staticGenerationAsyncStorage } from './static-generation-async-storage.external' import { staticGenerationAsyncStorage } from './static-generation-async-storage.external'
class StaticGenBailoutError extends Error { class StaticGenBailoutError extends Error {
...@@ -25,28 +26,18 @@ export const staticGenerationBailout: StaticGenerationBailout = ( ...@@ -25,28 +26,18 @@ export const staticGenerationBailout: StaticGenerationBailout = (
opts opts
) => { ) => {
const staticGenerationStore = staticGenerationAsyncStorage.getStore() const staticGenerationStore = staticGenerationAsyncStorage.getStore()
if (!staticGenerationStore) return false
if (staticGenerationStore?.forceStatic) { if (staticGenerationStore.forceStatic) {
return true return true
} }
if (staticGenerationStore?.dynamicShouldError) { if (staticGenerationStore.dynamicShouldError) {
throw new StaticGenBailoutError( throw new StaticGenBailoutError(
formatErrorMessage(reason, { ...opts, dynamic: opts?.dynamic ?? 'error' }) formatErrorMessage(reason, { ...opts, dynamic: opts?.dynamic ?? 'error' })
) )
} }
if (staticGenerationStore && !staticGenerationStore?.experimental.ppr) {
staticGenerationStore.revalidate = 0
if (!opts?.dynamic) {
// we can statically prefetch pages that opt into dynamic,
// but not things like headers/cookies
staticGenerationStore.staticPrefetchBailout = true
}
}
if (staticGenerationStore?.isStaticGeneration) {
const message = formatErrorMessage(reason, { const message = formatErrorMessage(reason, {
...opts, ...opts,
// this error should be caught by Next to bail out of static generation // this error should be caught by Next to bail out of static generation
...@@ -54,16 +45,19 @@ export const staticGenerationBailout: StaticGenerationBailout = ( ...@@ -54,16 +45,19 @@ export const staticGenerationBailout: StaticGenerationBailout = (
link: 'https://nextjs.org/docs/messages/dynamic-server-error', link: 'https://nextjs.org/docs/messages/dynamic-server-error',
}) })
if (staticGenerationStore?.experimental.ppr) { maybePostpone(staticGenerationStore, message)
const React = require('react') as typeof import('react')
// App Route's cannot be postponed, so we only postpone if it's a page. // As this is a bailout, we don't want to revalidate, so set the revalidate
if (typeof React.unstable_postpone === 'function') { // to 0.
// This throws a postpone error similar to the below error. staticGenerationStore.revalidate = 0
React.unstable_postpone(message)
} if (!opts?.dynamic) {
// we can statically prefetch pages that opt into dynamic,
// but not things like headers/cookies
staticGenerationStore.staticPrefetchBailout = true
} }
if (staticGenerationStore.isStaticGeneration) {
const err = new DynamicServerError(message) const err = new DynamicServerError(message)
staticGenerationStore.dynamicUsageDescription = reason staticGenerationStore.dynamicUsageDescription = reason
staticGenerationStore.dynamicUsageStack = err.stack staticGenerationStore.dynamicUsageStack = err.stack
......
...@@ -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 * as Log from '../../build/output/log' import * as Log from '../../build/output/log'
import { maybePostpone } from '../../client/components/maybe-postpone'
const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge' const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge'
...@@ -70,13 +71,8 @@ const getDerivedTags = (pathname: string): string[] => { ...@@ -70,13 +71,8 @@ const getDerivedTags = (pathname: string): string[] => {
return derivedTags return derivedTags
} }
export function addImplicitTags( export function addImplicitTags(staticGenerationStore: StaticGenerationStore) {
staticGenerationStore: ReturnType<StaticGenerationAsyncStorage['getStore']>
) {
const newTags: string[] = [] const newTags: string[] = []
if (!staticGenerationStore) {
return newTags
}
const { pagePath, urlPathname } = staticGenerationStore const { pagePath, urlPathname } = staticGenerationStore
if (!Array.isArray(staticGenerationStore.tags)) { if (!Array.isArray(staticGenerationStore.tags)) {
...@@ -106,7 +102,7 @@ export function addImplicitTags( ...@@ -106,7 +102,7 @@ export function addImplicitTags(
} }
function trackFetchMetric( function trackFetchMetric(
staticGenerationStore: ReturnType<StaticGenerationAsyncStorage['getStore']>, staticGenerationStore: StaticGenerationStore,
ctx: { ctx: {
url: string url: string
status: number status: number
...@@ -276,11 +272,19 @@ export function patchFetch({ ...@@ -276,11 +272,19 @@ export function patchFetch({
if (_cache === 'force-cache') { if (_cache === 'force-cache') {
curRevalidate = false curRevalidate = false
} } else if (
if (['no-cache', 'no-store'].includes(_cache || '')) { _cache === 'no-cache' ||
_cache === 'no-store' ||
isForceNoStore ||
isOnlyNoStore
) {
curRevalidate = 0 curRevalidate = 0
}
if (_cache === 'no-cache' || _cache === 'no-store') {
cacheReason = `cache: ${_cache}` cacheReason = `cache: ${_cache}`
} }
if (typeof curRevalidate === 'number' || curRevalidate === false) { if (typeof curRevalidate === 'number' || curRevalidate === false) {
revalidate = curRevalidate revalidate = curRevalidate
} }
...@@ -306,7 +310,6 @@ export function patchFetch({ ...@@ -306,7 +310,6 @@ export function patchFetch({
staticGenerationStore.revalidate === 0 staticGenerationStore.revalidate === 0
if (isForceNoStore) { if (isForceNoStore) {
revalidate = 0
cacheReason = 'fetchCache = force-no-store' cacheReason = 'fetchCache = force-no-store'
} }
...@@ -320,7 +323,6 @@ export function patchFetch({ ...@@ -320,7 +323,6 @@ export function patchFetch({
`cache: 'force-cache' used on fetch for ${fetchUrl} with 'export const fetchCache = 'only-no-store'` `cache: 'force-cache' used on fetch for ${fetchUrl} with 'export const fetchCache = 'only-no-store'`
) )
} }
revalidate = 0
cacheReason = 'fetchCache = only-no-store' cacheReason = 'fetchCache = only-no-store'
} }
...@@ -370,6 +372,11 @@ export function patchFetch({ ...@@ -370,6 +372,11 @@ export function patchFetch({
(typeof staticGenerationStore.revalidate === 'number' && (typeof staticGenerationStore.revalidate === 'number' &&
revalidate < staticGenerationStore.revalidate)))) revalidate < staticGenerationStore.revalidate))))
) { ) {
// If enabled, we should bail out of static generation.
if (revalidate === 0) {
maybePostpone(staticGenerationStore, 'revalidate: 0')
}
staticGenerationStore.revalidate = revalidate staticGenerationStore.revalidate = revalidate
} }
...@@ -566,15 +573,17 @@ export function patchFetch({ ...@@ -566,15 +573,17 @@ export function patchFetch({
} }
} }
if (staticGenerationStore.isStaticGeneration) { if (
if (init && typeof init === 'object') { staticGenerationStore.isStaticGeneration &&
const cache = init.cache init &&
typeof init === 'object'
) {
const { cache } = init
// Delete `cache` property as Cloudflare Workers will throw an error // Delete `cache` property as Cloudflare Workers will throw an error
if (isEdgeRuntime) { if (isEdgeRuntime) delete init.cache
delete init.cache
}
if (cache === 'no-store') { if (cache === 'no-store') {
staticGenerationStore.revalidate = 0
const dynamicUsageReason = `no-store fetch ${input}${ const dynamicUsageReason = `no-store fetch ${input}${
staticGenerationStore.urlPathname staticGenerationStore.urlPathname
? ` ${staticGenerationStore.urlPathname}` ? ` ${staticGenerationStore.urlPathname}`
...@@ -584,10 +593,17 @@ export function patchFetch({ ...@@ -584,10 +593,17 @@ export function patchFetch({
staticGenerationStore.dynamicUsageErr = err staticGenerationStore.dynamicUsageErr = err
staticGenerationStore.dynamicUsageStack = err.stack staticGenerationStore.dynamicUsageStack = err.stack
staticGenerationStore.dynamicUsageDescription = dynamicUsageReason staticGenerationStore.dynamicUsageDescription = dynamicUsageReason
// If enabled, we should bail out of static generation.
maybePostpone(staticGenerationStore, dynamicUsageReason)
// PPR is not enabled, or React postpone is not available, we
// should set the revalidate to 0.
staticGenerationStore.revalidate = 0
} }
const hasNextConfig = 'next' in init const hasNextConfig = 'next' in init
const next = init.next || {} const { next = {} } = init
if ( if (
typeof next.revalidate === 'number' && typeof next.revalidate === 'number' &&
(typeof staticGenerationStore.revalidate === 'undefined' || (typeof staticGenerationStore.revalidate === 'undefined' ||
...@@ -596,14 +612,8 @@ export function patchFetch({ ...@@ -596,14 +612,8 @@ export function patchFetch({
) { ) {
const forceDynamic = staticGenerationStore.forceDynamic const forceDynamic = staticGenerationStore.forceDynamic
if (!forceDynamic || next.revalidate !== 0) {
staticGenerationStore.revalidate = next.revalidate
}
if (!forceDynamic && next.revalidate === 0) { if (!forceDynamic && next.revalidate === 0) {
const dynamicUsageReason = `revalidate: ${ const dynamicUsageReason = `revalidate: 0 fetch ${input}${
next.revalidate
} fetch ${input}${
staticGenerationStore.urlPathname staticGenerationStore.urlPathname
? ` ${staticGenerationStore.urlPathname}` ? ` ${staticGenerationStore.urlPathname}`
: '' : ''
...@@ -611,13 +621,19 @@ export function patchFetch({ ...@@ -611,13 +621,19 @@ export function patchFetch({
const err = new DynamicServerError(dynamicUsageReason) const err = new DynamicServerError(dynamicUsageReason)
staticGenerationStore.dynamicUsageErr = err staticGenerationStore.dynamicUsageErr = err
staticGenerationStore.dynamicUsageStack = err.stack staticGenerationStore.dynamicUsageStack = err.stack
staticGenerationStore.dynamicUsageDescription = staticGenerationStore.dynamicUsageDescription = dynamicUsageReason
dynamicUsageReason
// If enabled, we should bail out of static generation.
maybePostpone(staticGenerationStore, dynamicUsageReason)
}
if (!forceDynamic || next.revalidate !== 0) {
staticGenerationStore.revalidate = next.revalidate
} }
} }
if (hasNextConfig) delete init.next if (hasNextConfig) delete init.next
} }
}
return doOriginalFetch(false, cacheReasonOverride).finally(handleUnlock) return doOriginalFetch(false, cacheReasonOverride).finally(handleUnlock)
} }
......
...@@ -77,7 +77,7 @@ export function unstable_cache<T extends Callback>( ...@@ -77,7 +77,7 @@ export function unstable_cache<T extends Callback>(
} }
} }
} }
const implicitTags = addImplicitTags(store) const implicitTags = store ? addImplicitTags(store) : []
const cacheKey = await incrementalCache?.fetchCacheKey(joinedKey) const cacheKey = await incrementalCache?.fetchCacheKey(joinedKey)
const cacheEntry = const cacheEntry =
......
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