Skip to content

Commit 8897c3e

Browse files
committed
Prevent CSS from being duplicated between SSR and prerender
1 parent 15fc25e commit 8897c3e

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

packages/astro/src/core/build/plugins/plugin-css.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] {
4141
const pagesToCss: Record<string, Record<string, { order: number; depth: number }>> = {};
4242
// Map of module Ids (usually something like `/Users/...blog.mdx?astroPropagatedAssets`) to its imported CSS
4343
const moduleIdToPropagatedCss: Record<string, Set<string>> = {};
44+
// Keep track of CSS that has been bundled to avoid duplication between ssr and prerender.
45+
const cssModulesInBundles = new Set();
4446

4547
const cssBuildPlugin: VitePlugin = {
4648
name: 'astro:rollup-plugin-build-css',
@@ -49,6 +51,21 @@ function rollupPluginAstroBuildCSS(options: PluginOptions): VitePlugin[] {
4951
return environment.name === 'client' || environment.name === 'ssr' || environment.name === 'prerender';
5052
},
5153

54+
transform(_code, id) {
55+
if(isCSSRequest(id)) {
56+
// In prerender, don't rebundle CSS that was already bundled in SSR.
57+
// Return an empty string here to prevent it.
58+
if(this.environment.name === 'prerender') {
59+
if(cssModulesInBundles.has(id)) {
60+
return {
61+
code: ''
62+
}
63+
}
64+
}
65+
cssModulesInBundles.add(id);
66+
}
67+
},
68+
5269
async generateBundle(_outputOptions, bundle) {
5370
// Collect CSS modules that were bundled during SSR build for deduplication in client build
5471
if (this.environment?.name === 'ssr' || this.environment?.name === 'prerender') {

0 commit comments

Comments
 (0)