Skip to content

Commit e37788b

Browse files
authored
refactor(rsc): self-accept css module direct request module on client environment (#842)
1 parent a4bc2e0 commit e37788b

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

packages/plugin-rsc/src/plugin.ts

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { generateEncryptionKey, toBase64 } from './utils/encryption-utils'
3434
import { createRpcServer } from './utils/rpc'
3535
import {
3636
cleanUrl,
37+
directRequestRE,
3738
evalValue,
3839
normalizeViteImportAnalysisUrl,
3940
prepareError,
@@ -563,11 +564,7 @@ export default function vitePluginRsc(
563564
async hotUpdate(ctx) {
564565
if (isCSSRequest(ctx.file)) {
565566
if (this.environment.name === 'client') {
566-
// filter out `.css?direct` (injected by SSR) to avoid browser full reload
567-
// when changing non-self accepting css such as `module.css`.
568-
return ctx.modules.filter(
569-
(m) => !(m.id?.includes('?direct') && !m.isSelfAccepting),
570-
)
567+
return
571568
}
572569
}
573570

@@ -1976,6 +1973,29 @@ function vitePluginRscCss(
19761973
}
19771974
},
19781975
},
1976+
{
1977+
// force self accepting "?direct" css (injected via SSR `<link />`) to avoid full reload.
1978+
// this should only apply to css modules
1979+
// https://github.com/vitejs/vite/blob/84079a84ad94de4c1ef4f1bdb2ab448ff2c01196/packages/vite/src/node/plugins/css.ts#L1096
1980+
name: 'rsc:rsc-css-self-accept',
1981+
apply: 'serve',
1982+
transform: {
1983+
order: 'post',
1984+
handler(_code, id, _options) {
1985+
if (
1986+
this.environment.name === 'client' &&
1987+
this.environment.mode === 'dev' &&
1988+
isCSSRequest(id) &&
1989+
directRequestRE.test(id)
1990+
) {
1991+
const mod = this.environment.moduleGraph.getModuleById(id)
1992+
if (mod && !mod.isSelfAccepting) {
1993+
mod.isSelfAccepting = true
1994+
}
1995+
}
1996+
},
1997+
},
1998+
},
19791999
{
19802000
name: 'rsc:css-virtual',
19812001
resolveId(source) {

packages/plugin-rsc/src/plugins/vite-utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,6 @@ export function evalValue<T = any>(rawValue: string): T {
161161
`)
162162
return fn()
163163
}
164+
165+
// https://github.com/vitejs/vite/blob/84079a84ad94de4c1ef4f1bdb2ab448ff2c01196/packages/vite/src/node/utils.ts#L321
166+
export const directRequestRE: RegExp = /(\?|&)direct=?(?:&|$)/

0 commit comments

Comments
 (0)