Skip to content

Commit

Permalink
Fix prerendering with unused dynamic chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Jul 1, 2024
1 parent e6de11f commit e2e3f93
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-lies-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fixes prerendering not removing unused dynamic imported chunks
4 changes: 3 additions & 1 deletion packages/astro/src/core/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/build/plugins/plugin-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };`);
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/build/plugins/plugin-prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<html>
<head>
<title>Static Page</title>
<title>Static Page should not exist in chunks</title>
</head>
<body>
<Counter client:load />
Expand Down
11 changes: 11 additions & 0 deletions packages/astro/test/ssr-prerender-chunks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/);
}
});
});

0 comments on commit e2e3f93

Please sign in to comment.