Skip to content

Commit ac20b31

Browse files
authored
fix(rsc): keep manually added link stylesheet during dev (#663)
1 parent aa95627 commit ac20b31

File tree

6 files changed

+36
-8
lines changed

6 files changed

+36
-8
lines changed

packages/plugin-rsc/e2e/basic.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,10 @@ function defineTest(f: Fixture) {
326326
'color',
327327
'rgb(255, 165, 0)',
328328
)
329+
await expect(page.locator('.test-style-server-manual')).toHaveCSS(
330+
'color',
331+
'rgb(255, 165, 0)',
332+
)
329333
})
330334

331335
testNoJs('css @nojs', async ({ page }) => {
@@ -335,6 +339,10 @@ function defineTest(f: Fixture) {
335339
'color',
336340
'rgb(255, 165, 0)',
337341
)
342+
await expect(page.locator('.test-style-server-manual')).toHaveCSS(
343+
'color',
344+
'rgb(255, 165, 0)',
345+
)
338346
})
339347

340348
async function testCss(page: Page, color = 'rgb(255, 165, 0)') {
@@ -453,6 +461,10 @@ function defineTest(f: Fixture) {
453461
'color',
454462
'rgb(255, 165, 0)',
455463
)
464+
await expect(page.locator('.test-style-server-manual')).toHaveCSS(
465+
'color',
466+
'rgb(255, 165, 0)',
467+
)
456468
})
457469

458470
// TODO: need a way to add/remove links on server hmr. for now, it requires a manually reload.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.test-style-server-manual {
2+
color: orange;
3+
}

packages/plugin-rsc/examples/basic/src/routes/style-server/server.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ export function TestStyleServer() {
88
<div data-testid="css-module-server" className={styles.server}>
99
test-css-module-server
1010
</div>
11+
<link
12+
rel="stylesheet"
13+
href="/test.css"
14+
precedence="test-style-server-manual"
15+
/>
16+
<div className="test-style-server-manual">test-style-server-manual</div>
1117
</>
1218
)
1319
}

packages/plugin-rsc/examples/basic/vite.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ export default { fetch: handler };
123123
source: `\
124124
/favicon.ico
125125
Cache-Control: public, max-age=3600, s-maxage=3600
126+
/test.css
127+
Cache-Control: public, max-age=3600, s-maxage=3600
126128
/assets/*
127129
Cache-Control: public, max-age=31536000, immutable
128130
`,

packages/plugin-rsc/src/plugin.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -829,14 +829,16 @@ window.__vite_plugin_react_preamble_installed__ = true;
829829
const resolvedEntry = await this.resolve(source)
830830
assert(resolvedEntry, `[vite-rsc] failed to resolve entry '${source}'`)
831831
code += `await import(${JSON.stringify(resolvedEntry.id)});`
832-
// TODO
833-
// should remove only the ones we injected during ssr, which are duplicated by browser imports for HMR.
834-
// technically this doesn't have to wait for "vite:beforeUpdate" and should do it right after browser css import.
835-
// TODO: there migth be a clever way to let Vite deduplicate itself.
836-
// cf. https://github.com/withastro/astro/blob/acb9b302f56e38833a1ab01147f7fde0bf967889/packages/astro/src/vite-plugin-astro-server/pipeline.ts#L133-L135
837-
code += `
832+
// TODO: this doesn't have to wait for "vite:beforeUpdate" and should do it right after browser css import.
833+
code += /* js */ `
838834
const ssrCss = document.querySelectorAll("link[rel='stylesheet']");
839-
import.meta.hot.on("vite:beforeUpdate", () => ssrCss.forEach(node => node.remove()));
835+
import.meta.hot.on("vite:beforeUpdate", () => {
836+
ssrCss.forEach(node => {
837+
if (node.dataset.precedence?.startsWith("vite-rsc/")) {
838+
node.remove();
839+
}
840+
});
841+
});
840842
`
841843
// close error overlay after syntax error is fixed and hmr is triggered.
842844
// https://github.com/vitejs/vite/blob/8033e5bf8d3ff43995d0620490ed8739c59171dd/packages/vite/src/client/client.ts#L318-L320

packages/plugin-rsc/src/ssr.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ function preloadDeps(deps: ResolvedAssetDeps) {
6060
})
6161
}
6262
for (const href of deps.css) {
63-
ReactDOM.preinit(href, { as: 'style' })
63+
ReactDOM.preinit(href, {
64+
as: 'style',
65+
precedence: 'vite-rsc/client-reference',
66+
})
6467
}
6568
}

0 commit comments

Comments
 (0)