Skip to content

Commit 8cb96d6

Browse files
committed
fix(rsc): remove duplicate server component css link
1 parent 9b2741f commit 8cb96d6

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

packages/plugin-rsc/examples/basic/src/routes/hmr-shared/atomic/client.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export function TestClient({
99
testSharedFromServer: string
1010
}) {
1111
React.useEffect(() => {
12-
console.log({ testShared, testSharedFromServer })
1312
if (testShared !== testSharedFromServer) {
13+
console.log({ testShared, testSharedFromServer })
1414
throw new Error(
1515
`Mismatch: ${JSON.stringify({ testShared, testSharedFromServer })}`,
1616
)

packages/plugin-rsc/src/plugin.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ window.__vite_plugin_react_preamble_installed__ = true;
836836
const ssrCss = document.querySelectorAll("link[rel='stylesheet']");
837837
import.meta.hot.on("vite:beforeUpdate", () => {
838838
ssrCss.forEach(node => {
839-
if (node.dataset.precedence?.startsWith("vite-rsc/")) {
839+
if (node.dataset.precedence === "vite-rsc/client-reference") {
840840
node.remove();
841841
}
842842
});
@@ -1910,6 +1910,20 @@ export function vitePluginRscCss(
19101910
// ensure hmr boundary at this virtual since otherwise non-self accepting css
19111911
// (e.g. css module) causes full reload
19121912
code += `if (import.meta.hot) { import.meta.hot.accept() }\n`
1913+
// remove server rendered <link> after inline <style> is injected on client
1914+
// to avoid duplicate styles
1915+
function removeFn(hrefs: string[]) {
1916+
for (const href of hrefs) {
1917+
const el = document.querySelector(
1918+
`link[rel="stylesheet"][href="${href}"]`,
1919+
)
1920+
console.log({ href, el })
1921+
if (el) {
1922+
el.remove()
1923+
}
1924+
}
1925+
}
1926+
code += `(${removeFn.toString()})(${JSON.stringify(result.hrefs)});\n`
19131927
return code
19141928
}
19151929
},

0 commit comments

Comments
 (0)