Skip to content

Commit

Permalink
fix #6020 (#6320)
Browse files Browse the repository at this point in the history
  • Loading branch information
JerryWu1234 authored Feb 22, 2023
1 parent a9a40d0 commit ccd72e6
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/lazy-cats-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

fix #6020
1 change: 1 addition & 0 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,7 @@ export interface RouteData {
pattern: RegExp;
segments: RoutePart[][];
type: RouteType;
prerender: boolean;
}

export type SerializedRouteData = Omit<RouteData, 'generate' | 'pattern'> & {
Expand Down
5 changes: 2 additions & 3 deletions packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/core/build/plugins/plugin-prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions packages/astro/src/core/routing/manifest/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ export function createRouteManifest(
component,
generate,
pathname: pathname || undefined,
prerender: false
});
}
});
Expand Down Expand Up @@ -407,6 +408,7 @@ export function createRouteManifest(
component,
generate,
pathname: pathname || void 0,
prerender: false
});
});

Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/core/routing/manifest/serialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
}
13 changes: 0 additions & 13 deletions packages/astro/src/core/routing/match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>): 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));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div>
<p>Rest route. Should not give 404</p>
</div>
14 changes: 14 additions & 0 deletions packages/astro/test/ssr-prerender.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')

});
})
});

0 comments on commit ccd72e6

Please sign in to comment.