CSS Modules are duplicated when a component is both server-rendered and used with client:only
Astro Info
Astro v7.3.5
Vite v8.3.1
Node v24.11.0
System Linux (x64)
Package Manager npm
Output server
Adapter @astrojs/node (v11.1.6)
Integrations @astrojs/react (v7.0.0)If this issue only occurs in one browser, which browser is a problem?
No response
Describe the Bug
When a React component imports a CSS Module and is used both server-rendered and
with client:only="react", a production server build emits its CSS in two files
and links both files on the same page.
This is not just an unused asset left in dist: both stylesheets are downloaded
and contain the same scoped selector and declarations. The example below emits:
- A page stylesheet containing the component's CSS plus the page's own CSS.
- A client-component stylesheet containing the component's CSS again.
build.inlineStylesheets: 'never' makes the duplication easy to inspect. It is
not required to trigger it: with the default auto setting, this small example
instead includes the component's rules twice in inline <style> elements.
Important scope distinction
I initially suspected any mixture of hydrated and non-hydrated components.
However, the minimal controls with a static instance plus client:load or
client:visible did not reproduce the duplication. The isolated trigger here
is a component used server-rendered and also discovered with client:only.
Strictly speaking, client:only mounts a component without server rendering it,
rather than hydrating existing HTML.
A client:only instance does not even have to render. This also reproduces:
<Card label="Static" />
<Card label="Hydrated" client:load />
{false && <Card label="Never rendered" client:only="react" />}This matters for component dispatchers that support multiple client directives in conditional branches: duplication can affect a response that only renders static and normally hydrated instances.
What's the expected result?
Link to Minimal Reproducible Example
Before submitting: replace this line with the public URL containing the five files below. All reproduction source is included here so it can also be run locally without the originating application.
package.json
{
"name": "astro-css-duplication-repro",
"private": true,
"type": "module",
"scripts": {
"build": "astro build",
"preview": "astro preview",
"info": "astro info"
},
"dependencies": {
"astro": "7.3.5",
"@astrojs/react": "7.0.0",
"@astrojs/node": "11.1.6",
"react": "19.3.0",
"react-dom": "19.3.0"
}
}astro.config.mjs
import { defineConfig } from 'astro/config';
import react from '@astrojs/react';
import node from '@astrojs/node';
export default defineConfig({
integrations: [react()],
output: 'server',
adapter: node({ mode: 'standalone' }),
build: { inlineStylesheets: 'never' },
});src/components/Card.jsx
import { useState } from 'react';
import styles from './Card.module.css';
export default function Card({ label }) {
const [count, setCount] = useState(0);
return (
<button className={styles.card} onClick={() => setCount(count + 1)}>
{label}: {count}
</button>
);
}src/components/Card.module.css
.card {
border: 3px solid rebeccapurple;
padding: 17px;
color: darkslateblue;
--card-css-marker: 123456;
}src/pages/index.astro
---
import Card from '../components/Card.jsx';
---
<html lang="en">
<head><title>CSS duplication reproduction</title></head>
<body>
<Card label="Static" />
<Card label="Client only" client:only="react" />
</body>
</html>
<style>
body { margin: 23px; }
</style>Steps to reproduce
Create the five files above in an empty directory.
Install and build:
npm install npm run buildStart the built Node server:
HOST=127.0.0.1 PORT=4321 node dist/server/entry.mjsOpen
http://127.0.0.1:4321/with the browser cache disabled. Inspect the stylesheet links, CSS requests, and response bodies.Observe that both stylesheets contain
--card-css-marker:123456and the same scoped.cardrule. Clicking the client-only button increments its counter; the static button stays at zero.
There is no Tailwind, CSS composition, custom PostCSS configuration, custom chunk splitting, custom Vite plugin, or prerendered route in this reproduction.
Actual production output
The server response contains these two stylesheet links (hashes may vary):
<link rel="stylesheet" href="/_astro/index.FlVORkSR.css">
<link rel="stylesheet" href="/_astro/Card.CDknfVlL.css">dist/client/_astro/index.FlVORkSR.css — 107 bytes, including trailing newline:
._card_1foop_1{color:#483d8b;--card-css-marker:123456;border:3px solid #639;padding:17px}body{margin:23px}dist/client/_astro/Card.CDknfVlL.css — 90 bytes, including trailing newline:
._card_1foop_1{color:#483d8b;--card-css-marker:123456;border:3px solid #639;padding:17px}Both stylesheet URLs returned HTTP 200. A fresh Chromium context recorded both
as link-initiated network requests, with decoded body sizes of 107 and 90 bytes.
The CSSOM contained the identical component rule in both stylesheets. Both
buttons had the expected computed styles and the client-only counter worked.
Removing the page's <style> block also reproduces the issue: it emits and links
index.CDknfVlL.css and Card.CDknfVlL.css, each with identical 90-byte contents.
Keeping the page style demonstrates that this is not limited to byte-identical
assets: one stylesheet can contain the other stylesheet's rules as a subset.
Control builds
Each row was built separately with Astro 7.3.5 and @astrojs/react 7.0.0.
Counts refer to component-rule copies actually included by the page, not all
files present in the build directory. Unless noted otherwise, these variants
retain the page's <style> block and use inlineStylesheets: 'never'.
| Output | Component usage / change | External copies | Inline copies |
|---|---|---|---|
| Server | Static only | 1 | 0 |
| Server | Static + client:load |
1 | 0 |
| Server | Static + client:visible |
1 | 0 |
| Server | Static + client:only |
2 | 0 |
| Server | Static + client:only, no page CSS |
2 | 0 |
| Server | client:load + client:only |
2 | 0 |
| Server | client:only alone |
1 | 0 |
| Server | Static + client:load + unrendered client:only branch |
2 | 0 |
| Server | Static + client:only, inlineStylesheets: 'always' |
0 | 2 |
| Server | Static + client:only, default inlineStylesheets: 'auto' |
0 | 2 |
| Static | Static + client:load, no adapter |
1 | 0 |
| Static | Static + client:only, no adapter |
1 | 1 |
For the static-output controls, remove output, adapter, and the unused Node
adapter import from the config. The last row is noteworthy: the duplicate
component CSS is partly inline even though inlineStylesheets is set to never.
The main report concerns duplication, regardless of whether the second copy is
external or inline.
Related issues
- #12303 — CSS is duplicated across external files:
similar symptom, including both copies being requested by the browser. Its
discussion says an earlier fix caused regressions and was being reverted. It
was subsequently closed as stale, with a request to open a new issue if the
problem still occurred on a current Astro version. This report provides a
fresh reproduction on Astro 7.3.5 with a narrowed
client:onlytrigger. - #17488 — Deduplicate CSS assets between prerender and SSR environments: related deduplication work, but the server-output reproduction here has no prerendered route and involves shared server/client component CSS.
No particular internal cause or fix is asserted here; the evidence establishes the build output, page inclusion, and the directive combinations that trigger it.
What's the expected result?
A single page should not receive duplicate copies of the same component's CSS
because that component is also reachable through a client:only boundary.
The static instance and client-rendered instance should both remain styled,
without downloading or inlining the same rules twice.
This is not a request to remove CSS needed exclusively by other pages or lazy components, or to change cascade order. The reproduction has one page, one React component, and no dynamic imports.
Link to Minimal Reproducible Example
https://github.com/forberg/astro-css-depdup-bug
Participation
- I am willing to submit a pull request for this issue.
Source: withastro/astro