From cce1d0cc22e4f22c6eb0553ce87df16cdeb17edf Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Mon, 28 Oct 2024 14:44:35 +0100 Subject: [PATCH 1/4] feat: add test fixture --- .../i18n-routing-404/astro.config.mjs | 18 +++ .../fixtures/i18n-routing-404/package.json | 8 ++ .../src/components/Card.astro | 61 ++++++++ .../i18n-routing-404/src/i18n/locales.ts | 11 ++ .../i18n-routing-404/src/layouts/Layout.astro | 50 +++++++ .../i18n-routing-404/src/pages/404.astro | 4 + .../src/pages/[lang]/404.astro | 14 ++ .../src/pages/[lang]/index.astro | 130 ++++++++++++++++++ .../i18n-routing-404/src/pages/index.astro | 123 +++++++++++++++++ pnpm-lock.yaml | 7 + 10 files changed, 426 insertions(+) create mode 100644 packages/astro/test/fixtures/i18n-routing-404/astro.config.mjs create mode 100644 packages/astro/test/fixtures/i18n-routing-404/package.json create mode 100644 packages/astro/test/fixtures/i18n-routing-404/src/components/Card.astro create mode 100644 packages/astro/test/fixtures/i18n-routing-404/src/i18n/locales.ts create mode 100644 packages/astro/test/fixtures/i18n-routing-404/src/layouts/Layout.astro create mode 100644 packages/astro/test/fixtures/i18n-routing-404/src/pages/404.astro create mode 100644 packages/astro/test/fixtures/i18n-routing-404/src/pages/[lang]/404.astro create mode 100644 packages/astro/test/fixtures/i18n-routing-404/src/pages/[lang]/index.astro create mode 100644 packages/astro/test/fixtures/i18n-routing-404/src/pages/index.astro diff --git a/packages/astro/test/fixtures/i18n-routing-404/astro.config.mjs b/packages/astro/test/fixtures/i18n-routing-404/astro.config.mjs new file mode 100644 index 000000000000..0df2d119941c --- /dev/null +++ b/packages/astro/test/fixtures/i18n-routing-404/astro.config.mjs @@ -0,0 +1,18 @@ +import { defineConfig } from "astro/config"; + +export default defineConfig({ + i18n: { + defaultLocale: 'en', + locales: ['en', 'ja', 'ko', 'zh'], + routing: { + prefixDefaultLocale: true, + fallbackType: 'rewrite', + redirectToDefaultLocale: true, + }, + fallback: { + ja: 'en', + ko: 'en', + zh: 'en', + }, + }, +}) diff --git a/packages/astro/test/fixtures/i18n-routing-404/package.json b/packages/astro/test/fixtures/i18n-routing-404/package.json new file mode 100644 index 000000000000..bc34c4dc5e42 --- /dev/null +++ b/packages/astro/test/fixtures/i18n-routing-404/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/i18n-routing-404", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/i18n-routing-404/src/components/Card.astro b/packages/astro/test/fixtures/i18n-routing-404/src/components/Card.astro new file mode 100644 index 000000000000..bd6d5971ebf3 --- /dev/null +++ b/packages/astro/test/fixtures/i18n-routing-404/src/components/Card.astro @@ -0,0 +1,61 @@ +--- +interface Props { + title: string; + body: string; + href: string; +} + +const { href, title, body } = Astro.props; +--- + + + diff --git a/packages/astro/test/fixtures/i18n-routing-404/src/i18n/locales.ts b/packages/astro/test/fixtures/i18n-routing-404/src/i18n/locales.ts new file mode 100644 index 000000000000..089aaf5731aa --- /dev/null +++ b/packages/astro/test/fixtures/i18n-routing-404/src/i18n/locales.ts @@ -0,0 +1,11 @@ +export type Locales = Record<'en' | 'zh' | 'ja' | 'ko', string>; + +export const LOCALE_NAMES: Locales = { + en: 'English', + zh: '中文', + ja: '日本語', + ko: '한국어', +}; + +export const KNOWN_LANGUAGE_CODES = Object.keys(LOCALE_NAMES); +export const defaultLang = 'en'; diff --git a/packages/astro/test/fixtures/i18n-routing-404/src/layouts/Layout.astro b/packages/astro/test/fixtures/i18n-routing-404/src/layouts/Layout.astro new file mode 100644 index 000000000000..181097125d82 --- /dev/null +++ b/packages/astro/test/fixtures/i18n-routing-404/src/layouts/Layout.astro @@ -0,0 +1,50 @@ +--- +interface Props { + title: string; +} + +const { title } = Astro.props; +--- + + + + + + + + + + {title} + + + + + + diff --git a/packages/astro/test/fixtures/i18n-routing-404/src/pages/404.astro b/packages/astro/test/fixtures/i18n-routing-404/src/pages/404.astro new file mode 100644 index 000000000000..fe4587ad67af --- /dev/null +++ b/packages/astro/test/fixtures/i18n-routing-404/src/pages/404.astro @@ -0,0 +1,4 @@ +
+

404

+

You're in the wrong part of town.

+
\ No newline at end of file diff --git a/packages/astro/test/fixtures/i18n-routing-404/src/pages/[lang]/404.astro b/packages/astro/test/fixtures/i18n-routing-404/src/pages/[lang]/404.astro new file mode 100644 index 000000000000..04b2601ef7e2 --- /dev/null +++ b/packages/astro/test/fixtures/i18n-routing-404/src/pages/[lang]/404.astro @@ -0,0 +1,14 @@ +--- +import { KNOWN_LANGUAGE_CODES } from "../../i18n/locales"; + +export const getStaticPaths = () => { + return KNOWN_LANGUAGE_CODES.flatMap((langKey) => ({ + params: { lang: langKey }, + })); +}; +--- + +
+

404

+

You're in the wrong part of this localized town.

+
\ No newline at end of file diff --git a/packages/astro/test/fixtures/i18n-routing-404/src/pages/[lang]/index.astro b/packages/astro/test/fixtures/i18n-routing-404/src/pages/[lang]/index.astro new file mode 100644 index 000000000000..f10a2373aeb3 --- /dev/null +++ b/packages/astro/test/fixtures/i18n-routing-404/src/pages/[lang]/index.astro @@ -0,0 +1,130 @@ +--- +import Layout from '../../layouts/Layout.astro'; +import Card from '../../components/Card.astro'; +import { KNOWN_LANGUAGE_CODES } from "../../i18n/locales"; + +export const getStaticPaths = () => { + return KNOWN_LANGUAGE_CODES.flatMap((langKey) => ({ + params: { lang: langKey }, + })); +}; +--- + + +
+ +

Welcome to Localized Astro

+

+ To get started, open the directory src/pages in your project.
+ Code Challenge: Tweak the "Welcome to Astro" message above. +

+ +
+
+ + diff --git a/packages/astro/test/fixtures/i18n-routing-404/src/pages/index.astro b/packages/astro/test/fixtures/i18n-routing-404/src/pages/index.astro new file mode 100644 index 000000000000..fb6262872d0e --- /dev/null +++ b/packages/astro/test/fixtures/i18n-routing-404/src/pages/index.astro @@ -0,0 +1,123 @@ +--- +import Layout from '../layouts/Layout.astro'; +import Card from '../components/Card.astro'; +--- + + +
+ +

Welcome to Astro

+

+ To get started, open the directory src/pages in your project.
+ Code Challenge: Tweak the "Welcome to Astro" message above. +

+ +
+
+ + diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 98bf229977ca..e169f02fb334 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3123,6 +3123,12 @@ importers: specifier: workspace:* version: link:../../.. + packages/astro/test/fixtures/i18n-routing-404: + dependencies: + astro: + specifier: workspace:* + version: link:../../.. + packages/astro/test/fixtures/i18n-routing-base: dependencies: astro: @@ -8715,6 +8721,7 @@ packages: libsql@0.4.5: resolution: {integrity: sha512-sorTJV6PNt94Wap27Sai5gtVLIea4Otb2LUiAUyr3p6BPOScGMKGt5F1b5X/XgkNtcsDKeX5qfeBDj+PdShclQ==} + cpu: [x64, arm64, wasm32] os: [darwin, linux, win32] lilconfig@2.1.0: From 97ad8770a8fcb60a3ceafd600d3911d8c5a858f4 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Mon, 28 Oct 2024 15:27:21 +0100 Subject: [PATCH 2/4] fix --- .changeset/rude-frogs-hear.md | 5 +++++ packages/astro/src/i18n/index.ts | 4 ++++ packages/astro/test/i18n-routing.test.js | 23 +++++++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 .changeset/rude-frogs-hear.md diff --git a/.changeset/rude-frogs-hear.md b/.changeset/rude-frogs-hear.md new file mode 100644 index 000000000000..946c041d035c --- /dev/null +++ b/.changeset/rude-frogs-hear.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes a case where root 404 and 500 custom routes would not be taken into account when using i18n features diff --git a/packages/astro/src/i18n/index.ts b/packages/astro/src/i18n/index.ts index 2e8da37c6a3e..82c969983eb2 100644 --- a/packages/astro/src/i18n/index.ts +++ b/packages/astro/src/i18n/index.ts @@ -21,6 +21,10 @@ export function requestHasLocale(locales: Locales) { export function requestIs404Or500(request: Request, base = '') { const url = new URL(request.url); + // needed otherwise the check becomes //404, which is always wrong + if (base === '/') { + base = ''; + } return url.pathname.startsWith(`${base}/404`) || url.pathname.startsWith(`${base}/500`); } diff --git a/packages/astro/test/i18n-routing.test.js b/packages/astro/test/i18n-routing.test.js index 10c35bacd9b3..8133a105d2b7 100644 --- a/packages/astro/test/i18n-routing.test.js +++ b/packages/astro/test/i18n-routing.test.js @@ -1176,6 +1176,29 @@ describe('[SSG] i18n routing', () => { }); }); }); + + describe('404s', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: './fixtures/i18n-routing-404/', + }); + await fixture.build(); + }); + + it('should output both root and localized 404s', async () => { + let html = await fixture.readFile('/en/404/index.html'); + assert.equal(html.includes("You're in the wrong part of this localized town."), true); + + html = await fixture.readFile('/ja/404/index.html'); + assert.equal(html.includes("You're in the wrong part of this localized town."), true); + + html = await fixture.readFile('/404.html'); + assert.equal(html.includes("You're in the wrong part of town."), true); + }); + }); }); describe('[SSR] i18n routing', () => { let app; From e65fccb06674f30681c82a42648780b87dce5757 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Mon, 28 Oct 2024 15:29:18 +0100 Subject: [PATCH 3/4] chore: remove useless files --- .../src/components/Card.astro | 61 -------- .../i18n-routing-404/src/layouts/Layout.astro | 50 ------- .../src/pages/[lang]/index.astro | 130 ------------------ .../i18n-routing-404/src/pages/index.astro | 123 ----------------- 4 files changed, 364 deletions(-) delete mode 100644 packages/astro/test/fixtures/i18n-routing-404/src/components/Card.astro delete mode 100644 packages/astro/test/fixtures/i18n-routing-404/src/layouts/Layout.astro delete mode 100644 packages/astro/test/fixtures/i18n-routing-404/src/pages/[lang]/index.astro delete mode 100644 packages/astro/test/fixtures/i18n-routing-404/src/pages/index.astro diff --git a/packages/astro/test/fixtures/i18n-routing-404/src/components/Card.astro b/packages/astro/test/fixtures/i18n-routing-404/src/components/Card.astro deleted file mode 100644 index bd6d5971ebf3..000000000000 --- a/packages/astro/test/fixtures/i18n-routing-404/src/components/Card.astro +++ /dev/null @@ -1,61 +0,0 @@ ---- -interface Props { - title: string; - body: string; - href: string; -} - -const { href, title, body } = Astro.props; ---- - - - diff --git a/packages/astro/test/fixtures/i18n-routing-404/src/layouts/Layout.astro b/packages/astro/test/fixtures/i18n-routing-404/src/layouts/Layout.astro deleted file mode 100644 index 181097125d82..000000000000 --- a/packages/astro/test/fixtures/i18n-routing-404/src/layouts/Layout.astro +++ /dev/null @@ -1,50 +0,0 @@ ---- -interface Props { - title: string; -} - -const { title } = Astro.props; ---- - - - - - - - - - - {title} - - - - - - diff --git a/packages/astro/test/fixtures/i18n-routing-404/src/pages/[lang]/index.astro b/packages/astro/test/fixtures/i18n-routing-404/src/pages/[lang]/index.astro deleted file mode 100644 index f10a2373aeb3..000000000000 --- a/packages/astro/test/fixtures/i18n-routing-404/src/pages/[lang]/index.astro +++ /dev/null @@ -1,130 +0,0 @@ ---- -import Layout from '../../layouts/Layout.astro'; -import Card from '../../components/Card.astro'; -import { KNOWN_LANGUAGE_CODES } from "../../i18n/locales"; - -export const getStaticPaths = () => { - return KNOWN_LANGUAGE_CODES.flatMap((langKey) => ({ - params: { lang: langKey }, - })); -}; ---- - - -
- -

Welcome to Localized Astro

-

- To get started, open the directory src/pages in your project.
- Code Challenge: Tweak the "Welcome to Astro" message above. -

- -
-
- - diff --git a/packages/astro/test/fixtures/i18n-routing-404/src/pages/index.astro b/packages/astro/test/fixtures/i18n-routing-404/src/pages/index.astro deleted file mode 100644 index fb6262872d0e..000000000000 --- a/packages/astro/test/fixtures/i18n-routing-404/src/pages/index.astro +++ /dev/null @@ -1,123 +0,0 @@ ---- -import Layout from '../layouts/Layout.astro'; -import Card from '../components/Card.astro'; ---- - - -
- -

Welcome to Astro

-

- To get started, open the directory src/pages in your project.
- Code Challenge: Tweak the "Welcome to Astro" message above. -

- -
-
- - From 7f2b88e53be90720ad94ee19c2edd7d272e98e10 Mon Sep 17 00:00:00 2001 From: Florian Lefebvre Date: Mon, 28 Oct 2024 18:30:26 +0100 Subject: [PATCH 4/4] fix: fixture --- .../astro/test/fixtures/i18n-routing-404/src/pages/index.astro | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 packages/astro/test/fixtures/i18n-routing-404/src/pages/index.astro diff --git a/packages/astro/test/fixtures/i18n-routing-404/src/pages/index.astro b/packages/astro/test/fixtures/i18n-routing-404/src/pages/index.astro new file mode 100644 index 000000000000..e69de29bb2d1