summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Bjorn Lu <bjornlu.dev@gmail.com> 2024-07-17 15:48:28 +0800
committerGravatar GitHub <noreply@github.com> 2024-07-17 15:48:28 +0800
commitb498461e277bffb0abe21b59a94b1e56a8c69d47 (patch)
tree49c64d17fc961a65ba8fbcca9aa3a3e2f15c4184
parentcb4e6d09deb7507058115a3fd2a567019a501e4d (diff)
downloadastro-b498461e277bffb0abe21b59a94b1e56a8c69d47.tar.gz
astro-b498461e277bffb0abe21b59a94b1e56a8c69d47.tar.zst
astro-b498461e277bffb0abe21b59a94b1e56a8c69d47.zip
Fix prerendering with unused dynamic chunks (#11387)
-rw-r--r--.changeset/giant-lies-taste.md5
-rw-r--r--packages/astro/src/core/app/index.ts4
-rw-r--r--packages/astro/src/core/build/plugins/plugin-pages.ts4
-rw-r--r--packages/astro/src/core/build/plugins/plugin-prerender.ts2
-rw-r--r--packages/astro/test/fixtures/ssr-prerender-chunks/src/pages/index.astro2
-rw-r--r--packages/astro/test/ssr-prerender-chunks.test.js11
6 files changed, 23 insertions, 5 deletions
diff --git a/.changeset/giant-lies-taste.md b/.changeset/giant-lies-taste.md
new file mode 100644
index 000000000..64476edf8
--- /dev/null
+++ b/.changeset/giant-lies-taste.md
@@ -0,0 +1,5 @@
+---
+'astro': patch
+---
+
+Fixes prerendering not removing unused dynamic imported chunks
diff --git a/packages/astro/src/core/app/index.ts b/packages/astro/src/core/app/index.ts
index 0ef48a926..aa3c7370d 100644
--- a/packages/astro/src/core/app/index.ts
+++ b/packages/astro/src/core/app/index.ts
@@ -314,10 +314,12 @@ export class App {
}
const pathname = this.#getPathnameFromRequest(request);
const defaultStatus = this.#getDefaultStatusCode(routeData, pathname);
- const mod = await this.#pipeline.getModuleForRoute(routeData);
let response;
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({
pipeline: this.#pipeline,
locals,
diff --git a/packages/astro/src/core/build/plugins/plugin-pages.ts b/packages/astro/src/core/build/plugins/plugin-pages.ts
index 9b7faf07c..4df70eac4 100644
--- a/packages/astro/src/core/build/plugins/plugin-pages.ts
+++ b/packages/astro/src/core/build/plugins/plugin-pages.ts
@@ -44,8 +44,8 @@ function vitePluginPages(opts: StaticBuildOptions, internals: BuildInternals): V
for (const pageData of pageDatas) {
const resolvedPage = await this.resolve(pageData.moduleSpecifier);
if (resolvedPage) {
- imports.push(`const page = () => import(${JSON.stringify(pageData.moduleSpecifier)});`);
- exports.push(`export { page }`);
+ imports.push(`import * as _page from ${JSON.stringify(pageData.moduleSpecifier)};`);
+ exports.push(`export const page = () => _page`);
imports.push(`import { renderers } from "${RENDERERS_MODULE_ID}";`);
exports.push(`export { renderers };`);
diff --git a/packages/astro/src/core/build/plugins/plugin-prerender.ts b/packages/astro/src/core/build/plugins/plugin-prerender.ts
index b7feb70e3..a8ace9a25 100644
--- a/packages/astro/src/core/build/plugins/plugin-prerender.ts
+++ b/packages/astro/src/core/build/plugins/plugin-prerender.ts
@@ -40,7 +40,7 @@ function getNonPrerenderOnlyChunks(bundle: Rollup.OutputBundle, internals: Build
const prerenderOnlyEntryChunks = new Set<Rollup.OutputChunk>();
const nonPrerenderOnlyEntryChunks = new Set<Rollup.OutputChunk>();
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
if (chunk.facadeModuleId?.startsWith(ASTRO_PAGE_RESOLVED_MODULE_ID)) {
const pageDatas = getPagesFromVirtualModulePageName(
diff --git a/packages/astro/test/fixtures/ssr-prerender-chunks/src/pages/index.astro b/packages/astro/test/fixtures/ssr-prerender-chunks/src/pages/index.astro
index 21a503211..05ac05b68 100644
--- a/packages/astro/test/fixtures/ssr-prerender-chunks/src/pages/index.astro
+++ b/packages/astro/test/fixtures/ssr-prerender-chunks/src/pages/index.astro
@@ -5,7 +5,7 @@
<html>
<head>
- <title>Static Page</title>
+ <title>Static Page should not exist in chunks</title>
</head>
<body>
<Counter client:load />
diff --git a/packages/astro/test/ssr-prerender-chunks.test.js b/packages/astro/test/ssr-prerender-chunks.test.js
index 7bd916814..c44d1d347 100644
--- a/packages/astro/test/ssr-prerender-chunks.test.js
+++ b/packages/astro/test/ssr-prerender-chunks.test.js
@@ -18,4 +18,15 @@ describe('Chunks', () => {
const hasImportFromPrerender = !content.includes(`React } from './chunks/prerender`);
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/);
+ }
+ });
});