diff --git a/docs/guide/edge/revalidation.md b/docs/guide/edge/revalidation.md index 0370152e7..a993cd28e 100644 --- a/docs/guide/edge/revalidation.md +++ b/docs/guide/edge/revalidation.md @@ -109,6 +109,8 @@ Responses that vary by request headers, such as `Vary: Accept-Language` or `Vary Requests with `Cookie`, `Authorization`, or Cloudflare Access's `Cf-Access-Jwt-Assertion` header bypass the ISR cache entirely and always dispatch to the worker. Authenticated responses are not shared between users, including when Access authentication supplies no cookie. +A request that negotiates a representation other than the cached HTML document also bypasses the cache, because each path holds a single cached body. That covers `Accept: text/event-stream` and `Accept: text/markdown`, so middleware that answers those requests in another format runs on every request. + ## Cache keys and rewrites For a dispatch rewrite, the cache key includes both the destination and original pathname. A direct request to `/en/docs/foo` therefore uses a different entry from `/docs/foo` rewritten to that destination. diff --git a/packages/isr/src/core.ts b/packages/isr/src/core.ts index 3e48ddffd..e67b4b978 100644 --- a/packages/isr/src/core.ts +++ b/packages/isr/src/core.ts @@ -256,23 +256,31 @@ export function resolveRevalidateTtl(response: Response, defaultTtl: number): nu return Number.isNaN(value) || value < 0 ? defaultTtl : value; } -function acceptsEventStream(request: Request): boolean { - return ( - request.headers - .get('Accept') - ?.toLowerCase() - .split(',') - .some((part) => part.split(';', 1)[0]?.trim() === 'text/event-stream') ?? false - ); +/** + * Representations a single cached entry per path cannot hold: the entry stores + * one body, so a request that negotiates one of these reads must render live. + */ +const LIVE_ONLY_MEDIA_TYPES = ['text/event-stream', 'text/markdown'] as const; + +function negotiatesLiveOnlyMediaType(request: Request): boolean { + const accept = request.headers.get('Accept')?.toLowerCase(); + if (!accept) { + return false; + } + return accept + .split(',') + .some((part) => + LIVE_ONLY_MEDIA_TYPES.some((mediaType) => part.split(';', 1)[0]?.trim() === mediaType), + ); } -/** Return whether credentials or EventSource semantics require an ISR bypass. */ +/** Return whether credentials or negotiated media types require an ISR bypass. */ export function shouldBypassIsr(request: Request): boolean { return ( request.headers.has('Cookie') || request.headers.has('Authorization') || request.headers.has('Cf-Access-Jwt-Assertion') || - acceptsEventStream(request) + negotiatesLiveOnlyMediaType(request) ); } diff --git a/packages/void/test/unit/isr-cache.test.ts b/packages/void/test/unit/isr-cache.test.ts index 6b0c8f28c..e75835715 100644 --- a/packages/void/test/unit/isr-cache.test.ts +++ b/packages/void/test/unit/isr-cache.test.ts @@ -1134,6 +1134,53 @@ describe('handleIsr', () => { expect(tasks).toHaveLength(0); }); + it('renders a text/markdown negotiation live instead of replaying the cached HTML', async () => { + const { kv, store } = createKvMock(); + const env = makeEnv(kv); + const html = createRender( + () => new Response('page', { headers: { 'content-type': 'text/html' } }), + ); + const fill = createCtx(); + const miss = await handleIsr( + htmlRequest('/blog/post', { Accept: 'text/html' }), + env, + fill.ctx, + OPTS, + html.render, + ); + expect(miss?.headers.get('x-isr-cache')).toBe('MISS'); + await fill.flush(); + expect(store.size).toBe(1); + + // A markdown client asks for the same path. The HTML entry cannot answer it, + // and the live render must not replace that entry either. + const markdown = createRender( + () => + new Response('# page', { + headers: { + 'content-type': 'text/markdown; charset=utf-8', + 'cache-control': 'no-store', + vary: 'Accept', + }, + }), + ); + const live = createCtx(); + expect( + await handleIsr( + htmlRequest('/blog/post', { Accept: 'text/html, text/markdown' }), + env, + live.ctx, + OPTS, + markdown.render, + ), + ).toBeNull(); + expect(markdown.spy).not.toHaveBeenCalled(); + expect(live.tasks).toHaveLength(0); + + const entry = await readIsrCache(env, HOST, '/blog/post'); + expect(await new Response(entry!.body).text()).toBe('page'); + }); + it('returns null (caller renders live) for non-GET, credentialed, and non-ISR paths', async () => { const { kv } = createKvMock(); const env = makeEnv(kv); diff --git a/platform/packages/dispatch/test/unit/isr.test.ts b/platform/packages/dispatch/test/unit/isr.test.ts index f8c7dc5fd..eacddfd3a 100644 --- a/platform/packages/dispatch/test/unit/isr.test.ts +++ b/platform/packages/dispatch/test/unit/isr.test.ts @@ -539,6 +539,13 @@ describe('shouldBypassIsr', () => { expect(shouldBypassIsr(request)).toBe(true); }); + it('returns true when the request negotiates markdown', () => { + const request = new Request('https://example.com', { + headers: { Accept: 'text/markdown' }, + }); + expect(shouldBypassIsr(request)).toBe(true); + }); + it('returns false for plain GET request', () => { const request = new Request('https://example.com'); expect(shouldBypassIsr(request)).toBe(false);