-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fix: i18n 404 #12321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
fix: i18n 404 #12321
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/astro/test/fixtures/i18n-routing-404/astro.config.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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', | ||
| }, | ||
| }, | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "name": "@test/i18n-routing-404", | ||
| "version": "0.0.0", | ||
| "private": true, | ||
| "dependencies": { | ||
| "astro": "workspace:*" | ||
| } | ||
| } |
11 changes: 11 additions & 0 deletions
11
packages/astro/test/fixtures/i18n-routing-404/src/i18n/locales.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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'; |
4 changes: 4 additions & 0 deletions
4
packages/astro/test/fixtures/i18n-routing-404/src/pages/404.astro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| <div> | ||
| <h1>404</h1> | ||
| <p>You're in the wrong part of town.</p> | ||
| </div> |
14 changes: 14 additions & 0 deletions
14
packages/astro/test/fixtures/i18n-routing-404/src/pages/[lang]/404.astro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| --- | ||
| import { KNOWN_LANGUAGE_CODES } from "../../i18n/locales"; | ||
| export const getStaticPaths = () => { | ||
| return KNOWN_LANGUAGE_CODES.flatMap((langKey) => ({ | ||
| params: { lang: langKey }, | ||
| })); | ||
| }; | ||
| --- | ||
|
|
||
| <div> | ||
| <h1>404</h1> | ||
| <p>You're in the wrong part of this localized town.</p> | ||
| </div> |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is the correct fix, and I don't think the test is accurate either. We have
prefixDefaultLocale: true, so I would expect/404to be prefixed, hence/en/404. However, I don't expect/404to exist. Having both seems incorrect.However, if you think this is acceptable, we need to update the docs. For example,
pages/index.astrois required even when we haveprefixDefaultLocale: trueandredictToDefaultLocale: true. So we need to document that if users use404.astro, Astro will create a404.htmlunder each locale.Also, I would add more test cases, for example if
prefixDefaultLocale: false, I wouldn't expect to have/en/404.html. Also, I would create a test case without fallback.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you think that? If someone visits an invalid locale at the moment, they will see the host 404, not the user 404 no matter if it's set. So I think supporting this case is valid. But I'm not an i18n expert so let me know!
This is not what this fix is about I believe, it's about supporting a custom root 404. That doesn't prevent users from having a custom 404 under a locale.
Definitely agree, thanks for the pointers! I'm not super familiar with i18n in Astro so that helps a ton
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the PR description is empty, I find it difficult to understand what we're trying to fix 😅
Since the host always renders
/404.html, how can a developer tell the host to render/ru/404.htmlfor an invalid URL under the/rulocale? (It's an example.)I am not saying that the fix is incorrect, I am saying I am missing a lot of context from the PR. I believe the fix is correct, but I would add more tests to it, especially for existing fixtures where we don't use
[lang]and we have fixed pages.