Skip to content
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

fix(i18n): review fallback system #9119

Merged
merged 4 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
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
Loading