Skip to content

Commit 7d34d61

Browse files
authored
Apply title templates to default error page titles. (#827)
Default 404 and 500 boundary titles were added after head resolution, so they bypassed `titleTemplate`. Apply project or middleware templates to those default titles while preserving explicit titles and the correct title when a failing 404 boundary escalates to a 500 boundary. Covers HTML responses, Pages navigation payloads, and browser tab titles across React, Vue, Svelte, and Solid. A custom error-page title API remains separate from this fix. Validation: - Seven default-title regression cases fail before the fix. - Focused boundary, head, and four-adapter integration tests pass independently. - Combined fix-batch verification: 13,338 affected tests, 17,009 full-suite tests with `vp test run --pool forks`, `vp run build:all`, `vp run check`, formatting, linting, and build-artifact checks. The default full-suite run hit a native Node 24/V8 WebAssembly JIT assertion (`end > addr`); forked-worker verification completed successfully. Refs #825.
1 parent 29d965e commit 7d34d61

3 files changed

Lines changed: 89 additions & 9 deletions

File tree

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -445,15 +445,11 @@ async function renderPageFailure(
445445
console.error(error);
446446
}
447447
const shared = (c.get('shared') as Record<string, unknown> | undefined) ?? {};
448+
const middlewareHead = c.get('headDefaults') as HeadConfig | undefined;
448449
let head: HeadDescriptor | undefined;
449450
// Invalid shared/head input is not repairable by trying another component.
450451
try {
451-
head = resolveRequestHead(
452-
c,
453-
configHead,
454-
c.get('headDefaults') as HeadConfig | undefined,
455-
undefined,
456-
);
452+
head = resolveRequestHead(c, configHead, middlewareHead, undefined);
457453
serializePageData({ shared, head });
458454
} catch (cause) {
459455
throw new Error('Unable to render Pages error boundary', { cause });
@@ -473,7 +469,11 @@ async function renderPageFailure(
473469
errors: null,
474470
head: {
475471
...head,
476-
title: head?.title ?? (status === 404 ? 'Not Found' : 'Internal Server Error'),
472+
title:
473+
head?.title ??
474+
resolveHead(configHead, middlewareHead, {
475+
title: status === 404 ? 'Not Found' : 'Internal Server Error',
476+
})?.title,
477477
},
478478
};
479479
try {

‎packages/void/test/integration/page-errors.test.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe.each(frameworks)('Pages errors with %s', (framework) => {
7575
);
7676
write(
7777
'void.config.ts',
78-
`import { defineConfig } from 'void/config'; export default defineConfig({target:'node'});`,
78+
`import { defineConfig } from 'void/config'; export default defineConfig({target:'node',head:{titleTemplate:'%s | Error tests'}});`,
7979
);
8080
write(
8181
'middleware/shared.ts',
@@ -313,15 +313,18 @@ describe.each(frameworks)('Pages errors with %s', (framework) => {
313313
expect(response.headers.get('X-Security')).toBe('retained');
314314
const text = await response.text();
315315
expect(text).not.toContain('SECRET_');
316+
const title = `${status === 404 ? 'Not Found' : 'Internal Server Error'} | Error tests`;
316317
if (pages) {
317318
expect(response.headers.get('X-VoidPages')).toBe('true');
318319
expect(deserializePageData(text)).toMatchObject({
319320
url: path,
320321
shared: { viewer: 'alice' },
321322
props: { error: { status } },
323+
head: { title },
322324
});
323325
} else {
324326
expect(text).toContain('data-layout="root"');
327+
expect(text).toContain(`<title>${title}</title>`);
325328
}
326329
}
327330
}
@@ -366,6 +369,11 @@ describe.each(frameworks)('Pages errors with %s', (framework) => {
366369
await expect
367370
.poll(() => page.locator('h1').textContent())
368371
.toBe(name === 'Returned' || name === 'Thrown' ? 'Missing page' : 'Error page');
372+
if (name !== 'Crash') {
373+
await expect
374+
.poll(() => page.title())
375+
.toBe(`${name === 'Unexpected' ? 'Internal Server Error' : 'Not Found'} | Error tests`);
376+
}
369377
expect(await page.evaluate(() => (window as unknown as { marker: boolean }).marker)).toBe(
370378
true,
371379
);

‎packages/void/test/unit/page-boundaries.test.ts‎

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ import { PAGES_PROTOCOL_VERSION } from '../../../edge/src/pages-protocol.ts';
88
import { createActionErrorFromResponse } from '../../src/pages/action-result.ts';
99
import { clientPageError } from '../../src/pages/boundary-client.ts';
1010
import { createBoundaryManifest } from '../../src/pages/boundary-plans.ts';
11+
import { renderHeadToString } from '../../src/pages/head.ts';
1112
import { generateComponentManifest } from '../../src/pages/manifest.ts';
1213
import { scanPages } from '../../src/pages/scan.ts';
1314
import { deserializePageData } from '../../src/pages/serialize.ts';
14-
import { defineHandler } from '../../src/runtime/handler.ts';
15+
import { defineHandler, type CloudEnv } from '../../src/runtime/handler.ts';
1516
import { createErrorRenderer } from '../../src/runtime/routing-errors.ts';
1617
import { createPageGetHandler, createPagesFallback } from '../../src/runtime/routing-pages.ts';
1718
import { createRoutingRuntime } from '../../src/runtime/routing.ts';
@@ -484,6 +485,77 @@ describe('loader not-found dispatch', () => {
484485
});
485486
});
486487

488+
describe.each([false, true])('boundary titles (Pages: %s)', (pages) => {
489+
it.each([
490+
{ status: 404, expected: 'Not Found | Site' },
491+
{ status: 500, expected: 'Internal Server Error | Site' },
492+
...(pages ? [] : [{ status: 404, escalate: true, expected: 'Internal Server Error | Site' }]),
493+
{ status: 404, configTitle: 'Configured', expected: 'Configured | Site' },
494+
{ status: 500, middlewareTitle: 'Custom error', expected: 'Custom error | Admin' },
495+
{ status: 404, middlewareTemplate: true, expected: 'Not Found | Admin' },
496+
])(
497+
'resolves $expected for status $status',
498+
async ({ status, expected, escalate, configTitle, middlewareTitle, middlewareTemplate }) => {
499+
const app = new Hono<CloudEnv>();
500+
app.use(async (c, next) => {
501+
if (middlewareTitle !== undefined || middlewareTemplate) {
502+
c.set('headDefaults', { title: middlewareTitle, titleTemplate: '%s | Admin' });
503+
}
504+
await next();
505+
});
506+
app.get(
507+
'/',
508+
createPageGetHandler({
509+
componentId: 'index',
510+
load: async () => ({
511+
loader: () => {
512+
if (status === 404) {
513+
return new Response(null, { status: 404 });
514+
}
515+
throw new Error('SECRET_FAILURE');
516+
},
517+
}),
518+
assets: async () => ({ css: '', body: '', preloads: '' }),
519+
headConfig: {
520+
title: configTitle,
521+
titleTemplate: '%s | Site',
522+
meta: [{ name: 'description', content: 'Site description' }],
523+
},
524+
renderOptions: { boundaries: { notFound: 'missing', errors: ['error'] } },
525+
render: async (page) => {
526+
if (escalate && page.component === 'missing') {
527+
throw new Error('SECRET_BOUNDARY_FAILURE');
528+
}
529+
return renderHeadToString(page.head!);
530+
},
531+
}),
532+
);
533+
const response = await app.request('https://app.test/', {
534+
headers: pages
535+
? {
536+
'X-VoidPages': 'true',
537+
'X-VoidPages-Version': PAGES_PROTOCOL_VERSION,
538+
}
539+
: {},
540+
});
541+
expect(response.status).toBe(escalate ? 500 : status);
542+
const body = await response.text();
543+
expect(body).not.toContain('SECRET_');
544+
if (pages) {
545+
expect(deserializePageData(body)).toMatchObject({
546+
head: {
547+
title: expected,
548+
meta: [{ name: 'description', content: 'Site description' }],
549+
},
550+
});
551+
} else {
552+
expect(body).toContain(`<title>${expected}</title>`);
553+
expect(body).toContain('Site description');
554+
}
555+
},
556+
);
557+
});
558+
487559
describe('boundary recovery failures', () => {
488560
it.each(['malformed JSON', 'body rejection', 'exhausted fallback'])(
489561
'handles %s without replaying the loader',

0 commit comments

Comments
 (0)