Skip to content

Commit 971bed5

Browse files
authored
Keep bench's 404 title after the page loads (#200)
The server sent a bench 404 with the title "Page not found | Vortex". After hydration, the client set the route's own title instead. The catch-all route had no metadata, and the chart route returned none for an unknown chart, so the tab showed the layout's "Benchmarks | Vortex". Both routes now export the 404's metadata, from one module that not-found.tsx also uses. A smoke test checks the title on both routes after hydration. Signed-off-by: Will Manning <will@willmanning.io>
1 parent 32ea700 commit 971bed5

6 files changed

Lines changed: 58 additions & 16 deletions

File tree

‎src/app/(bench)/bench/[...notFound]/page.tsx‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4+
import type { Metadata } from 'next';
45
import { notFound } from 'next/navigation';
56

7+
import { NOT_FOUND_METADATA } from '@/bench/lib/not-found-metadata';
8+
9+
// The 404's title after hydration too, not the layout's (see NOT_FOUND_METADATA).
10+
export const metadata: Metadata = NOT_FOUND_METADATA;
11+
612
/**
713
* Every bench URL no other bench route matches. The app has two root layouts,
814
* and an unmatched URL gets the marketing site's 404 (src/app/global-not-found.tsx).

‎src/app/(bench)/bench/chart/[slug]/page.test.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ vi.mock('@/bench/lib/queries', () => ({
6565
vi.mock('@/bench/components/Chart', () => ({ Chart: () => null }));
6666

6767
const { generateMetadata } = await import('@/app/(bench)/bench/chart/[slug]/page');
68+
const { NOT_FOUND_METADATA } = await import('@/bench/lib/not-found-metadata');
6869

6970
type Query = Record<string, string | string[] | undefined>;
7071

@@ -84,6 +85,9 @@ async function metadata(slug: string, query: Query = {}): Promise<Redirect | obj
8485
}
8586

8687
const permanent = (to: string) => new Redirect('permanent', to);
88+
// The fake has no chart, so a request that is not redirected gets the 404's
89+
// metadata, as the page 404s.
90+
const served = NOT_FOUND_METADATA;
8791
const temporary = (to: string) => new Redirect('temporary', to);
8892

8993
afterEach(() => {
@@ -93,13 +97,13 @@ afterEach(() => {
9397

9498
describe('the chart page redirect', () => {
9599
it('serves the default window as sent, whatever else the query holds', async () => {
96-
expect(await metadata('ok', { utm_source: 'x' })).toEqual({});
100+
expect(await metadata('ok', { utm_source: 'x' })).toEqual(served);
97101
expect(cachedDefaultChartPayload).toHaveBeenCalledWith('ok');
98102
expect(cachedFilterUniverse).not.toHaveBeenCalled();
99103
});
100104

101105
it('serves a canonical non-default window without a redirect', async () => {
102-
expect(await metadata('ok', { n: 'all', engine: 'duckdb' })).toEqual({});
106+
expect(await metadata('ok', { n: 'all', engine: 'duckdb' })).toEqual(served);
103107
expect(chartPayload).toHaveBeenCalledWith(expect.anything(), { kind: 'all' });
104108
});
105109

@@ -137,12 +141,12 @@ describe('the chart page redirect', () => {
137141
});
138142

139143
it('treats a repeated parameter as absent, as the page does', async () => {
140-
expect(await metadata('ok', { n: ['all', '100'] })).toEqual({});
144+
expect(await metadata('ok', { n: ['all', '100'] })).toEqual(served);
141145
expect(cachedDefaultChartPayload).toHaveBeenCalledWith('ok');
142146
});
143147

144148
it('leaves a malformed slug to the 404, without a redirect', async () => {
145-
expect(await metadata('bad', { n: 'all' })).toEqual({});
149+
expect(await metadata('bad', { n: 'all' })).toEqual(served);
146150
expect(chartPayload).not.toHaveBeenCalled();
147151
expect(cachedDefaultChartPayload).not.toHaveBeenCalled();
148152
});

‎src/app/(bench)/bench/chart/[slug]/page.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
singleSearchParam,
1717
} from '@/bench/lib/chart-format';
1818
import { cachedDefaultChartPayload, cachedFilterUniverse } from '@/bench/lib/data-cache';
19+
import { NOT_FOUND_METADATA } from '@/bench/lib/not-found-metadata';
1920
import { chartPayload, type ChartResponse } from '@/bench/lib/queries';
2021
import { chartDescription, chartUrl, openGraph } from '@/bench/lib/site-files';
2122
import { chartKeyFromSlug, chartKeyToSlug, type ChartKey } from '@/bench/lib/slug';
@@ -131,7 +132,8 @@ export async function generateMetadata({
131132
await redirectToCanonical(slug, sp);
132133
const chart = await getChart(slug, singleSearchParam(sp.n));
133134
if (chart === null) {
134-
return {};
135+
// The page 404s: keep the 404's title after hydration (see NOT_FOUND_METADATA).
136+
return NOT_FOUND_METADATA;
135137
}
136138
const key = chartKeyFromSlug(slug);
137139
const title = `${chart.display_name} | Vortex`;

‎src/app/(bench)/bench/not-found.tsx‎

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,10 @@
44
import type { Metadata } from 'next';
55

66
import { BenchHeader } from '@/bench/components/BenchHeader';
7+
import { NOT_FOUND_METADATA } from '@/bench/lib/not-found-metadata';
78
import { NotFoundStatus } from '@/components/not-found';
89

9-
const TITLE = 'Page not found | Vortex';
10-
const SENTENCE = 'There is no page at this address.';
11-
12-
// The site's 404 metadata (src/app/(site)/not-found.tsx). The Open Graph
13-
// object replaces the layout's, so the page has no og:url: a missing page is
14-
// not bench's home.
15-
export const metadata: Metadata = {
16-
title: TITLE,
17-
description: SENTENCE,
18-
openGraph: { title: TITLE, description: SENTENCE, siteName: 'Vortex', type: 'website' },
19-
};
10+
export const metadata: Metadata = NOT_FOUND_METADATA;
2011

2112
/**
2213
* bench's 404, for the paths the catch-all route hands to `notFound()`: the
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
import type { Metadata } from 'next';
5+
6+
const TITLE = 'Page not found | Vortex';
7+
const SENTENCE = 'There is no page at this address.';
8+
9+
/**
10+
* bench's 404 metadata, the site's (src/app/(site)/not-found.tsx). The Open
11+
* Graph object replaces the layout's, so the page has no og:url: a missing
12+
* page is not bench's home.
13+
*
14+
* not-found.tsx exports it, and so does every route that calls `notFound()`.
15+
* The server renders not-found.tsx's metadata, but after hydration the client
16+
* sets the route's own, so a route without it showed the layout's title,
17+
* "Benchmarks | Vortex", on its 404.
18+
*/
19+
export const NOT_FOUND_METADATA: Metadata = {
20+
title: TITLE,
21+
description: SENTENCE,
22+
openGraph: { title: TITLE, description: SENTENCE, siteName: 'Vortex', type: 'website' },
23+
};

‎tests/smoke.spec.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,22 @@ test.describe("404", () => {
751751
page.getByRole("heading", { level: 1, name: "404" })
752752
).toBeVisible();
753753
});
754+
755+
// 7 Nov 2026: the server sent the 404's title, and then the client set the
756+
// route's own title, which was bench's layout's "Benchmarks | Vortex". Both
757+
// routes that call notFound() must give the 404's title after hydration.
758+
for (const path of ["/bench/no-such-page", "/bench/chart/not-a-chart"]) {
759+
test(`${path} keeps the 404 title after it loads`, async ({ page }) => {
760+
const res = await page.goto(path);
761+
expect(res?.status()).toBe(404);
762+
await page.waitForLoadState("networkidle");
763+
await expect(page).toHaveTitle("Page not found | Vortex");
764+
// Hydration has run when the theme bootstrap's class is gone.
765+
await expect(page.locator("html")).toHaveClass(/\bjs\b/);
766+
await page.waitForTimeout(1000);
767+
await expect(page).toHaveTitle("Page not found | Vortex");
768+
});
769+
}
754770
});
755771

756772
// Note 56: under the blog a 404 follows the reader's theme, and the mark

0 commit comments

Comments
 (0)