Unverified Commit 16bfce64 authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

[Segment Cache] Respond with 204 on cache miss (#73649)

Currently, when the client prefetches a segment, the server responds
with a 404 if it cannot fulfill the request. This updates it to respond
with a 204 No Content instead, since it's not an error for the client to
request a segment whose prefetch hasn't been generated.

When responding with 204, the server also sends the 'x-nextjs-postponed'
header, but only if PPR is enabled for the route. The client can use the
absence of this header to distinguish between a regular cache miss and a
miss due to PPR being disabled. In a later PR, I will update the client
to use this information to fall back to the non-PPR behavior.
parent c7d6ab7f
...@@ -500,8 +500,17 @@ async function fetchRouteOnCacheMiss( ...@@ -500,8 +500,17 @@ async function fetchRouteOnCacheMiss(
const nextUrl = key.nextUrl const nextUrl = key.nextUrl
try { try {
const response = await fetchSegmentPrefetchResponse(href, '/_tree', nextUrl) const response = await fetchSegmentPrefetchResponse(href, '/_tree', nextUrl)
if (!response || !response.ok || !response.body) { if (
// Received an unexpected response. !response ||
!response.ok ||
// 204 is a Cache miss. Though theoretically this shouldn't happen when
// PPR is enabled, because we always respond to route tree requests, even
// if it needs to be blockingly generated on demand.
response.status === 204 ||
!response.body
) {
// Server responded with an error, or with a miss. We should still cache
// the response, but we can try again after 10 seconds.
rejectRouteCacheEntry(entry, Date.now() + 10 * 1000) rejectRouteCacheEntry(entry, Date.now() + 10 * 1000)
return return
} }
...@@ -594,9 +603,14 @@ async function fetchSegmentEntryOnCacheMiss( ...@@ -594,9 +603,14 @@ async function fetchSegmentEntryOnCacheMiss(
accessToken === '' ? segmentPath : `${segmentPath}.${accessToken}`, accessToken === '' ? segmentPath : `${segmentPath}.${accessToken}`,
routeKey.nextUrl routeKey.nextUrl
) )
if (!response || !response.ok || !response.body) { if (
// Server responded with an error. We should still cache the response, but !response ||
// we can try again after 10 seconds. !response.ok ||
response.status === 204 || // Cache miss
!response.body
) {
// Server responded with an error, or with a miss. We should still cache
// the response, but we can try again after 10 seconds.
rejectSegmentCacheEntry(segmentCacheEntry, Date.now() + 10 * 1000) rejectSegmentCacheEntry(segmentCacheEntry, Date.now() + 10 * 1000)
return return
} }
......
...@@ -3044,49 +3044,55 @@ export default abstract class Server< ...@@ -3044,49 +3044,55 @@ export default abstract class Server<
} }
) )
if ( if (isPrefetchRSCRequest && typeof segmentPrefetchHeader === 'string') {
isRoutePPREnabled && // This is a prefetch request issued by the client Segment Cache. These
isPrefetchRSCRequest && // should never reach the application layer (lambda). We should either
typeof segmentPrefetchHeader === 'string' // respond from the cache (HIT) or respond with 204 No Content (MISS).
) { if (
if (cacheEntry?.value?.kind === CachedRouteKind.APP_PAGE) { cacheEntry !== null &&
// This is a prefetch request for an individual segment's static data. // This is always true at runtime but is needed to refine the type
// Unless the segment is fully dynamic, the data should have already been // of cacheEntry.value to CachedAppPageValue, because the outer
// loaded into the cache, when the page itself was generated. So we should // ResponseCacheEntry is not a discriminated union.
// always either return the cache entry. If no cache entry is available, cacheEntry.value?.kind === CachedRouteKind.APP_PAGE &&
// it's a 404 — either the segment is fully dynamic, or an invalid segment cacheEntry.value.segmentData
// path was requested. ) {
if (cacheEntry.value.segmentData) { const matchedSegment = cacheEntry.value.segmentData.get(
const matchedSegment = cacheEntry.value.segmentData.get( segmentPrefetchHeader
segmentPrefetchHeader )
) if (matchedSegment !== undefined) {
if (matchedSegment !== undefined) { // Cache hit
return { return {
type: 'rsc', type: 'rsc',
body: RenderResult.fromStatic(matchedSegment), body: RenderResult.fromStatic(matchedSegment),
// TODO: Eventually this should use revalidate time of the // TODO: Eventually this should use revalidate time of the
// individual segment, not the whole page. // individual segment, not the whole page.
revalidate: cacheEntry.revalidate, revalidate: cacheEntry.revalidate,
}
} }
} }
// If the segment is not found, return a 404. Since this is an RSC }
// request, there's no reason to render a 404 page; just return an
// empty response. // Cache miss. Either a cache entry for this route has not been generated,
res.statusCode = 404 // or there's no match for the requested segment. Regardless, respond with
return { // a 204 No Content. We don't bother to respond with 404 in cases where
type: 'rsc', // the segment does not exist, because these requests are only issued by
body: RenderResult.fromStatic(''), // the client cache.
revalidate: cacheEntry.revalidate, // TODO: If this is a request for the route tree (the special /_tree
} // segment), we should *always* respond with a tree, even if PPR
} else { // is disabled.
// Segment prefetches should never reach the application layer. If res.statusCode = 204
// there's no cache entry for this page, it's a 404. if (isRoutePPREnabled) {
res.statusCode = 404 // Set a header to indicate that PPR is enabled for this route. This
return { // lets the client distinguish between a regular cache miss and a cache
type: 'rsc', // miss due to PPR being disabled.
body: RenderResult.fromStatic(''), // NOTE: Theoretically, when PPR is enabled, there should *never* be
} // a cache miss because we should generate a fallback route. So this
// is mostly defensive.
res.setHeader(NEXT_DID_POSTPONE_HEADER, '1')
}
return {
type: 'rsc',
body: RenderResult.fromStatic(''),
revalidate: cacheEntry?.revalidate,
} }
} }
......
...@@ -64,9 +64,9 @@ describe('per segment prefetching', () => { ...@@ -64,9 +64,9 @@ describe('per segment prefetching', () => {
expect(childResponseText).toInclude('"rsc"') expect(childResponseText).toInclude('"rsc"')
}) })
it('respond with 404 if the segment does not have prefetch data', async () => { it('respond with 204 if the segment does not have prefetch data', async () => {
const response = await prefetch('/en', '/does-not-exist') const response = await prefetch('/en', '/does-not-exist')
expect(response.status).toBe(404) expect(response.status).toBe(204)
const responseText = await response.text() const responseText = await response.text()
expect(responseText.trim()).toBe('') expect(responseText.trim()).toBe('')
}) })
......
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
import Link from 'next/link'
export default function Page() {
return (
<ul>
<li>
<Link href="/ppr-enabled">Page with PPR enabled</Link>
</li>
<li>
<Link href="/ppr-enabled/dynamic-param">
Page with PPR enabled but has dynamic param
</Link>
</li>
<li>
<Link href="/ppr-disabled">Page with PPR disabled</Link>
</li>
</ul>
)
}
export default function PPRDisabled() {
return '(intentionally empty)'
}
export default function Page() {
return '(intentionally empty)'
}
export const experimental_ppr = true
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return children
}
export default function PPREnabled() {
return '(intentionally empty)'
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
experimental: {
ppr: 'incremental',
dynamicIO: true,
clientSegmentCache: true,
},
}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
describe('segment cache (incremental opt in)', () => {
const { next, isNextDev, skipped } = nextTestSetup({
files: __dirname,
skipDeployment: true,
})
if (isNextDev || skipped) {
test('ppr is disabled', () => {})
return
}
// TODO: Replace with e2e test once the client part is implemented
it('prefetch responds with 204 if PPR is disabled for a route', async () => {
await next.browser('/')
const response = await next.fetch('/ppr-disabled', {
headers: {
RSC: '1',
'Next-Router-Prefetch': '1',
'Next-Router-Segment-Prefetch': '/_tree',
},
})
expect(response.status).toBe(204)
})
})
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