This commit is contained in:
wulinsheng123 2023-02-22 22:49:49 +08:00 committed by GitHub
parent a9a40d05d6
commit ccd72e6bb4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 29 additions and 16 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
fix #6020

View file

@ -1395,6 +1395,7 @@ export interface RouteData {
pattern: RegExp; pattern: RegExp;
segments: RoutePart[][]; segments: RoutePart[][];
type: RouteType; type: RouteType;
prerender: boolean;
} }
export type SerializedRouteData = Omit<RouteData, 'generate' | 'pattern'> & { export type SerializedRouteData = Omit<RouteData, 'generate' | 'pattern'> & {

View file

@ -25,7 +25,7 @@ import {
createLinkStylesheetElementSet, createLinkStylesheetElementSet,
createModuleScriptElement, createModuleScriptElement,
} from '../render/ssr-element.js'; } from '../render/ssr-element.js';
import { matchAssets, matchRoute } from '../routing/match.js'; import { matchRoute } from '../routing/match.js';
export { deserializeManifest } from './common.js'; export { deserializeManifest } from './common.js';
export const pagesVirtualModuleId = '@astrojs-pages-virtual-entry'; export const pagesVirtualModuleId = '@astrojs-pages-virtual-entry';
@ -100,8 +100,7 @@ export class App {
let routeData = matchRoute(pathname, this.#manifestData); let routeData = matchRoute(pathname, this.#manifestData);
if (routeData) { if (routeData) {
const asset = matchAssets(routeData, this.#manifest.assets); if (routeData.prerender) return undefined;
if (asset) return undefined;
return routeData; return routeData;
} else if (matchNotFound) { } else if (matchNotFound) {
return matchRoute('/404', this.#manifestData); return matchRoute('/404', this.#manifestData);

View file

@ -23,6 +23,7 @@ export function vitePluginPrerender(
// prerendered pages should be split into their own chunk // prerendered pages should be split into their own chunk
// Important: this can't be in the `pages/` directory! // Important: this can't be in the `pages/` directory!
if (meta.getModuleInfo(id)?.meta.astro?.pageOptions?.prerender) { if (meta.getModuleInfo(id)?.meta.astro?.pageOptions?.prerender) {
pageInfo.route.prerender = true;
return 'prerender'; return 'prerender';
} }
// dynamic pages should all go in their own chunk in the pages/* directory // dynamic pages should all go in their own chunk in the pages/* directory

View file

@ -332,6 +332,7 @@ export function createRouteManifest(
component, component,
generate, generate,
pathname: pathname || undefined, pathname: pathname || undefined,
prerender: false
}); });
} }
}); });
@ -407,6 +408,7 @@ export function createRouteManifest(
component, component,
generate, generate,
pathname: pathname || void 0, pathname: pathname || void 0,
prerender: false
}); });
}); });

View file

@ -24,5 +24,6 @@ export function deserializeRouteData(rawRouteData: SerializedRouteData): RouteDa
generate: getRouteGenerator(rawRouteData.segments, rawRouteData._meta.trailingSlash), generate: getRouteGenerator(rawRouteData.segments, rawRouteData._meta.trailingSlash),
pathname: rawRouteData.pathname || undefined, pathname: rawRouteData.pathname || undefined,
segments: rawRouteData.segments, segments: rawRouteData.segments,
prerender: rawRouteData.prerender
}; };
} }

View file

@ -5,19 +5,6 @@ export function matchRoute(pathname: string, manifest: ManifestData): RouteData
return manifest.routes.find((route) => route.pattern.test(decodeURI(pathname))); return manifest.routes.find((route) => route.pattern.test(decodeURI(pathname)));
} }
/** Find matching static asset from pathname */
export function matchAssets(route: RouteData, assets: Set<string>): 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 */ /** Finds all matching routes from pathname */
export function matchAllRoutes(pathname: string, manifest: ManifestData): RouteData[] { export function matchAllRoutes(pathname: string, manifest: ManifestData): RouteData[] {
return manifest.routes.filter((route) => route.pattern.test(pathname)); return manifest.routes.filter((route) => route.pattern.test(pathname));

View file

@ -0,0 +1,3 @@
<div>
<p>Rest route. Should not give 404</p>
</div>

View file

@ -49,4 +49,18 @@ describe('SSR: prerender', () => {
expect($('.user').text()).to.equal('houston'); 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')
});
})
}); });