Skip to content

Commit 5b28ba5

Browse files
authored
fix(rsc): inject __vite_rsc_importer_resources import only once (#742)
1 parent e1d1eea commit 5b28ba5

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

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

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect, test } from '@playwright/test'
2-
import { useFixture } from './fixture'
2+
import { setupInlineFixture, useFixture, type Fixture } from './fixture'
33
import { defineStarterTest } from './starter'
4-
import { waitForHydration } from './helper'
4+
import { expectNoPageError, waitForHydration } from './helper'
55

66
test.describe('dev-default', () => {
77
const f = useFixture({ root: 'examples/starter', mode: 'dev' })
@@ -53,3 +53,44 @@ test.describe('build-development', () => {
5353
expect(output).toContain('jsxDEV')
5454
})
5555
})
56+
57+
test.describe('duplicate loadCss', () => {
58+
const root = 'examples/e2e/temp/duplicate-load-css'
59+
test.beforeAll(async () => {
60+
await setupInlineFixture({
61+
src: 'examples/starter',
62+
dest: root,
63+
files: {
64+
'src/root.tsx': {
65+
edit: (s) =>
66+
s.replace(
67+
'</head>',
68+
() =>
69+
`\
70+
{import.meta.viteRsc.loadCss()}
71+
{import.meta.viteRsc.loadCss()}
72+
</head>`,
73+
),
74+
},
75+
},
76+
})
77+
})
78+
79+
test.describe('dev', () => {
80+
const f = useFixture({ root, mode: 'dev' })
81+
defineTest(f)
82+
})
83+
84+
test.describe('build', () => {
85+
const f = useFixture({ root, mode: 'build' })
86+
defineTest(f)
87+
})
88+
89+
function defineTest(f: Fixture) {
90+
test('basic', async ({ page }) => {
91+
using _ = expectNoPageError(page)
92+
await page.goto(f.url())
93+
await waitForHydration(page)
94+
})
95+
}
96+
})

packages/plugin-rsc/src/plugin.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1800,6 +1800,7 @@ export function vitePluginRscCss(
18001800

18011801
assert(this.environment.name === 'rsc')
18021802
const output = new MagicString(code)
1803+
let importAdded = false
18031804

18041805
for (const match of code.matchAll(
18051806
/import\.meta\.viteRsc\.loadCss\(([\s\S]*?)\)/dg,
@@ -1833,7 +1834,11 @@ export function vitePluginRscCss(
18331834
})`
18341835
} else {
18351836
const hash = hashString(importId)
1836-
if (!code.includes(`__vite_rsc_importer_resources_${hash}`)) {
1837+
if (
1838+
!importAdded &&
1839+
!code.includes(`__vite_rsc_importer_resources_${hash}`)
1840+
) {
1841+
importAdded = true
18371842
output.prepend(
18381843
`import * as __vite_rsc_importer_resources_${hash} from ${JSON.stringify(
18391844
importId,

0 commit comments

Comments
 (0)