diff --git a/.changeset/lazy-cats-clean.md b/.changeset/lazy-cats-clean.md new file mode 100644 index 000000000..6b5015d43 --- /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 ea3e5cd3c..868ba7fc2 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 615be50aa..c9ec49bd3 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 911f153ba..449fc2bc5 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 fda9b7e58..7e9738e43 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 866c0d7bb..361c60cba 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 b7694bec2..84b65fd6f 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 000000000..1ffc0a6f0 --- /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 6e5d854b6..86328e22a 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') + + }); + }) });