Skip to content

Commit f2ddda0

Browse files
committed
fix(pages): add Vary header to negotiated HTML responses
1 parent afcc6d0 commit f2ddda0

2 files changed

Lines changed: 88 additions & 23 deletions

File tree

‎packages/void/src/pages/protocol.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ function appendVary(headers: Headers, name: string): void {
109109
}
110110
}
111111

112+
function varyPagesResponse(c: CloudContext): void {
113+
const headers = new Headers(c.res.headers);
114+
appendVary(headers, 'X-VoidPages');
115+
c.header('Vary', headers.get('Vary')!);
116+
}
117+
112118
function escapeHtmlAttr(value: string): string {
113119
return value.replaceAll('&', '&amp;').replaceAll('"', '&quot;').replaceAll('<', '&lt;');
114120
}
@@ -464,6 +470,9 @@ export async function handlePageGet(
464470

465471
// Redirect/error responses from the loader are returned before rendering
466472
return runLoader(handler, c, async (props) => {
473+
if (renderMode !== 'island') {
474+
varyPagesResponse(c);
475+
}
467476
// Read at render time so per-route middleware can set them before next()
468477
const shared = (c.get('shared') as Record<string, unknown> | undefined) ?? {};
469478
const pageHead = headFn ? headFn(c, props) : undefined;
@@ -538,9 +547,6 @@ export async function handlePageGet(
538547
}
539548
// Stream NDJSON: first line is PageObject, subsequent lines are deferred groups
540549
c.header('X-VoidPages', 'true');
541-
const headers = new Headers(c.res.headers);
542-
appendVary(headers, 'X-VoidPages');
543-
c.header('Vary', headers.get('Vary')!);
544550
return streamDeferredResponse(
545551
JSON.stringify(pageObj) + '\n',
546552
deferredMap,
@@ -652,6 +658,7 @@ export async function handlePageMutation(
652658
async function respondWithPage(errors: Record<string, string> | null): Promise<Response> {
653659
try {
654660
return await runLoader(getHandler, c, async (props) => {
661+
varyPagesResponse(c);
655662
const pageObj = buildMutationPageObj(stripDeferred(props), errors);
656663
if (isPagesRequest(c)) {
657664
return pageJsonResponse(c, pageObj);

‎packages/void/test/unit/pages-protocol.test.ts‎

Lines changed: 78 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,64 @@ const mockRenderFn = vi.fn(async (pageObj: RenderPage) => `<html>${pageObj.compo
6767
const mockGetAssetTags = vi.fn(async () => ({ css: '', preloads: '', body: '' }));
6868

6969
describe('handlePageGet', () => {
70-
it.each([false, true])(
71-
'preserves loader Vary headers on Pages JSON (deferred=%s)',
72-
async (deferred) => {
73-
const c = await createTestContext('GET', '/users', { headers: { 'x-voidpages': 'true' } });
74-
const response = await handlePageGet(
75-
(ctx) => {
76-
ctx.header('Vary', 'Accept-Language');
77-
return deferred ? { users: defer(async () => []) } : { users: [] };
78-
},
79-
undefined,
80-
c,
81-
'users',
82-
mockRenderFn,
83-
mockGetAssetTags,
84-
);
85-
expect(response.headers.get('X-VoidPages')).toBe('true');
86-
expect(response.headers.get('Vary')).toBe('Accept-Language, X-VoidPages');
87-
await response.text();
88-
},
89-
);
70+
describe.each([
71+
{ name: 'HTML', spa: false, deferred: false, contentType: 'text/html; charset=utf-8' },
72+
{ name: 'JSON', spa: true, deferred: false, contentType: 'application/json' },
73+
{ name: 'streamed HTML', spa: false, deferred: true, contentType: 'text/html; charset=utf-8' },
74+
{ name: 'streamed NDJSON', spa: true, deferred: true, contentType: 'application/x-ndjson' },
75+
])('$name cache negotiation', ({ spa, deferred, contentType }) => {
76+
it.each([
77+
{ existing: undefined, expected: 'X-VoidPages' },
78+
{ existing: 'Accept-Language', expected: 'Accept-Language, X-VoidPages' },
79+
{ existing: 'Accept-Language, x-voidpages', expected: 'Accept-Language, x-voidpages' },
80+
{ existing: '*', expected: '*' },
81+
])(
82+
'sets Vary without losing or duplicating existing values ($existing)',
83+
async ({ existing, expected }) => {
84+
const app = new Hono<CloudEnv>();
85+
app.get('/users', (c) =>
86+
handlePageGet(
87+
(ctx) => {
88+
if (existing) {
89+
ctx.header('Vary', existing);
90+
}
91+
return deferred ? { users: defer(async () => []) } : { users: [] };
92+
},
93+
undefined,
94+
c,
95+
'users',
96+
mockRenderFn,
97+
mockGetAssetTags,
98+
),
99+
);
100+
const response = await app.request('/users', {
101+
headers: spa ? { 'x-voidpages': 'true' } : {},
102+
});
103+
// Consume streams even if a header assertion fails.
104+
await response.text();
105+
expect(response.headers.get('Content-Type')).toBe(contentType);
106+
expect(response.headers.get('X-VoidPages')).toBe(spa ? 'true' : null);
107+
expect(response.headers.get('Vary')).toBe(expected);
108+
},
109+
);
110+
});
111+
112+
it.each([false, true])('does not vary HTML-only island pages (deferred=%s)', async (deferred) => {
113+
const c = await createTestContext('GET', '/users', { headers: { 'x-voidpages': 'true' } });
114+
const response = await handlePageGet(
115+
() => (deferred ? { users: defer(async () => []) } : { users: [] }),
116+
undefined,
117+
c,
118+
'users',
119+
mockRenderFn,
120+
mockGetAssetTags,
121+
undefined,
122+
{ renderMode: 'island' },
123+
);
124+
await response.text();
125+
expect(response.headers.get('Content-Type')).toBe('text/html; charset=utf-8');
126+
expect(response.headers.get('Vary')).toBeNull();
127+
});
90128

91129
it('returns JSON page object when X-VoidPages header present', async () => {
92130
const c = await createTestContext('GET', '/users', {
@@ -523,6 +561,26 @@ describe('handlePageGet', () => {
523561
});
524562

525563
describe('handlePageMutation', () => {
564+
it.each([false, true])('varies re-rendered action pages by X-VoidPages (spa=%s)', async (spa) => {
565+
const c = await createTestContext('POST', '/users', {
566+
headers: spa ? { 'x-voidpages': 'true' } : {},
567+
});
568+
const response = await handlePageMutation(
569+
() => undefined,
570+
(ctx) => {
571+
ctx.header('Vary', 'Accept-Language');
572+
return { users: [] };
573+
},
574+
undefined,
575+
c,
576+
'users',
577+
mockRenderFn,
578+
mockGetAssetTags,
579+
);
580+
await response.text();
581+
expect(response.headers.get('Vary')).toBe('Accept-Language, X-VoidPages');
582+
});
583+
526584
it.each<Record<string, string>>([
527585
{ origin: 'https://attacker.test' },
528586
{ origin: 'http://sibling.localhost', 'sec-fetch-site': 'same-site' },

0 commit comments

Comments
 (0)