Skip to content

Commit

Permalink
fix(i18n): review fallback system (#9119)
Browse files Browse the repository at this point in the history
  • Loading branch information
ematipico authored Nov 17, 2023
1 parent f4efd1c commit 3067817
Show file tree
Hide file tree
Showing 15 changed files with 285 additions and 254 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-jobs-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix a flaw in the i18n fallback logic, where the routes didn't preserve their metadata, such as hoisted scripts
7 changes: 6 additions & 1 deletion packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2434,16 +2434,21 @@ export interface RouteData {
prerender: boolean;
redirect?: RedirectConfig;
redirectRoute?: RouteData;
fallbackRoutes: RouteData[];
}

export type RedirectRouteData = RouteData & {
redirect: string;
};

export type SerializedRouteData = Omit<RouteData, 'generate' | 'pattern' | 'redirectRoute'> & {
export type SerializedRouteData = Omit<
RouteData,
'generate' | 'pattern' | 'redirectRoute' | 'fallbackRoutes'
> & {
generate: undefined;
pattern: string;
redirectRoute: SerializedRouteData | undefined;
fallbackRoutes: SerializedRouteData[];
_meta: {
trailingSlash: AstroConfig['trailingSlash'];
};
Expand Down
21 changes: 18 additions & 3 deletions packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ export class App {
}
return pathname;
}

#getPathnameFromRequest(request: Request): string {
const url = new URL(request.url);
const pathname = prependForwardSlash(this.removeBase(url.pathname));
return pathname;
}

match(request: Request, _opts: MatchOptions = {}): RouteData | undefined {
const url = new URL(request.url);
// ignore requests matching public assets
Expand All @@ -151,7 +158,8 @@ export class App {
}

Reflect.set(request, clientLocalsSymbol, locals ?? {});
const defaultStatus = this.#getDefaultStatusCode(routeData.route);
const pathname = this.#getPathnameFromRequest(request);
const defaultStatus = this.#getDefaultStatusCode(routeData, pathname);
const mod = await this.#getModuleForRoute(routeData);

const pageModule = (await mod.page()) as any;
Expand Down Expand Up @@ -369,8 +377,15 @@ export class App {
});
}

#getDefaultStatusCode(route: string): number {
route = removeTrailingForwardSlash(route);
#getDefaultStatusCode(routeData: RouteData, pathname: string): number {
if (!routeData.pattern.exec(pathname)) {
for (const fallbackRoute of routeData.fallbackRoutes) {
if (fallbackRoute.pattern.test(pathname)) {
return 302;
}
}
}
const route = removeTrailingForwardSlash(routeData.route);
if (route.endsWith('/404')) return 404;
if (route.endsWith('/500')) return 500;
return 200;
Expand Down
20 changes: 9 additions & 11 deletions packages/astro/src/core/build/buildPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,15 @@ export class BuildPipeline extends Pipeline {
}
}

for (const [path, pageDataList] of this.#internals.pagesByComponents.entries()) {
for (const pageData of pageDataList) {
if (routeIsRedirect(pageData.route)) {
pages.set(pageData, path);
} else if (
routeIsFallback(pageData.route) &&
(i18nHasFallback(this.getConfig()) ||
(routeIsFallback(pageData.route) && pageData.route.route === '/'))
) {
pages.set(pageData, path);
}
for (const [path, pageData] of this.#internals.pagesByComponent.entries()) {
if (routeIsRedirect(pageData.route)) {
pages.set(pageData, path);
} else if (
routeIsFallback(pageData.route) &&
(i18nHasFallback(this.getConfig()) ||
(routeIsFallback(pageData.route) && pageData.route.route === '/'))
) {
pages.set(pageData, path);
}
}

Expand Down
Loading

0 comments on commit 3067817

Please sign in to comment.