Skip to content

Commit f0359c4

Browse files
authored
fix(rsc): replace ?v= check with more robust node_modules detection (#696)
1 parent c329914 commit f0359c4

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

packages/plugin-rsc/src/plugin.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ import {
3232
} from './transforms'
3333
import { generateEncryptionKey, toBase64 } from './utils/encryption-utils'
3434
import { createRpcServer } from './utils/rpc'
35-
import { normalizeViteImportAnalysisUrl, prepareError } from './vite-utils'
35+
import {
36+
cleanUrl,
37+
normalizeViteImportAnalysisUrl,
38+
prepareError,
39+
} from './vite-utils'
3640
import { cjsModuleRunnerPlugin } from './plugins/cjs'
3741
import { evalValue, parseIdQuery } from './plugins/utils'
3842

@@ -959,9 +963,12 @@ function vitePluginUseClient(
959963
let importId: string
960964
let referenceKey: string
961965
const packageSource = packageSources.get(id)
962-
if (!packageSource && id.includes('?v=')) {
963-
assert(this.environment.mode === 'dev')
964-
// If non package source `?v=<hash>` reached here, this is a client boundary
966+
if (
967+
!packageSource &&
968+
this.environment.mode === 'dev' &&
969+
id.includes('/node_modules/')
970+
) {
971+
// If non package source reached here (often with ?v=... query), this is a client boundary
965972
// created by a package imported on server environment, which breaks the
966973
// expectation on dependency optimizer on browser. Directly copying over
967974
// "?v=<hash>" from client optimizer in client reference can make a hashed
@@ -978,9 +985,7 @@ function vitePluginUseClient(
978985
`[vite-rsc] detected an internal client boundary created by a package imported on rsc environment`,
979986
)
980987
}
981-
importId = `/@id/__x00__virtual:vite-rsc/client-in-server-package-proxy/${encodeURIComponent(
982-
id.split('?v=')[0]!,
983-
)}`
988+
importId = `/@id/__x00__virtual:vite-rsc/client-in-server-package-proxy/${encodeURIComponent(cleanUrl(id))}`
984989
referenceKey = importId
985990
} else if (packageSource) {
986991
if (this.environment.mode === 'dev') {
@@ -1223,8 +1228,10 @@ function vitePluginUseServer(
12231228
let normalizedId_: string | undefined
12241229
const getNormalizedId = () => {
12251230
if (!normalizedId_) {
1226-
if (id.includes('?v=')) {
1227-
assert(this.environment.mode === 'dev')
1231+
if (
1232+
this.environment.mode === 'dev' &&
1233+
id.includes('/node_modules/')
1234+
) {
12281235
const ignored =
12291236
useServerPluginOptions.ignoredPackageWarnings?.some(
12301237
(pattern) =>
@@ -1238,8 +1245,8 @@ function vitePluginUseServer(
12381245
)
12391246
}
12401247
// module runner has additional resolution step and it's not strict about
1241-
// module identity of `import(id)` like browser, so we simply strip it off.
1242-
id = id.split('?v=')[0]!
1248+
// module identity of `import(id)` like browser, so we simply strip queries such as `?v=`.
1249+
id = cleanUrl(id)
12431250
}
12441251
if (config.command === 'build') {
12451252
normalizedId_ = hashString(path.relative(config.root, id))

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ export function parseIdQuery(id: string): {
1919
const query = Object.fromEntries(new URLSearchParams(rawQuery))
2020
return { filename, query }
2121
}
22+
23+
// https://github.com/vitejs/vite/blob/946831f986cb797009b8178659d2b31f570c44ff/packages/vite/src/shared/utils.ts#L31-L34
24+
const postfixRE = /[?#].*$/
25+
export function cleanUrl(url: string): string {
26+
return url.replace(postfixRE, '')
27+
}

0 commit comments

Comments
 (0)