Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/i18n/ui/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@
"description": "The page you are looking for could not be found.",
"heading": "Page Not Found",
"message": "The page you are looking for does not exist or has been moved.",
"goHome": "Go to Home"
"goHome": "Go to Home",
"reportIssue": "Report an issue"
},
"common": {
"download": "Download"
Expand Down
59 changes: 53 additions & 6 deletions src/pages/404.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,75 @@
import Layout from '@/layouts/Layout.astro';
import { Icon } from 'astro-icon/components';
import { Container, H1, H2, Body, Button, Flex } from '@components/primitives';
import { defaultLang, languageCodes, languages, type LanguageCode } from '@/i18n/locales';
import { getHomePath, useTranslations } from '@/i18n/utils';

// The host serves this single `404.html` for every missing URL, whatever its
// language, so the page can't be localized at build time. It is rendered in
// English, and the inline script below switches it to the language of the
// requested URL (e.g. `/de/…`) using the strings collected here.
const translations = Object.fromEntries(
(languageCodes as LanguageCode[]).map((lang) => {
const t = useTranslations(lang);
return [
lang,
{
direction: languages[lang].direction,
title: t('error404.title'),
heading: t('error404.heading'),
message: t('error404.message'),
goHome: t('error404.goHome'),
reportIssue: t('error404.reportIssue'),
homePath: getHomePath(lang),
},
];
})
);
const strings = translations[defaultLang];
const description = useTranslations(defaultLang)('error404.description');
---

<Layout title="404 - Page Not Found" description="The page you are looking for could not be found.">
<Layout title={strings.title} description={description}>
<Container class="not-found">
<div>
<H1>404</H1>
<H2>Page Not Found</H2>
<Body color="secondary">The page you are looking for does not exist or has been moved.</Body>
<H2 data-not-found="heading">{strings.heading}</H2>
<Body color="secondary" data-not-found="message">
{strings.message}
</Body>
<Flex gap="4">
<Button as="a" href="/en" variant="primary">
Go to Home
<Button as="a" href={strings.homePath} variant="primary" data-not-found="goHome">
{strings.goHome}
</Button>
<Button
as="a"
href={'https://github.com/expressjs/expressjs.com/issues/new?template=4documentation.yml'}
>
<Icon name="fluent:warning-24-regular" size="20" />
Raise a issue
<span data-not-found="reportIssue">{strings.reportIssue}</span>
</Button>
</Flex>
</div>
</Container>
<script is:inline define:vars={{ translations, defaultLang }}>
// Runs as soon as the content above is parsed, so the localized text replaces
// the English text before the page is first painted.
const lang = location.pathname.split('/')[1];
if (lang !== defaultLang && Object.hasOwn(translations, lang)) {
const { direction, title, homePath, ...text } = translations[lang];

document.documentElement.lang = lang;
document.documentElement.dir = direction;
document.title = document.title.replace(translations[defaultLang].title, title);

// Each remaining string replaces the element tagged with the same key.
for (const [key, value] of Object.entries(text)) {
const el = document.querySelector(`[data-not-found="${key}"]`);
if (el) el.textContent = value;
}
document.querySelector('[data-not-found="goHome"]')?.setAttribute('href', homePath);
}
</script>
</Layout>

<style>
Expand Down
67 changes: 67 additions & 0 deletions tests/e2e/not-found.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { test, expect } from '@playwright/test';
import enStrings from '../../src/i18n/ui/en.json' with { type: 'json' };
import esStrings from '../../src/i18n/ui/es.json' with { type: 'json' };
import frStrings from '../../src/i18n/ui/fr.json' with { type: 'json' };
import deStrings from '../../src/i18n/ui/de.json' with { type: 'json' };
import itStrings from '../../src/i18n/ui/it.json' with { type: 'json' };
import jaStrings from '../../src/i18n/ui/ja.json' with { type: 'json' };
import koStrings from '../../src/i18n/ui/ko.json' with { type: 'json' };
import ptBrStrings from '../../src/i18n/ui/pt-br.json' with { type: 'json' };
import zhCnStrings from '../../src/i18n/ui/zh-cn.json' with { type: 'json' };
import zhTwStrings from '../../src/i18n/ui/zh-tw.json' with { type: 'json' };

const MISSING_PATH = 'this-page-does-not-exist';

const languagesToTest = [
{ code: 'es', strings: esStrings.error404 },
{ code: 'fr', strings: frStrings.error404 },
{ code: 'de', strings: deStrings.error404 },
{ code: 'it', strings: itStrings.error404 },
{ code: 'ja', strings: jaStrings.error404 },
{ code: 'ko', strings: koStrings.error404 },
{ code: 'pt-br', strings: ptBrStrings.error404 },
{ code: 'zh-cn', strings: zhCnStrings.error404 },
{ code: 'zh-tw', strings: zhTwStrings.error404 },
];

/**
* 404 Page E2E Tests
*
* A single 404 page is served for every missing URL, and it switches to the
* language of the requested URL on the client.
*/
test.describe('404 Page', () => {
// Paths without a supported language prefix (including removed locales) stay in English.
for (const path of [`/en/${MISSING_PATH}`, `/${MISSING_PATH}`, `/ru/guide/${MISSING_PATH}`]) {
test(`should show the English 404 page for ${path}`, async ({ page }) => {
const response = await page.goto(path);
expect(response?.status()).toBe(404);

await expect(page.locator('html')).toHaveAttribute('lang', 'en');
await expect(page).toHaveTitle(`${enStrings.error404.title} · Express.js`);
await expect(page.getByRole('heading', { name: enStrings.error404.heading })).toBeVisible();
await expect(page.getByText(enStrings.error404.message)).toBeVisible();
await expect(page.getByRole('link', { name: enStrings.error404.goHome })).toHaveAttribute(
'href',
'/'
);
await expect(page.getByRole('link', { name: enStrings.error404.reportIssue })).toBeVisible();
});
}

for (const { code, strings } of languagesToTest) {
test(`should show the 404 page in ${code} for /${code}/ URLs`, async ({ page }) => {
const response = await page.goto(`/${code}/guide/${MISSING_PATH}`);
expect(response?.status()).toBe(404);

await expect(page.locator('html')).toHaveAttribute('lang', code);
await expect(page).toHaveTitle(`${strings.title} · Express.js`);
await expect(page.getByRole('heading', { name: strings.heading })).toBeVisible();
await expect(page.getByText(strings.message)).toBeVisible();
await expect(page.getByRole('link', { name: strings.goHome })).toHaveAttribute(
'href',
`/${code}/`
);
});
}
});
Loading