fix: localized error route status handling#17087
Conversation
🦋 Changeset detectedLatest commit: fe37920 The changes in this PR will be included in the next version bump. This PR includes changesets to release 392 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
e18e dependency analysisNo dependency warnings found. |
5295030 to
e797b8e
Compare
eaf7cea to
17bb65a
Compare
| export function getOutputFilename( | ||
| buildFormat: NonNullable<AstroConfig['build']>['format'], | ||
| name: string, | ||
| routeData: RouteData, | ||
| ) { | ||
| if (routeData.type === 'endpoint') { | ||
| return name; | ||
| } | ||
| if (name === '/' || name === '') { | ||
| return name === '' ? 'index.html' : '/index.html'; | ||
| } | ||
| if (buildFormat === 'file' || STATUS_CODE_PAGES.has(name)) { | ||
| return `${removeTrailingForwardSlash(name || 'index')}.html`; | ||
| } | ||
| if (buildFormat === 'preserve' && !routeData.isIndex) { | ||
| return `${removeTrailingForwardSlash(name || 'index')}.html`; | ||
| } | ||
| return `${removeTrailingForwardSlash(name)}/index.html`; | ||
| } |
There was a problem hiding this comment.
Extracted getOutputFilename so both build code and the error handler can use the same output path logic.
This lets localized prerendered error pages use the correct URL, without making the error
handler import Node-only modules(node:fs) from core/util.ts.
4e9de86 to
7810fba
Compare
| import type { Locales } from '../types/public/config.js'; | ||
|
|
||
| // Checks if the pathname has any locale | ||
| export function pathHasLocale(path: string, locales: Locales): boolean { | ||
| // pages that use a locale param ([locale].astro or [locale]/index.astro) | ||
| // and getStaticPaths make [locale].html the pathname during SSG | ||
| // which will not match a configured locale without removing .html | ||
| // as we do in normalizeThePath | ||
| const segments = path.split('/').map(normalizeThePath); | ||
| for (const segment of segments) { | ||
| for (const locale of locales) { | ||
| if (typeof locale === 'string') { | ||
| if (normalizeTheLocale(segment) === normalizeTheLocale(locale)) { | ||
| return true; | ||
| } | ||
| } else if (segment === locale.path) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| * Given a locale, this function: | ||
| * - replaces the `_` with a `-`; | ||
| * - transforms all letters to be lowercase; | ||
| */ | ||
| export function normalizeTheLocale(locale: string): string { | ||
| return locale.replaceAll('_', '-').toLowerCase(); | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| * Given a path or path segment, this function: | ||
| * - removes the `.html` extension if it exists | ||
| */ | ||
| export function normalizeThePath(path: string): string { | ||
| return path.endsWith('.html') ? path.slice(0, -5) : path; | ||
| } |
There was a problem hiding this comment.
a pure leaf (locale path matching) shared by i18n/index.ts and error-routes.ts, with no dependency back on the i18n barrel.
| } | ||
| } | ||
| return `/${status}${suffix}`; | ||
| } |
There was a problem hiding this comment.
The always-loaded DefaultErrorHandler imports the error-route helpers from error-routes.ts, which only pulls path.ts, not the i18n barrel (createI18nMiddleware, etc.)
6b03e8a to
fe37920
Compare
This PR contains the following updates: | Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) | |---|---|---|---| | [astro](https://astro.build) ([source](https://github.com/withastro/astro/tree/HEAD/packages/astro)) | [`7.0.2` → `7.0.3`](https://renovatebot.com/diffs/npm/astro/7.0.2/7.0.3) |  |  | --- ### Release Notes <details> <summary>withastro/astro (astro)</summary> ### [`v7.0.3`](https://github.com/withastro/astro/blob/HEAD/packages/astro/CHANGELOG.md#703) [Compare Source](https://github.com/withastro/astro/compare/astro@7.0.2...astro@7.0.3) ##### Patch Changes - [#​17189](withastro/astro#17189) [`24d2c9e`](withastro/astro@24d2c9e) Thanks [@​astrobot-houston](https://github.com/astrobot-houston)! - Fixes a bug where an error thrown inside one route's `getStaticPaths()` would prevent other valid routes from being matched in dev mode - [#​16932](withastro/astro#16932) [`8f4a3db`](withastro/astro@8f4a3db) Thanks [@​fkatsuhiro](https://github.com/fkatsuhiro)! - Fixes HMR for action files during development. Editing files in `src/actions/` now takes effect on the next request without requiring a dev server restart. - [#​17087](withastro/astro#17087) [`fb0ab02`](withastro/astro@fb0ab02) Thanks [@​jp-knj](https://github.com/jp-knj)! - Fixes localized custom error pages in i18n projects so routes like `/pt/404` are used for missing localized pages and return the correct status code </details> --- ### Configuration 📅 **Schedule**: (UTC) - Branch creation - At any time (no schedule defined) - Automerge - At any time (no schedule defined) 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Mend Renovate](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4yMzQuMCIsInVwZGF0ZWRJblZlciI6IjQzLjI0Ni4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->
Close: #12175
Closes AST-188
Changes
When it defines a locale-specific page like
/pt/404.astro,/pt/*routes, returns the correct 404 status for/pt/404,/docs/404as normal pages.Testing
Added focused unit tests for the new localized error route helpers and app routing behavior, then reviewed the generated test cases to make sure they match the existing test style and cover the important cases.
The tests cover:
/404and/500routes when available/pt/404/pt/aboutas 200/docs/404as normal pagesDocs
This may need docs because this PR fixes support for localized custom error pages