Skip to content

Commit a82cec6

Browse files
fix(start): avoid encoded virtual adapter ids in vite dev (#7484)
1 parent abd44dd commit a82cec6

16 files changed

Lines changed: 133 additions & 61 deletions

File tree

‎.changeset/clean-adapters-proxy.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@tanstack/start-plugin-core': patch
3+
'@tanstack/start-client-core': patch
4+
'@tanstack/start-server-core': patch
5+
'@tanstack/react-start': patch
6+
'@tanstack/solid-start': patch
7+
'@tanstack/vue-start': patch
8+
---
9+
10+
Fix serialization adapter module resolution in TanStack Start. Vite dev now uses clean runtime-specific virtual module IDs instead of browser requests containing encoded null-byte virtual IDs, which avoids reverse proxy failures. When no serialization adapters are configured, Vite and Rsbuild now resolve `#tanstack-start-plugin-adapters` through the package empty-adapter fallback.

‎packages/start-client-core/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
},
9292
"#tanstack-start-plugin-adapters": {
9393
"types": "./src/start-entry.d.ts",
94-
"default": "./dist/esm/fake-entries/plugin-adapters.js"
94+
"default": "./dist/esm/empty-plugin-adapters.js"
9595
}
9696
},
9797
"sideEffects": false,

packages/start-client-core/src/fake-entries/plugin-adapters.ts renamed to packages/start-client-core/src/empty-plugin-adapters.ts

File renamed without changes.

‎packages/start-client-core/vite.config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default defineConfig(({ command }) =>
3030
'./src/hydration/runtime.ts',
3131
'./src/fake-entries/start.ts',
3232
'./src/fake-entries/router.ts',
33-
'./src/fake-entries/plugin-adapters.ts',
33+
'./src/empty-plugin-adapters.ts',
3434
],
3535
cjs: false,
3636
externalDeps: [

‎packages/start-plugin-core/src/import-protection/utils.ts‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,6 @@ export function clearNormalizeFilePathCache(): void {
9292
normalizeFilePathCache.clear()
9393
}
9494

95-
export function escapeRegExp(s: string): string {
96-
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
97-
}
98-
9995
/** Get a value from a Map, creating it with `factory` if absent. */
10096
export function getOrCreate<TKey, TValue>(
10197
map: Map<TKey, TValue>,

‎packages/start-plugin-core/src/rsbuild/plugin.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
applyResolvedRouterBasepath,
88
createStartConfigContext,
99
} from '../config-context'
10-
import { normalizePath } from '../utils'
10+
import { escapeRegExp, normalizePath } from '../utils'
1111
import { createServerFnBasePath, normalizePublicBase } from '../planning'
1212
import { parseStartConfig, rsbuildClientOutputSchema } from './schema'
1313
import {
@@ -760,10 +760,6 @@ export function tanStackStartRsbuild(
760760
}
761761
}
762762

763-
function escapeRegExp(value: string): string {
764-
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
765-
}
766-
767763
const defaultRspackWatchIgnored = /[\\/](?:\.git|node_modules)[\\/]/
768764

769765
function seedResolveModules(

‎packages/start-plugin-core/src/rsbuild/virtual-modules.ts‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { VIRTUAL_MODULES } from '@tanstack/start-server-core'
22
import { generateSerializationAdaptersModule } from '../serialization-adapters-module'
33
import { generateServerFnResolverModule } from '../start-compiler/server-fn-resolver-module'
44
import { buildStartManifest } from '../start-manifest-plugin/manifestBuilder'
5+
import { escapeRegExp } from '../utils'
56
import { RSBUILD_ENVIRONMENT_NAMES } from './planning'
67
import type {
78
RsbuildPluginAPI,
@@ -265,6 +266,7 @@ export function registerVirtualModules(
265266
const hasSeparateProviderEnvironment =
266267
!opts.rscEnabled &&
267268
opts.providerEnvName !== RSBUILD_ENVIRONMENT_NAMES.server
269+
const hasSerializationAdapters = Boolean(opts.serializationAdapters?.length)
268270

269271
function isProviderEnvironment(environmentName: string): boolean {
270272
return environmentName === opts.providerEnvName
@@ -384,14 +386,16 @@ export function registerVirtualModules(
384386
content[paths.serverFnResolver] = 'export {}'
385387
}
386388

387-
// Plugin adapters — both environments get environment-specific content
388-
content[paths.pluginAdapters] = generateSerializationAdaptersModule({
389-
adapters: opts.serializationAdapters,
390-
runtime:
391-
environmentName === RSBUILD_ENVIRONMENT_NAMES.client
392-
? 'client'
393-
: 'server',
394-
})
389+
if (hasSerializationAdapters) {
390+
// Plugin adapters — both environments get environment-specific content
391+
content[paths.pluginAdapters] = generateSerializationAdaptersModule({
392+
adapters: opts.serializationAdapters,
393+
runtime:
394+
environmentName === RSBUILD_ENVIRONMENT_NAMES.client
395+
? 'client'
396+
: 'server',
397+
})
398+
}
395399

396400
// --- RSC virtual modules ---
397401
if (rscPaths) {
@@ -430,7 +434,10 @@ export function createFromReadableStream() { throw new Error('RSC SSR decode is
430434
const aliasMap: Record<string, string> = {
431435
[VIRTUAL_MODULES.startManifest]: paths.manifest,
432436
[VIRTUAL_MODULES.serverFnResolver]: paths.serverFnResolver,
433-
[VIRTUAL_MODULES.pluginAdapters]: paths.pluginAdapters,
437+
}
438+
439+
if (hasSerializationAdapters) {
440+
aliasMap[VIRTUAL_MODULES.pluginAdapters] = paths.pluginAdapters
434441
}
435442

436443
// Add RSC virtual module aliases
@@ -470,15 +477,18 @@ export function createFromReadableStream() { throw new Error('RSC SSR decode is
470477

471478
// Rewrite scheme-like IDs to the VirtualModulesPlugin-backed file paths.
472479
for (const [moduleId, virtualFilePath] of Object.entries(aliasMap)) {
473-
const escaped = moduleId.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
474480
const NMR = utils.rspack.NormalModuleReplacementPlugin
475-
config.plugins.push(new NMR(new RegExp(`^${escaped}$`), virtualFilePath))
481+
config.plugins.push(
482+
new NMR(new RegExp(`^${escapeRegExp(moduleId)}$`), virtualFilePath),
483+
)
476484
}
477485

478486
const resolve = config.resolve
479487
const resolveAlias = (resolve.alias ??= {}) as Record<string, string>
480488
resolveAlias[VIRTUAL_MODULES.serverFnResolver] = paths.serverFnResolver
481-
resolveAlias[VIRTUAL_MODULES.pluginAdapters] = paths.pluginAdapters
489+
if (hasSerializationAdapters) {
490+
resolveAlias[VIRTUAL_MODULES.pluginAdapters] = paths.pluginAdapters
491+
}
482492

483493
// Add RSC-specific resolve aliases
484494
if (rscPaths) {

‎packages/start-plugin-core/src/utils.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ export function resolveViteId(id: string) {
1313
return `\0${id}`
1414
}
1515

16+
export function createIdFilter(ids: Array<string>): RegExp {
17+
return new RegExp(`^(?:${ids.map(escapeRegExp).join('|')})$`)
18+
}
19+
20+
export function escapeRegExp(value: string): string {
21+
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
22+
}
23+
1624
export function createLogger(prefix: string) {
1725
const label = `[${prefix}]`
1826
return {

‎packages/start-plugin-core/src/vite/createVirtualModule.ts‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
import { resolveViteId } from '../utils'
1+
import { createIdFilter, resolveViteId } from '../utils'
22
import type { Plugin } from 'vite'
33

44
type VirtualModuleLoadHandler = (
55
this: any,
66
id: string,
77
) => string | null | undefined | Promise<string | null | undefined>
88

9-
function escapeRegExp(value: string): string {
10-
return value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
11-
}
12-
139
export function createVirtualModule(opts: {
1410
name: string
1511
moduleId: string
@@ -31,7 +27,7 @@ export function createVirtualModule(opts: {
3127
enforce: opts.enforce,
3228
sharedDuringBuild: opts.sharedDuringBuild,
3329
resolveId: {
34-
filter: { id: new RegExp(escapeRegExp(opts.moduleId)) },
30+
filter: { id: createIdFilter([opts.moduleId]) },
3531
handler(id) {
3632
if (id === opts.moduleId) {
3733
return resolvedId
@@ -41,7 +37,7 @@ export function createVirtualModule(opts: {
4137
},
4238
},
4339
load: {
44-
filter: { id: new RegExp(escapeRegExp(resolvedId)) },
40+
filter: { id: createIdFilter([resolvedId]) },
4541
handler(id) {
4642
if (id !== resolvedId) {
4743
return undefined

‎packages/start-plugin-core/src/vite/import-protection-plugin/plugin.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { writeFileSync } from 'node:fs'
22
import { normalizePath } from 'vite'
33
import { dirname, relative } from 'pathe'
44

5-
import { resolveViteId } from '../../utils'
5+
import { escapeRegExp, resolveViteId } from '../../utils'
66
import { VITE_ENVIRONMENT_NAMES } from '../../constants'
77
import {
88
ImportGraph,
@@ -29,7 +29,6 @@ import {
2929
debugLog,
3030
dedupePatterns,
3131
dedupeViolationKey,
32-
escapeRegExp,
3332
getOrCreate,
3433
isFileExcluded,
3534
isInsideDirectory,

0 commit comments

Comments
 (0)