Skip to content

Commit 9c09bca

Browse files
fix(start): emit boot-sibling chunks as scripts for IIFE entries (#7501)
Co-authored-by: Keven Arroyo <[email protected]>
1 parent bae50be commit 9c09bca

13 files changed

Lines changed: 436 additions & 488 deletions

File tree

‎.changeset/seven-times-pump.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@tanstack/start-plugin-core': patch
3+
'@tanstack/start-server-core': patch
4+
---
5+
6+
fix(start): emit client entry scripts from the root route manifest. When `scriptFormat: 'iife'` and the entry chunk has static-import siblings (e.g. an extracted runtime via `optimization.runtimeChunk: 'single'`), the manifest now includes those async siblings before the async client entry in root route scripts, fixing hydration for IIFE bundles with extracted runtimes.
Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,65 @@
1+
import path from 'node:path'
12
import { defineConfig } from '@rsbuild/core'
23
import { pluginReact } from '@rsbuild/plugin-react'
34
import { tanstackStart } from '@tanstack/react-start/plugin/rsbuild'
45

6+
// Stress-test fixture for `<Scripts />` and the start manifest under a
7+
// non-default client chunk layout. The combination that matters for the
8+
// repro is:
9+
//
10+
// - `client.output: 'iife'` — emit the client entry as a self-executing
11+
// script. The manifest uses plain script tags and classic script preloads;
12+
// setting IIFE here exercises that non-module asset path.
13+
//
14+
// - Client `runtimeChunk: 'single'` — extracts the webpack runtime into
15+
// its own chunk. With IIFE plain scripts, the entry can't bootstrap
16+
// until the runtime has executed, so `<Scripts />` has to emit a
17+
// `<script>` for the runtime chunk (not just a preload). This was
18+
// the regression this fixture covers.
19+
//
20+
// - `client.distPath.root` + `distPath.js: ''` — flat layout, JS at the
21+
// dist root. Matches the path `express-server.ts` serves via
22+
// `express.static('dist/client')`.
23+
//
24+
// - `performance.buildCache: true` — exercise the rspack persistent
25+
// cache, including warm-restart paths.
26+
//
27+
// - `output.assetPrefix: '/static/'` — force manifest URLs through an
28+
// explicit prefix.
529
export default defineConfig({
630
plugins: [
7-
pluginReact({ splitChunks: false }),
8-
tanstackStart({ rsbuild: { installDevServerMiddleware: false } }),
31+
pluginReact(),
32+
tanstackStart({
33+
rsbuild: {
34+
installDevServerMiddleware: false,
35+
client: {
36+
output: 'iife',
37+
},
38+
},
39+
}),
940
],
41+
performance: {
42+
buildCache: true,
43+
},
44+
output: {
45+
assetPrefix: '/static/',
46+
},
47+
environments: {
48+
client: {
49+
output: {
50+
distPath: {
51+
root: path.resolve(__dirname, 'dist/client'),
52+
js: '',
53+
},
54+
},
55+
tools: {
56+
rspack: (config) => {
57+
config.optimization = {
58+
...(config.optimization ?? {}),
59+
runtimeChunk: 'single',
60+
}
61+
},
62+
},
63+
},
64+
},
1065
})

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,27 @@ function getScriptFormatProperty(scriptFormat: ScriptFormat): string {
7272
return scriptFormat === 'iife' ? ` scriptFormat: 'iife',\n` : ''
7373
}
7474

75+
function getEntryScriptAttrs(
76+
entryUrl: string,
77+
scriptFormat: ScriptFormat,
78+
): string {
79+
return scriptFormat === 'module'
80+
? `{ type: 'module', async: true, src: '${entryUrl}' }`
81+
: `{ async: true, src: '${entryUrl}' }`
82+
}
83+
7584
function generateManifestModuleDev(
7685
devClientEntryUrl: string,
7786
scriptFormat: ScriptFormat,
7887
): string {
7988
const scriptFormatProperty = getScriptFormatProperty(scriptFormat)
8089
return `const fallbackManifest = {
81-
${scriptFormatProperty} routes: {},
82-
clientEntry: '${devClientEntryUrl}',
90+
${scriptFormatProperty} routes: {
91+
__root__: {
92+
preloads: ['${devClientEntryUrl}'],
93+
scripts: [{ attrs: ${getEntryScriptAttrs(devClientEntryUrl, scriptFormat)} }],
94+
},
95+
},
8396
}
8497
export const tsrStartManifest = () => globalThis[${JSON.stringify(DEV_START_MANIFEST_GLOBAL)}] ?? fallbackManifest`
8598
}

‎packages/start-plugin-core/src/start-manifest-plugin/manifestBuilder.ts‎

Lines changed: 60 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@ import {
77
resolveManifestCssLink,
88
rootRouteId,
99
} from '@tanstack/router-core'
10-
import {
11-
getRouteFilePathsFromModuleIds,
12-
normalizeViteClientBuild,
13-
normalizeViteClientChunk,
14-
} from '../vite/start-manifest-plugin/normalized-client-build'
1510
import { processInlineCssUrls } from './inlineCss'
1611
import type {
1712
ManifestAssetLink,
@@ -58,7 +53,6 @@ type DedupeRoute = {
5853
export interface StartManifest {
5954
scriptFormat?: ScriptFormat
6055
routes: Record<string, RouteTreeRoute>
61-
clientEntry: string
6256
inlineCss?: {
6357
styles: Record<string, string>
6458
templates?: Record<string, InlineCssTemplate>
@@ -187,6 +181,56 @@ function appendRouteStylesheets(
187181
route.css = appendUniqueStylesheets(route.css, stylesheets)
188182
}
189183

184+
function appendRouteScripts(
185+
route: RouteTreeRoute,
186+
scripts: Array<ManifestScript>,
187+
) {
188+
if (scripts.length === 0) {
189+
return
190+
}
191+
192+
route.scripts = [...(route.scripts ?? []), ...scripts]
193+
}
194+
195+
function buildScript(src: string, scriptFormat: ScriptFormat): ManifestScript {
196+
return {
197+
attrs: {
198+
...(scriptFormat === 'module' ? { type: 'module' } : {}),
199+
async: true,
200+
src,
201+
},
202+
}
203+
}
204+
205+
function appendEntryChunkScripts(options: {
206+
route: RouteTreeRoute
207+
chunk: NormalizedClientChunk
208+
scriptFormat: ScriptFormat
209+
getAssetPath: (fileName: string) => string
210+
}) {
211+
const scripts: Array<ManifestScript> = []
212+
213+
if (options.scriptFormat === 'iife') {
214+
for (let i = 0; i < options.chunk.imports.length; i++) {
215+
scripts.push(
216+
buildScript(
217+
options.getAssetPath(options.chunk.imports[i]!),
218+
options.scriptFormat,
219+
),
220+
)
221+
}
222+
}
223+
224+
scripts.push(
225+
buildScript(
226+
options.getAssetPath(options.chunk.fileName),
227+
options.scriptFormat,
228+
),
229+
)
230+
231+
appendRouteScripts(options.route, scripts)
232+
}
233+
190234
function appendAdditionalRouteEntries(
191235
route: RouteTreeRoute,
192236
entries: ReadonlyArray<AdditionalRouteManifestEntry>,
@@ -207,9 +251,7 @@ function appendAdditionalRouteEntries(
207251
}
208252

209253
appendRouteStylesheets(route, stylesheets)
210-
if (scripts.length > 0) {
211-
route.scripts = [...(route.scripts ?? []), ...scripts]
212-
}
254+
appendRouteScripts(route, scripts)
213255
}
214256

215257
export function buildStartManifest(options: {
@@ -234,6 +276,13 @@ export function buildStartManifest(options: {
234276
additionalRouteAssets: options.additionalRouteAssets,
235277
})
236278

279+
appendEntryChunkScripts({
280+
route: routes[rootRouteId]!,
281+
chunk: scannedChunks.entryChunk,
282+
scriptFormat: options.scriptFormat ?? 'module',
283+
getAssetPath: assetResolvers.getAssetPath,
284+
})
285+
237286
dedupeNestedRouteManifestEntries(rootRouteId, routes[rootRouteId]!, routes)
238287

239288
// Prune routes with no manifest data
@@ -249,7 +298,6 @@ export function buildStartManifest(options: {
249298

250299
const result: StartManifest = {
251300
routes,
252-
clientEntry: assetResolvers.getAssetPath(scannedChunks.entryChunk.fileName),
253301
}
254302

255303
if (options.scriptFormat === 'iife') {
@@ -504,7 +552,7 @@ export function buildRouteManifestRoutes(options: {
504552
for (const [routeId, route] of Object.entries(options.routeTreeRoutes)) {
505553
if (!route.filePath) {
506554
if (routeId === rootRouteId) {
507-
routes[routeId] = route
555+
routes[routeId] = { ...route }
508556
continue
509557
}
510558

@@ -513,7 +561,7 @@ export function buildRouteManifestRoutes(options: {
513561

514562
const chunks = options.routeChunksByFilePath.get(route.filePath)
515563
if (!chunks) {
516-
routes[routeId] = route
564+
routes[routeId] = { ...route }
517565
continue
518566
}
519567

@@ -634,12 +682,6 @@ function mergeReachableHydrationChunkData(options: {
634682
visitStaticChunk(options.chunk)
635683
}
636684

637-
export {
638-
getRouteFilePathsFromModuleIds,
639-
normalizeViteClientBuild,
640-
normalizeViteClientChunk,
641-
}
642-
643685
function dedupeNestedRouteManifestEntries(
644686
routeId: string,
645687
route: DedupeRoute,

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import { DEV_CLIENT_ENTRY, START_ENVIRONMENT_NAMES } from '../../constants'
55
import {
66
buildStartManifest,
77
createManifestAssetResolvers,
8-
normalizeViteClientBuild,
98
serializeStartManifest,
109
} from '../../start-manifest-plugin/manifestBuilder'
1110
import { createVirtualModule } from '../createVirtualModule'
11+
import { normalizeViteClientBuild } from './normalized-client-build'
1212
import type { GetConfigFn, NormalizedClientBuild } from '../../types'
1313
import type { PluginOption, Rollup } from 'vite'
1414

@@ -137,8 +137,12 @@ function getAssetFileNameByName(
137137

138138
function getEmptyStartManifestModule(clientEntry: string) {
139139
return `export const tsrStartManifest = () => ({
140-
routes: {},
141-
clientEntry: '${clientEntry}',
140+
routes: {
141+
__root__: {
142+
preloads: ['${clientEntry}'],
143+
scripts: [{ attrs: { type: 'module', async: true, src: '${clientEntry}' } }],
144+
},
145+
},
142146
})`
143147
}
144148

‎packages/start-plugin-core/tests/start-manifest-plugin.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ vi.mock('@tanstack/start-server-core', () => ({
1212
describe('startManifestPlugin', () => {
1313
test('uses the virtual client entry during unbundled dev', () => {
1414
expect(loadDevManifest({ bundledDev: false })).toContain(
15-
`clientEntry: '/@id/${DEV_CLIENT_ENTRY}'`,
15+
`src: '/@id/${DEV_CLIENT_ENTRY}'`,
1616
)
1717
})
1818

1919
test('uses the bundled client entry during bundled dev', () => {
2020
expect(loadDevManifest({ bundledDev: true })).toContain(
21-
`clientEntry: '/assets/index.js'`,
21+
`src: '/assets/index.js'`,
2222
)
2323
})
2424
})

0 commit comments

Comments
 (0)