From ccd72e6bb41e570d42b1b158e8124c8e04a1943d Mon Sep 17 00:00:00 2001 From: wulinsheng123 <409187100@qq.com> Date: Wed, 22 Feb 2023 22:49:49 +0800 Subject: [PATCH] fix #6020 (#6320) --- .changeset/lazy-cats-clean.md | 5 +++++ packages/astro/src/@types/astro.ts | 1 + packages/astro/src/core/app/index.ts | 5 ++--- .../src/core/build/plugins/plugin-prerender.ts | 1 + packages/astro/src/core/routing/manifest/create.ts | 2 ++ .../src/core/routing/manifest/serialization.ts | 1 + packages/astro/src/core/routing/match.ts | 13 ------------- .../ssr-prerender/src/pages/[...params].astro | 3 +++ packages/astro/test/ssr-prerender.test.js | 14 ++++++++++++++ 9 files changed, 29 insertions(+), 16 deletions(-) create mode 100644 .changeset/lazy-cats-clean.md create mode 100644 packages/astro/test/fixtures/ssr-prerender/src/pages/[...params].astro diff --git a/.changeset/lazy-cats-clean.md b/.changeset/lazy-cats-clean.md new file mode 100644 index 000000000000..6b5015d43e42 --- /dev/null +++ b/.changeset/lazy-cats-clean.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +fix #6020 diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index ea3e5cd3cf71..868ba7fc2aef 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -1395,6 +1395,7 @@ export interface RouteData { pattern: RegExp; segments: RoutePart[][]; type: RouteType; + prerender: boolean; } export type SerializedRouteData = Omit & { diff --git a/packages/astro/src/core/app/index.ts b/packages/astro/src/core/app/index.ts index 615be50aa585..c9ec49bd3c20 100644 --- a/packages/astro/src/core/app/index.ts +++ b/packages/astro/src/core/app/index.ts @@ -25,7 +25,7 @@ import { createLinkStylesheetElementSet, createModuleScriptElement, } from '../render/ssr-element.js'; -import { matchAssets, matchRoute } from '../routing/match.js'; +import { matchRoute } from '../routing/match.js'; export { deserializeManifest } from './common.js'; export const pagesVirtualModuleId = '@astrojs-pages-virtual-entry'; @@ -100,8 +100,7 @@ export class App { let routeData = matchRoute(pathname, this.#manifestData); if (routeData) { - const asset = matchAssets(routeData, this.#manifest.assets); - if (asset) return undefined; + if (routeData.prerender) return undefined; return routeData; } else if (matchNotFound) { return matchRoute('/404', this.#manifestData); diff --git a/packages/astro/src/core/build/plugins/plugin-prerender.ts b/packages/astro/src/core/build/plugins/plugin-prerender.ts index 911f153ba64e..449fc2bc527c 100644 --- a/packages/astro/src/core/build/plugins/plugin-prerender.ts +++ b/packages/astro/src/core/build/plugins/plugin-prerender.ts @@ -23,6 +23,7 @@ export function vitePluginPrerender( // prerendered pages should be split into their own chunk // Important: this can't be in the `pages/` directory! if (meta.getModuleInfo(id)?.meta.astro?.pageOptions?.prerender) { + pageInfo.route.prerender = true; return 'prerender'; } // dynamic pages should all go in their own chunk in the pages/* directory diff --git a/packages/astro/src/core/routing/manifest/create.ts b/packages/astro/src/core/routing/manifest/create.ts index fda9b7e58beb..7e9738e43f75 100644 --- a/packages/astro/src/core/routing/manifest/create.ts +++ b/packages/astro/src/core/routing/manifest/create.ts @@ -332,6 +332,7 @@ export function createRouteManifest( component, generate, pathname: pathname || undefined, + prerender: false }); } }); @@ -407,6 +408,7 @@ export function createRouteManifest( component, generate, pathname: pathname || void 0, + prerender: false }); }); diff --git a/packages/astro/src/core/routing/manifest/serialization.ts b/packages/astro/src/core/routing/manifest/serialization.ts index 866c0d7bbc5d..361c60cbade7 100644 --- a/packages/astro/src/core/routing/manifest/serialization.ts +++ b/packages/astro/src/core/routing/manifest/serialization.ts @@ -24,5 +24,6 @@ export function deserializeRouteData(rawRouteData: SerializedRouteData): RouteDa generate: getRouteGenerator(rawRouteData.segments, rawRouteData._meta.trailingSlash), pathname: rawRouteData.pathname || undefined, segments: rawRouteData.segments, + prerender: rawRouteData.prerender }; } diff --git a/packages/astro/src/core/routing/match.ts b/packages/astro/src/core/routing/match.ts index b7694bec2760..84b65fd6f054 100644 --- a/packages/astro/src/core/routing/match.ts +++ b/packages/astro/src/core/routing/match.ts @@ -5,19 +5,6 @@ export function matchRoute(pathname: string, manifest: ManifestData): RouteData return manifest.routes.find((route) => route.pattern.test(decodeURI(pathname))); } -/** Find matching static asset from pathname */ -export function matchAssets(route: RouteData, assets: Set): string | undefined { - for (const asset of assets) { - if (!asset.endsWith('.html')) continue; - if (route.pattern.test(asset)) { - return asset; - } - if (route.pattern.test(asset.replace(/index\.html$/, ''))) { - return asset; - } - } -} - /** Finds all matching routes from pathname */ export function matchAllRoutes(pathname: string, manifest: ManifestData): RouteData[] { return manifest.routes.filter((route) => route.pattern.test(pathname)); diff --git a/packages/astro/test/fixtures/ssr-prerender/src/pages/[...params].astro b/packages/astro/test/fixtures/ssr-prerender/src/pages/[...params].astro new file mode 100644 index 000000000000..1ffc0a6f00e3 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-prerender/src/pages/[...params].astro @@ -0,0 +1,3 @@ +
+

Rest route. Should not give 404

+
diff --git a/packages/astro/test/ssr-prerender.test.js b/packages/astro/test/ssr-prerender.test.js index 6e5d854b6fa1..86328e22af20 100644 --- a/packages/astro/test/ssr-prerender.test.js +++ b/packages/astro/test/ssr-prerender.test.js @@ -49,4 +49,18 @@ describe('SSR: prerender', () => { expect($('.user').text()).to.equal('houston'); }); }); + + describe('New prerender option breaks catch-all route on root when using preview', () => { + // bug id #6020 + it('fix bug id #6020', async () => { + const app = await fixture.loadTestAdapterApp(); + const request = new Request('http://example.com/some'); + const response = await app.render(request); + expect(response.status).to.equal(200); + const html = await response.text(); + const $ = cheerio.load(html); + expect($('p').text()).to.include('not give 404') + + }); + }) });