Skip to content

fix: add debug information around potential html/rsc response mismatches #2816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/run/handlers/cache.cts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {
}
}
case 'APP_PAGE': {
const requestContext = getRequestContext()
if (requestContext && blob.value?.kind === 'APP_PAGE') {
requestContext.isCacheableAppPage = true
}

const { revalidate, rscData, ...restOfPageValue } = blob.value

span.addEvent(blob.value?.kind, { lastModified: blob.lastModified, revalidate, ttl })
Expand Down Expand Up @@ -410,6 +415,13 @@ export class NetlifyCacheHandler implements CacheHandlerForMultipleVersions {

await this.cacheStore.set(key, { lastModified, value }, 'blobStore.set')

if (data?.kind === 'APP_PAGE') {
const requestContext = getRequestContext()
if (requestContext) {
requestContext.isCacheableAppPage = true
}
}

if ((!data && !isDataReq) || data?.kind === 'PAGE' || data?.kind === 'PAGES') {
const requestContext = getRequestContext()
if (requestContext?.didPagesRouterOnDemandRevalidate) {
Expand Down
1 change: 1 addition & 0 deletions src/run/handlers/request-context.cts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export type RequestContext = {
backgroundWorkPromise: Promise<unknown>
logger: SystemLogger
requestID: string
isCacheableAppPage?: boolean
}

type RequestContextAsyncLocalStorage = AsyncLocalStorage<RequestContext>
Expand Down
42 changes: 36 additions & 6 deletions src/run/handlers/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { nextResponseProxy } from '../revalidate.js'
import { setFetchBeforeNextPatchedIt } from '../storage/storage.cjs'

import { getLogger, type RequestContext } from './request-context.cjs'
import { getTracer } from './tracer.cjs'
import { getTracer, recordWarning } from './tracer.cjs'
import { setupWaitUntil } from './wait-until.cjs'

setFetchBeforeNextPatchedIt(globalThis.fetch)
Expand Down Expand Up @@ -117,11 +117,6 @@ export default async (
const nextCache = response.headers.get('x-nextjs-cache')
const isServedFromNextCache = nextCache === 'HIT' || nextCache === 'STALE'

topLevelSpan.setAttributes({
'x-nextjs-cache': nextCache ?? undefined,
isServedFromNextCache,
})

if (isServedFromNextCache) {
await adjustDateHeader({
headers: response.headers,
Expand All @@ -136,6 +131,41 @@ export default async (
setVaryHeaders(response.headers, request, nextConfig)
setCacheStatusHeader(response.headers, nextCache)

const netlifyVary = response.headers.get('netlify-vary') ?? undefined
const netlifyCdnCacheControl = response.headers.get('netlify-cdn-cache-control') ?? undefined
topLevelSpan.setAttributes({
'x-nextjs-cache': nextCache ?? undefined,
isServedFromNextCache,
netlifyVary,
netlifyCdnCacheControl,
})

if (requestContext.isCacheableAppPage) {
const isRSCRequest = request.headers.get('rsc') === '1'
const contentType = response.headers.get('content-type') ?? undefined

const isExpectedContentType =
((isRSCRequest && contentType?.includes('text/x-component')) ||
(!isRSCRequest && contentType?.includes('text/html'))) ??
false

topLevelSpan.setAttributes({
isRSCRequest,
isCacheableAppPage: true,
contentType,
isExpectedContentType,
})

if (!isExpectedContentType) {
recordWarning(
new Error(
`Unexpected content type was produced for App Router page response (isRSCRequest: ${isRSCRequest}, contentType: ${contentType})`,
),
topLevelSpan,
)
}
}

async function waitForBackgroundWork() {
// it's important to keep the stream open until the next handler has finished
await nextHandlerPromise
Expand Down
Loading