diff --git a/packages/void/src/pages/protocol.ts b/packages/void/src/pages/protocol.ts index d3b151928..e26472be9 100644 --- a/packages/void/src/pages/protocol.ts +++ b/packages/void/src/pages/protocol.ts @@ -445,15 +445,11 @@ async function renderPageFailure( console.error(error); } const shared = (c.get('shared') as Record | undefined) ?? {}; + const middlewareHead = c.get('headDefaults') as HeadConfig | undefined; let head: HeadDescriptor | undefined; // Invalid shared/head input is not repairable by trying another component. try { - head = resolveRequestHead( - c, - configHead, - c.get('headDefaults') as HeadConfig | undefined, - undefined, - ); + head = resolveRequestHead(c, configHead, middlewareHead, undefined); serializePageData({ shared, head }); } catch (cause) { throw new Error('Unable to render Pages error boundary', { cause }); @@ -473,7 +469,11 @@ async function renderPageFailure( errors: null, head: { ...head, - title: head?.title ?? (status === 404 ? 'Not Found' : 'Internal Server Error'), + title: + head?.title ?? + resolveHead(configHead, middlewareHead, { + title: status === 404 ? 'Not Found' : 'Internal Server Error', + })?.title, }, }; try { diff --git a/packages/void/test/integration/page-errors.test.ts b/packages/void/test/integration/page-errors.test.ts index 5e93e8403..a3e441d2e 100644 --- a/packages/void/test/integration/page-errors.test.ts +++ b/packages/void/test/integration/page-errors.test.ts @@ -75,7 +75,7 @@ describe.each(frameworks)('Pages errors with %s', (framework) => { ); write( 'void.config.ts', - `import { defineConfig } from 'void/config'; export default defineConfig({target:'node'});`, + `import { defineConfig } from 'void/config'; export default defineConfig({target:'node',head:{titleTemplate:'%s | Error tests'}});`, ); write( 'middleware/shared.ts', @@ -313,15 +313,18 @@ describe.each(frameworks)('Pages errors with %s', (framework) => { expect(response.headers.get('X-Security')).toBe('retained'); const text = await response.text(); expect(text).not.toContain('SECRET_'); + const title = `${status === 404 ? 'Not Found' : 'Internal Server Error'} | Error tests`; if (pages) { expect(response.headers.get('X-VoidPages')).toBe('true'); expect(deserializePageData(text)).toMatchObject({ url: path, shared: { viewer: 'alice' }, props: { error: { status } }, + head: { title }, }); } else { expect(text).toContain('data-layout="root"'); + expect(text).toContain(`${title}`); } } } @@ -366,6 +369,11 @@ describe.each(frameworks)('Pages errors with %s', (framework) => { await expect .poll(() => page.locator('h1').textContent()) .toBe(name === 'Returned' || name === 'Thrown' ? 'Missing page' : 'Error page'); + if (name !== 'Crash') { + await expect + .poll(() => page.title()) + .toBe(`${name === 'Unexpected' ? 'Internal Server Error' : 'Not Found'} | Error tests`); + } expect(await page.evaluate(() => (window as unknown as { marker: boolean }).marker)).toBe( true, ); diff --git a/packages/void/test/unit/page-boundaries.test.ts b/packages/void/test/unit/page-boundaries.test.ts index 0b8ca02dd..84dcacd5f 100644 --- a/packages/void/test/unit/page-boundaries.test.ts +++ b/packages/void/test/unit/page-boundaries.test.ts @@ -8,10 +8,11 @@ import { PAGES_PROTOCOL_VERSION } from '../../../edge/src/pages-protocol.ts'; import { createActionErrorFromResponse } from '../../src/pages/action-result.ts'; import { clientPageError } from '../../src/pages/boundary-client.ts'; import { createBoundaryManifest } from '../../src/pages/boundary-plans.ts'; +import { renderHeadToString } from '../../src/pages/head.ts'; import { generateComponentManifest } from '../../src/pages/manifest.ts'; import { scanPages } from '../../src/pages/scan.ts'; import { deserializePageData } from '../../src/pages/serialize.ts'; -import { defineHandler } from '../../src/runtime/handler.ts'; +import { defineHandler, type CloudEnv } from '../../src/runtime/handler.ts'; import { createErrorRenderer } from '../../src/runtime/routing-errors.ts'; import { createPageGetHandler, createPagesFallback } from '../../src/runtime/routing-pages.ts'; import { createRoutingRuntime } from '../../src/runtime/routing.ts'; @@ -484,6 +485,77 @@ describe('loader not-found dispatch', () => { }); }); +describe.each([false, true])('boundary titles (Pages: %s)', (pages) => { + it.each([ + { status: 404, expected: 'Not Found | Site' }, + { status: 500, expected: 'Internal Server Error | Site' }, + ...(pages ? [] : [{ status: 404, escalate: true, expected: 'Internal Server Error | Site' }]), + { status: 404, configTitle: 'Configured', expected: 'Configured | Site' }, + { status: 500, middlewareTitle: 'Custom error', expected: 'Custom error | Admin' }, + { status: 404, middlewareTemplate: true, expected: 'Not Found | Admin' }, + ])( + 'resolves $expected for status $status', + async ({ status, expected, escalate, configTitle, middlewareTitle, middlewareTemplate }) => { + const app = new Hono(); + app.use(async (c, next) => { + if (middlewareTitle !== undefined || middlewareTemplate) { + c.set('headDefaults', { title: middlewareTitle, titleTemplate: '%s | Admin' }); + } + await next(); + }); + app.get( + '/', + createPageGetHandler({ + componentId: 'index', + load: async () => ({ + loader: () => { + if (status === 404) { + return new Response(null, { status: 404 }); + } + throw new Error('SECRET_FAILURE'); + }, + }), + assets: async () => ({ css: '', body: '', preloads: '' }), + headConfig: { + title: configTitle, + titleTemplate: '%s | Site', + meta: [{ name: 'description', content: 'Site description' }], + }, + renderOptions: { boundaries: { notFound: 'missing', errors: ['error'] } }, + render: async (page) => { + if (escalate && page.component === 'missing') { + throw new Error('SECRET_BOUNDARY_FAILURE'); + } + return renderHeadToString(page.head!); + }, + }), + ); + const response = await app.request('https://app.test/', { + headers: pages + ? { + 'X-VoidPages': 'true', + 'X-VoidPages-Version': PAGES_PROTOCOL_VERSION, + } + : {}, + }); + expect(response.status).toBe(escalate ? 500 : status); + const body = await response.text(); + expect(body).not.toContain('SECRET_'); + if (pages) { + expect(deserializePageData(body)).toMatchObject({ + head: { + title: expected, + meta: [{ name: 'description', content: 'Site description' }], + }, + }); + } else { + expect(body).toContain(`${expected}`); + expect(body).toContain('Site description'); + } + }, + ); +}); + describe('boundary recovery failures', () => { it.each(['malformed JSON', 'body rejection', 'exhausted fallback'])( 'handles %s without replaying the loader',