Skip to content

Commit

Permalink
Merge pull request #73 from withastro/main
Browse files Browse the repository at this point in the history
#Updates--rM
  • Loading branch information
akshit20421 authored Nov 18, 2023
2 parents 384bb7e + 3067817 commit c277a4d
Show file tree
Hide file tree
Showing 17 changed files with 300 additions and 255 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-taxis-arrive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Adds a warning if `astro add` fetches a package but returns a non-404 status
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
11 changes: 10 additions & 1 deletion packages/astro/src/cli/add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -731,8 +731,11 @@ async function fetchPackageJson(
const res = await fetch(`${registry}/${packageName}/${tag}`);
if (res.status >= 200 && res.status < 300) {
return await res.json();
} else {
} else if (res.status === 404) {
// 404 means the package doesn't exist, so we don't need an error message here
return new Error();
} else {
return new Error(`Failed to fetch ${registry}/${packageName}/${tag} - GET ${res.status}`);
}
}

Expand All @@ -754,6 +757,9 @@ export async function validateIntegrations(integrations: string[]): Promise<Inte
} else {
const firstPartyPkgCheck = await fetchPackageJson('@astrojs', name, tag);
if (firstPartyPkgCheck instanceof Error) {
if (firstPartyPkgCheck.message) {
spinner.warn(yellow(firstPartyPkgCheck.message));
}
spinner.warn(
yellow(`${bold(integration)} is not an official Astro package. Use at your own risk!`)
);
Expand All @@ -780,6 +786,9 @@ export async function validateIntegrations(integrations: string[]): Promise<Inte
if (pkgType === 'third-party') {
const thirdPartyPkgCheck = await fetchPackageJson(scope, name, tag);
if (thirdPartyPkgCheck instanceof Error) {
if (thirdPartyPkgCheck.message) {
spinner.warn(yellow(thirdPartyPkgCheck.message));
}
throw new Error(`Unable to fetch ${bold(integration)}. Does the package exist?`);
} else {
pkgJson = thirdPartyPkgCheck as any;
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 c277a4d

Please sign in to comment.