0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-06 22:10:10 -05:00

Fix prerendering with unused dynamic chunks (#11387)

This commit is contained in:
Bjorn Lu 2024-07-17 15:48:28 +08:00 committed by GitHub
parent cb4e6d09de
commit b498461e27
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 23 additions and 5 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes prerendering not removing unused dynamic imported chunks

View file

@ -314,10 +314,12 @@ export class App {
} }
const pathname = this.#getPathnameFromRequest(request); const pathname = this.#getPathnameFromRequest(request);
const defaultStatus = this.#getDefaultStatusCode(routeData, pathname); const defaultStatus = this.#getDefaultStatusCode(routeData, pathname);
const mod = await this.#pipeline.getModuleForRoute(routeData);
let response; let response;
try { try {
// Load route module. We also catch its error here if it fails on initialization
const mod = await this.#pipeline.getModuleForRoute(routeData);
const renderContext = RenderContext.create({ const renderContext = RenderContext.create({
pipeline: this.#pipeline, pipeline: this.#pipeline,
locals, locals,

View file

@ -44,8 +44,8 @@ function vitePluginPages(opts: StaticBuildOptions, internals: BuildInternals): V
for (const pageData of pageDatas) { for (const pageData of pageDatas) {
const resolvedPage = await this.resolve(pageData.moduleSpecifier); const resolvedPage = await this.resolve(pageData.moduleSpecifier);
if (resolvedPage) { if (resolvedPage) {
imports.push(`const page = () => import(${JSON.stringify(pageData.moduleSpecifier)});`); imports.push(`import * as _page from ${JSON.stringify(pageData.moduleSpecifier)};`);
exports.push(`export { page }`); exports.push(`export const page = () => _page`);
imports.push(`import { renderers } from "${RENDERERS_MODULE_ID}";`); imports.push(`import { renderers } from "${RENDERERS_MODULE_ID}";`);
exports.push(`export { renderers };`); exports.push(`export { renderers };`);

View file

@ -40,7 +40,7 @@ function getNonPrerenderOnlyChunks(bundle: Rollup.OutputBundle, internals: Build
const prerenderOnlyEntryChunks = new Set<Rollup.OutputChunk>(); const prerenderOnlyEntryChunks = new Set<Rollup.OutputChunk>();
const nonPrerenderOnlyEntryChunks = new Set<Rollup.OutputChunk>(); const nonPrerenderOnlyEntryChunks = new Set<Rollup.OutputChunk>();
for (const chunk of chunks) { for (const chunk of chunks) {
if (chunk.type === 'chunk' && (chunk.isEntry || chunk.isDynamicEntry)) { if (chunk.type === 'chunk' && chunk.isEntry) {
// See if this entry chunk is prerendered, if so, skip it // See if this entry chunk is prerendered, if so, skip it
if (chunk.facadeModuleId?.startsWith(ASTRO_PAGE_RESOLVED_MODULE_ID)) { if (chunk.facadeModuleId?.startsWith(ASTRO_PAGE_RESOLVED_MODULE_ID)) {
const pageDatas = getPagesFromVirtualModulePageName( const pageDatas = getPagesFromVirtualModulePageName(

View file

@ -5,7 +5,7 @@
<html> <html>
<head> <head>
<title>Static Page</title> <title>Static Page should not exist in chunks</title>
</head> </head>
<body> <body>
<Counter client:load /> <Counter client:load />

View file

@ -18,4 +18,15 @@ describe('Chunks', () => {
const hasImportFromPrerender = !content.includes(`React } from './chunks/prerender`); const hasImportFromPrerender = !content.includes(`React } from './chunks/prerender`);
assert.ok(hasImportFromPrerender); assert.ok(hasImportFromPrerender);
}); });
it('does not have prerender code', async () => {
const files = await fixture.readdir('/_worker.js/chunks');
assert.ok(files.length > 0);
for (const file of files) {
// Skip astro folder
if (file === 'astro') continue
const content = await fixture.readFile(`/_worker.js/chunks/${file}`);
assert.doesNotMatch(content, /Static Page should not exist in chunks/);
}
});
}); });