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

fix: ensure dynamic fetches are tracked correctly (#66349)

This modifies the patched fetch implementation to better handle when
called within an `unstable_cache` callback. In these callbacks, it
should not throw an error related to dynamic access.

This replaces the verbose `trackDynamicFetch` instead with
`markCurrentScopeAsDynamic` which already has support for checking if
inside an unstable cache context. It also has been adjusted to be a
no-op when `export const dynamic = "force-static"`, further simplifying
the code within the patch fetch implementation.
parent deb60c65
...@@ -38,7 +38,7 @@ const { requestAsyncStorage, staticGenerationAsyncStorage, serverHooks } = ...@@ -38,7 +38,7 @@ const { requestAsyncStorage, staticGenerationAsyncStorage, serverHooks } =
const originalPathname = 'VAR_ORIGINAL_PATHNAME' const originalPathname = 'VAR_ORIGINAL_PATHNAME'
function patchFetch() { function patchFetch() {
return _patchFetch({ serverHooks, staticGenerationAsyncStorage }) return _patchFetch({ staticGenerationAsyncStorage })
} }
export { export {
......
...@@ -2,7 +2,9 @@ import stringHash from 'next/dist/compiled/string-hash' ...@@ -2,7 +2,9 @@ import stringHash from 'next/dist/compiled/string-hash'
import { formatServerError } from '../../lib/format-server-error' import { formatServerError } from '../../lib/format-server-error'
import { SpanStatusCode, getTracer } from '../lib/trace/tracer' import { SpanStatusCode, getTracer } from '../lib/trace/tracer'
import { isAbortError } from '../pipe-readable' import { isAbortError } from '../pipe-readable'
import { isDynamicUsageError } from '../../export/helpers/is-dynamic-usage-error' import { isBailoutToCSRError } from '../../shared/lib/lazy-dynamic/bailout-to-csr'
import { isNavigationSignalError } from '../../export/helpers/is-navigation-signal-error'
import { isDynamicServerError } from '../../client/components/hooks-server-context'
declare global { declare global {
var __next_log_error__: undefined | ((err: unknown) => void) var __next_log_error__: undefined | ((err: unknown) => void)
...@@ -57,13 +59,15 @@ export function createErrorHandler({ ...@@ -57,13 +59,15 @@ export function createErrorHandler({
if (allCapturedErrors) allCapturedErrors.push(err) if (allCapturedErrors) allCapturedErrors.push(err)
// These errors are expected. We return the digest
// so that they can be properly handled.
if (isDynamicUsageError(err)) return err.digest
// If the response was closed, we don't need to log the error. // If the response was closed, we don't need to log the error.
if (isAbortError(err)) return if (isAbortError(err)) return
// If we're bailing out to CSR, we don't need to log the error.
if (isBailoutToCSRError(err)) return err.digest
// If this is a navigation error, we don't need to log the error.
if (isNavigationSignalError(err)) return err.digest
if (!digestErrorsMap.has(digest)) { if (!digestErrorsMap.has(digest)) {
digestErrorsMap.set(digest, err) digestErrorsMap.set(digest, err)
} else if (source === ErrorHandlerSource.html) { } else if (source === ErrorHandlerSource.html) {
...@@ -72,6 +76,12 @@ export function createErrorHandler({ ...@@ -72,6 +76,12 @@ export function createErrorHandler({
err = digestErrorsMap.get(digest) err = digestErrorsMap.get(digest)
} }
// If this error occurs, we know that we should be stopping the static
// render. This is only thrown in static generation when PPR is not enabled,
// which causes the whole page to be marked as dynamic. We don't need to
// tell the user about this error, as it's not actionable.
if (isDynamicServerError(err)) return err.digest
// Format server errors in development to add more helpful error messages // Format server errors in development to add more helpful error messages
if (dev) { if (dev) {
formatServerError(err) formatServerError(err)
......
...@@ -76,17 +76,25 @@ export function markCurrentScopeAsDynamic( ...@@ -76,17 +76,25 @@ export function markCurrentScopeAsDynamic(
store: StaticGenerationStore, store: StaticGenerationStore,
expression: string expression: string
): void { ): void {
// inside cache scopes marking a scope as dynamic has no effect because the outer cache scope
// creates a cache boundary. This is subtly different from reading a dynamic data source which is
// forbidden inside a cache scope.
if (store.isUnstableCacheCallback) return
// If we're forcing dynamic rendering or we're forcing static rendering, we
// don't need to do anything here because the entire page is already dynamic
// or it's static and it should not throw or postpone here.
if (store.forceDynamic || store.forceStatic) return
const pathname = getPathname(store.urlPathname) const pathname = getPathname(store.urlPathname)
if (store.isUnstableCacheCallback) {
// inside cache scopes marking a scope as dynamic has no effect because the outer cache scope if (store.dynamicShouldError) {
// creates a cache boundary. This is subtly different from reading a dynamic data source which is
// forbidden inside a cache scope.
return
} else if (store.dynamicShouldError) {
throw new StaticGenBailoutError( throw new StaticGenBailoutError(
`Route ${pathname} with \`dynamic = "error"\` couldn't be rendered statically because it used \`${expression}\`. See more info here: https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering` `Route ${pathname} with \`dynamic = "error"\` couldn't be rendered statically because it used \`${expression}\`. See more info here: https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic#dynamic-rendering`
) )
} else if ( }
if (
// We are in a prerender (PPR enabled, during build) // We are in a prerender (PPR enabled, during build)
store.prerenderState store.prerenderState
) { ) {
...@@ -94,19 +102,19 @@ export function markCurrentScopeAsDynamic( ...@@ -94,19 +102,19 @@ export function markCurrentScopeAsDynamic(
// This will be used by the renderer to decide whether // This will be used by the renderer to decide whether
// the prerender requires a resume // the prerender requires a resume
postponeWithTracking(store.prerenderState, expression, pathname) postponeWithTracking(store.prerenderState, expression, pathname)
} else { }
store.revalidate = 0
if (store.isStaticGeneration) { store.revalidate = 0
// We aren't prerendering but we are generating a static page. We need to bail out of static generation
const err = new DynamicServerError(
`Route ${pathname} couldn't be rendered statically because it used ${expression}. See more info here: https://nextjs.org/docs/messages/dynamic-server-error`
)
store.dynamicUsageDescription = expression
store.dynamicUsageStack = err.stack
throw err if (store.isStaticGeneration) {
} // We aren't prerendering but we are generating a static page. We need to bail out of static generation
const err = new DynamicServerError(
`Route ${pathname} couldn't be rendered statically because it used ${expression}. See more info here: https://nextjs.org/docs/messages/dynamic-server-error`
)
store.dynamicUsageDescription = expression
store.dynamicUsageStack = err.stack
throw err
} }
} }
...@@ -172,21 +180,6 @@ export function Postpone({ ...@@ -172,21 +180,6 @@ export function Postpone({
postponeWithTracking(prerenderState, reason, pathname) postponeWithTracking(prerenderState, reason, pathname)
} }
// @TODO refactor patch-fetch and this function to better model dynamic semantics. Currently this implementation
// is too explicit about postponing if we are in a prerender and patch-fetch contains a lot of logic for determining
// what makes the fetch "dynamic". It also doesn't handle Non PPR cases so it is isn't as consistent with the other
// dynamic-rendering methods.
export function trackDynamicFetch(
store: StaticGenerationStore,
expression: string
) {
// If we aren't in a prerender, or we're in an unstable cache callback, we
// don't need to postpone.
if (!store.prerenderState || store.isUnstableCacheCallback) return
postponeWithTracking(store.prerenderState, expression, store.urlPathname)
}
function postponeWithTracking( function postponeWithTracking(
prerenderState: PrerenderState, prerenderState: PrerenderState,
expression: string, expression: string,
......
...@@ -44,7 +44,7 @@ function patchCacheScopeSupportIntoReact() { ...@@ -44,7 +44,7 @@ function patchCacheScopeSupportIntoReact() {
// patchFetch makes use of APIs such as `React.unstable_postpone` which are only available // patchFetch makes use of APIs such as `React.unstable_postpone` which are only available
// in the experimental channel of React, so export it from here so that it comes from the bundled runtime // in the experimental channel of React, so export it from here so that it comes from the bundled runtime
function patchFetch() { function patchFetch() {
return _patchFetch({ serverHooks, staticGenerationAsyncStorage }) return _patchFetch({ staticGenerationAsyncStorage })
} }
export { export {
......
...@@ -386,7 +386,6 @@ export class AppRouteRouteModule extends RouteModule< ...@@ -386,7 +386,6 @@ export class AppRouteRouteModule extends RouteModule<
async () => { async () => {
// Patch the global fetch. // Patch the global fetch.
patchFetch({ patchFetch({
serverHooks: this.serverHooks,
staticGenerationAsyncStorage: staticGenerationAsyncStorage:
this.staticGenerationAsyncStorage, this.staticGenerationAsyncStorage,
}) })
......
...@@ -2,7 +2,6 @@ import type { ...@@ -2,7 +2,6 @@ import type {
StaticGenerationAsyncStorage, StaticGenerationAsyncStorage,
StaticGenerationStore, StaticGenerationStore,
} from '../../client/components/static-generation-async-storage.external' } from '../../client/components/static-generation-async-storage.external'
import type * as ServerHooks from '../../client/components/hooks-server-context'
import { AppRenderSpan, NextNodeServerSpan } from './trace/constants' import { AppRenderSpan, NextNodeServerSpan } from './trace/constants'
import { getTracer, SpanKind } from './trace/tracer' import { getTracer, SpanKind } from './trace/tracer'
...@@ -13,7 +12,7 @@ import { ...@@ -13,7 +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 { trackDynamicFetch } from '../app-render/dynamic-rendering' import { markCurrentScopeAsDynamic } from '../app-render/dynamic-rendering'
import type { FetchMetric } from '../base-http' import type { FetchMetric } from '../base-http'
const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge' const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge'
...@@ -209,16 +208,12 @@ function trackFetchMetric( ...@@ -209,16 +208,12 @@ function trackFetchMetric(
} }
interface PatchableModule { interface PatchableModule {
serverHooks: typeof ServerHooks
staticGenerationAsyncStorage: StaticGenerationAsyncStorage staticGenerationAsyncStorage: StaticGenerationAsyncStorage
} }
function createPatchedFetcher( function createPatchedFetcher(
originFetch: Fetcher, originFetch: Fetcher,
{ { staticGenerationAsyncStorage }: PatchableModule
serverHooks: { DynamicServerError },
staticGenerationAsyncStorage,
}: PatchableModule
): PatchedFetcher { ): PatchedFetcher {
// Create the patched fetch function. We don't set the type here, as it's // Create the patched fetch function. We don't set the type here, as it's
// verified as the return value of this function. // verified as the return value of this function.
...@@ -482,7 +477,14 @@ function createPatchedFetcher( ...@@ -482,7 +477,14 @@ 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) {
trackDynamicFetch(staticGenerationStore, 'revalidate: 0') markCurrentScopeAsDynamic(
staticGenerationStore,
`revalidate: 0 fetch ${input}${
staticGenerationStore.urlPathname
? ` ${staticGenerationStore.urlPathname}`
: ''
}`
)
} }
staticGenerationStore.revalidate = finalRevalidate staticGenerationStore.revalidate = finalRevalidate
...@@ -700,28 +702,16 @@ function createPatchedFetcher( ...@@ -700,28 +702,16 @@ function createPatchedFetcher(
// Delete `cache` property as Cloudflare Workers will throw an error // Delete `cache` property as Cloudflare Workers will throw an error
if (isEdgeRuntime) delete init.cache if (isEdgeRuntime) delete init.cache
if (!staticGenerationStore.forceStatic && cache === 'no-store') { if (cache === 'no-store') {
const dynamicUsageReason = `no-store fetch ${input}${
staticGenerationStore.urlPathname
? ` ${staticGenerationStore.urlPathname}`
: ''
}`
// If enabled, we should bail out of static generation. // If enabled, we should bail out of static generation.
trackDynamicFetch(staticGenerationStore, dynamicUsageReason) markCurrentScopeAsDynamic(
staticGenerationStore,
// If partial prerendering is not enabled, then we should throw an `no-store fetch ${input}${
// error to indicate that this fetch is dynamic. staticGenerationStore.urlPathname
if (!staticGenerationStore.prerenderState) { ? ` ${staticGenerationStore.urlPathname}`
// PPR is not enabled, or React postpone is not available, we : ''
// should set the revalidate to 0. }`
staticGenerationStore.revalidate = 0 )
const err = new DynamicServerError(dynamicUsageReason)
staticGenerationStore.dynamicUsageErr = err
staticGenerationStore.dynamicUsageDescription = dynamicUsageReason
throw err
}
} }
const hasNextConfig = 'next' in init const hasNextConfig = 'next' in init
...@@ -732,29 +722,16 @@ function createPatchedFetcher( ...@@ -732,29 +722,16 @@ function createPatchedFetcher(
(typeof staticGenerationStore.revalidate === 'number' && (typeof staticGenerationStore.revalidate === 'number' &&
next.revalidate < staticGenerationStore.revalidate)) next.revalidate < staticGenerationStore.revalidate))
) { ) {
if ( if (next.revalidate === 0) {
!staticGenerationStore.forceDynamic &&
!staticGenerationStore.forceStatic &&
next.revalidate === 0
) {
const dynamicUsageReason = `revalidate: 0 fetch ${input}${
staticGenerationStore.urlPathname
? ` ${staticGenerationStore.urlPathname}`
: ''
}`
// If enabled, we should bail out of static generation. // If enabled, we should bail out of static generation.
trackDynamicFetch(staticGenerationStore, dynamicUsageReason) markCurrentScopeAsDynamic(
staticGenerationStore,
// If partial prerendering is not enabled, then we should throw an `revalidate: 0 fetch ${input}${
// error to indicate that this fetch is dynamic. staticGenerationStore.urlPathname
if (!staticGenerationStore.prerenderState) { ? ` ${staticGenerationStore.urlPathname}`
const err = new DynamicServerError(dynamicUsageReason) : ''
staticGenerationStore.dynamicUsageErr = err }`
staticGenerationStore.dynamicUsageDescription = )
dynamicUsageReason
throw err
}
} }
if (!staticGenerationStore.forceStatic || next.revalidate !== 0) { if (!staticGenerationStore.forceStatic || next.revalidate !== 0) {
......
...@@ -174,7 +174,7 @@ describe('app-dir static/dynamic handling', () => { ...@@ -174,7 +174,7 @@ describe('app-dir static/dynamic handling', () => {
it('should honor force-static with fetch cache: no-store correctly', async () => { it('should honor force-static with fetch cache: no-store correctly', async () => {
const res = await next.fetch('/force-static-fetch-no-store') const res = await next.fetch('/force-static-fetch-no-store')
expect(res.status).toBe(200) expect(res.status).toBe(200)
expect(res.headers.get('x-nextjs-cache').toLowerCase()).toBe('hit') expect(res.headers.get('x-nextjs-cache')?.toLowerCase()).toBe('hit')
}) })
} }
} }
...@@ -864,6 +864,14 @@ describe('app-dir static/dynamic handling', () => { ...@@ -864,6 +864,14 @@ describe('app-dir static/dynamic handling', () => {
"unstable-cache/dynamic-undefined/page_client-reference-manifest.js", "unstable-cache/dynamic-undefined/page_client-reference-manifest.js",
"unstable-cache/dynamic/page.js", "unstable-cache/dynamic/page.js",
"unstable-cache/dynamic/page_client-reference-manifest.js", "unstable-cache/dynamic/page_client-reference-manifest.js",
"unstable-cache/fetch/no-cache.html",
"unstable-cache/fetch/no-cache.rsc",
"unstable-cache/fetch/no-cache/page.js",
"unstable-cache/fetch/no-cache/page_client-reference-manifest.js",
"unstable-cache/fetch/no-store.html",
"unstable-cache/fetch/no-store.rsc",
"unstable-cache/fetch/no-store/page.js",
"unstable-cache/fetch/no-store/page_client-reference-manifest.js",
"variable-config-revalidate/revalidate-3.html", "variable-config-revalidate/revalidate-3.html",
"variable-config-revalidate/revalidate-3.rsc", "variable-config-revalidate/revalidate-3.rsc",
"variable-config-revalidate/revalidate-3/page.js", "variable-config-revalidate/revalidate-3/page.js",
...@@ -1612,6 +1620,38 @@ describe('app-dir static/dynamic handling', () => { ...@@ -1612,6 +1620,38 @@ describe('app-dir static/dynamic handling', () => {
"initialRevalidateSeconds": 50, "initialRevalidateSeconds": 50,
"srcRoute": "/strip-header-traceparent", "srcRoute": "/strip-header-traceparent",
}, },
"/unstable-cache/fetch/no-cache": {
"dataRoute": "/unstable-cache/fetch/no-cache.rsc",
"experimentalBypassFor": [
{
"key": "Next-Action",
"type": "header",
},
{
"key": "content-type",
"type": "header",
"value": "multipart/form-data;.*",
},
],
"initialRevalidateSeconds": false,
"srcRoute": "/unstable-cache/fetch/no-cache",
},
"/unstable-cache/fetch/no-store": {
"dataRoute": "/unstable-cache/fetch/no-store.rsc",
"experimentalBypassFor": [
{
"key": "Next-Action",
"type": "header",
},
{
"key": "content-type",
"type": "header",
"value": "multipart/form-data;.*",
},
],
"initialRevalidateSeconds": false,
"srcRoute": "/unstable-cache/fetch/no-store",
},
"/variable-config-revalidate/revalidate-3": { "/variable-config-revalidate/revalidate-3": {
"dataRoute": "/variable-config-revalidate/revalidate-3.rsc", "dataRoute": "/variable-config-revalidate/revalidate-3.rsc",
"experimentalBypassFor": [ "experimentalBypassFor": [
...@@ -3309,6 +3349,27 @@ describe('app-dir static/dynamic handling', () => { ...@@ -3309,6 +3349,27 @@ describe('app-dir static/dynamic handling', () => {
expect(data).toEqual(data2) expect(data).toEqual(data2)
expect(data).toEqual('typeof cachedData: undefined') expect(data).toEqual('typeof cachedData: undefined')
}) })
it.each(['no-store', 'no-cache'])(
'should not error when calling a fetch %s',
async (cache) => {
const browser = await next.browser(`/unstable-cache/fetch/${cache}`)
try {
const first = await browser.waitForElementByCss('#data').text()
expect(first).not.toBe('')
// Ensure the data is the same after 3 refreshes.
for (let i = 0; i < 3; i++) {
await browser.refresh()
const refreshed = await browser.waitForElementByCss('#data').text()
expect(refreshed).toEqual(first)
}
} finally {
await browser.close()
}
}
)
}) })
it('should keep querystring on static page', async () => { it('should keep querystring on static page', async () => {
......
import { unstable_cache } from 'next/cache'
import { Suspense } from 'react'
const getData = unstable_cache(
async (cache) => {
const res = await fetch(
'https://next-data-api-endpoint.vercel.app/api/random',
{ cache }
)
return res.text()
},
undefined,
{}
)
async function Data({ cache = undefined }) {
const data = await getData(cache)
return <div id="data">{data}</div>
}
export default function Page({ cache = undefined }) {
return (
<Suspense fallback={<div id="loading">Loading...</div>}>
<Data cache={cache} />
</Suspense>
)
}
import Page from '../fetch'
export default function UnstableCacheFetchNoStore() {
return <Page cache="no-cache" />
}
import Page from '../fetch'
export default function UnstableCacheFetchNoStore() {
return <Page cache="no-store" />
}
import { unstable_rethrow } from 'next/navigation'
const MAX_ATTEMPTS = 5 const MAX_ATTEMPTS = 5
export const fetchRetry = async (url, init) => { export const fetchRetry = async (url, init) => {
...@@ -5,12 +7,7 @@ export const fetchRetry = async (url, init) => { ...@@ -5,12 +7,7 @@ export const fetchRetry = async (url, init) => {
try { try {
return await fetch(url, init) return await fetch(url, init)
} catch (err) { } catch (err) {
// FIXME: (PPR) this is a workaround until we have a way to detect postpone errors unstable_rethrow(err)
// For PPR, we need to detect if this is a postpone error so we can
// re-throw it.
if (err.$$typeof === Symbol.for('react.postpone')) {
throw err
}
if (i === MAX_ATTEMPTS - 1) { if (i === MAX_ATTEMPTS - 1) {
throw err throw err
......
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