Skip to content

Commit e2ab690

Browse files
move entry scripts to root
1 parent 23c66b6 commit e2ab690

13 files changed

Lines changed: 377 additions & 518 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
'@tanstack/start-server-core': patch
44
---
55

6-
fix(start): emit `<script>` tags for client entry boot siblings under IIFE. When `scriptFormat: 'iife'` and the entry chunk has static-import siblings (e.g. an extracted runtime via `optimization.runtimeChunk: 'single'`), the manifest now carries vlientEntryImports`and`<Scripts />`emits a`<script>` for each before the entry, fixing hydration for IIFE bundles with extracted runtimes.
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.

‎e2e/react-start/custom-server-rsbuild/rsbuild.config.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ import { tanstackStart } from '@tanstack/react-start/plugin/rsbuild'
88
// repro is:
99
//
1010
// - `client.output: 'iife'` — emit the client entry as a self-executing
11-
// script. `<Scripts />` reads `scriptFormat` from the manifest and
12-
// switches between `<script type="module">` and plain `<script>`;
13-
// setting IIFE here exercises the plain-script path.
11+
// script. The manifest uses plain script tags and classic script preloads;
12+
// setting IIFE here exercises that non-module asset path.
1413
//
1514
// - Client `runtimeChunk: 'single'` — extracts the webpack runtime into
1615
// its own chunk. With IIFE plain scripts, the entry can't bootstrap
1716
// until the runtime has executed, so `<Scripts />` has to emit a
1817
// `<script>` for the runtime chunk (not just a preload). This was
19-
// the regression the upstream `clientEntryImports` fix addresses.
18+
// the regression this fixture covers.
2019
//
2120
// - `client.distPath.root` + `distPath.js: ''` — flat layout, JS at the
2221
// dist root. Matches the path `express-server.ts` serves via

‎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 & 25 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,8 +53,6 @@ type DedupeRoute = {
5853
export interface StartManifest {
5954
scriptFormat?: ScriptFormat
6055
routes: Record<string, RouteTreeRoute>
61-
clientEntry: string
62-
clientEntryImports?: Array<string>
6356
inlineCss?: {
6457
styles: Record<string, string>
6558
templates?: Record<string, InlineCssTemplate>
@@ -188,6 +181,56 @@ function appendRouteStylesheets(
188181
route.css = appendUniqueStylesheets(route.css, stylesheets)
189182
}
190183

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+
191234
function appendAdditionalRouteEntries(
192235
route: RouteTreeRoute,
193236
entries: ReadonlyArray<AdditionalRouteManifestEntry>,
@@ -208,9 +251,7 @@ function appendAdditionalRouteEntries(
208251
}
209252

210253
appendRouteStylesheets(route, stylesheets)
211-
if (scripts.length > 0) {
212-
route.scripts = [...(route.scripts ?? []), ...scripts]
213-
}
254+
appendRouteScripts(route, scripts)
214255
}
215256

216257
export function buildStartManifest(options: {
@@ -235,6 +276,13 @@ export function buildStartManifest(options: {
235276
additionalRouteAssets: options.additionalRouteAssets,
236277
})
237278

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

240288
// Prune routes with no manifest data
@@ -250,17 +298,10 @@ export function buildStartManifest(options: {
250298

251299
const result: StartManifest = {
252300
routes,
253-
clientEntry: assetResolvers.getAssetPath(scannedChunks.entryChunk.fileName),
254301
}
255302

256303
if (options.scriptFormat === 'iife') {
257304
result.scriptFormat = 'iife'
258-
259-
if (scannedChunks.entryChunk.imports.length > 0) {
260-
result.clientEntryImports = scannedChunks.entryChunk.imports.map(
261-
(importedFileName) => assetResolvers.getAssetPath(importedFileName),
262-
)
263-
}
264305
}
265306

266307
if (options.inlineCss?.enabled) {
@@ -511,7 +552,7 @@ export function buildRouteManifestRoutes(options: {
511552
for (const [routeId, route] of Object.entries(options.routeTreeRoutes)) {
512553
if (!route.filePath) {
513554
if (routeId === rootRouteId) {
514-
routes[routeId] = route
555+
routes[routeId] = { ...route }
515556
continue
516557
}
517558

@@ -520,7 +561,7 @@ export function buildRouteManifestRoutes(options: {
520561

521562
const chunks = options.routeChunksByFilePath.get(route.filePath)
522563
if (!chunks) {
523-
routes[routeId] = route
564+
routes[routeId] = { ...route }
524565
continue
525566
}
526567

@@ -641,12 +682,6 @@ function mergeReachableHydrationChunkData(options: {
641682
visitStaticChunk(options.chunk)
642683
}
643684

644-
export {
645-
getRouteFilePathsFromModuleIds,
646-
normalizeViteClientBuild,
647-
normalizeViteClientChunk,
648-
}
649-
650685
function dedupeNestedRouteManifestEntries(
651686
routeId: string,
652687
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,12 +5,12 @@ 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'
1211
import type { GetConfigFn, NormalizedClientBuild } from '../../types'
1312
import type { PluginOption, Rollup } from 'vite'
13+
import { normalizeViteClientBuild } from './normalized-client-build'
1414

1515
type StartManifestEnvironment = {
1616
config: {
@@ -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
})

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

Lines changed: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ import {
66
buildStartManifest,
77
createChunkCssAssetCollector,
88
createManifestAssetResolvers,
9-
getRouteFilePathsFromModuleIds,
10-
normalizeViteClientBuild,
11-
normalizeViteClientChunk,
129
serializeStartManifest,
1310
scanClientChunks,
1411
type StartManifest,
1512
} from '../../src/start-manifest-plugin/manifestBuilder'
1613
import type { ManifestCssLink } from '@tanstack/router-core'
1714
import type { Rollup } from 'vite'
15+
import {
16+
getRouteFilePathsFromModuleIds,
17+
normalizeViteClientBuild,
18+
normalizeViteClientChunk,
19+
} from '../../src/vite/start-manifest-plugin/normalized-client-build'
1820

1921
function normalizeTestBuild(bundle: Rollup.OutputBundle) {
2022
return normalizeViteClientBuild(bundle)
@@ -461,7 +463,16 @@ describe('buildStartManifest', () => {
461463
},
462464
})
463465

464-
expect(manifest.routes.__root__!.scripts).toEqual([script])
466+
expect(manifest.routes.__root__!.scripts).toEqual([
467+
script,
468+
{
469+
attrs: {
470+
type: 'module',
471+
async: true,
472+
src: '/assets/entry.js',
473+
},
474+
},
475+
])
465476
})
466477

467478
test('dedupes additional route scripts already owned by ancestor routes', () => {
@@ -492,7 +503,16 @@ describe('buildStartManifest', () => {
492503
},
493504
})
494505

495-
expect(manifest.routes.__root__!.scripts).toEqual([script])
506+
expect(manifest.routes.__root__!.scripts).toEqual([
507+
script,
508+
{
509+
attrs: {
510+
type: 'module',
511+
async: true,
512+
src: '/assets/entry.js',
513+
},
514+
},
515+
])
496516
expect(manifest.routes['/about']?.scripts).toBeUndefined()
497517
})
498518

@@ -892,7 +912,6 @@ describe('buildStartManifest', () => {
892912
preloads: ['/assets/c.js'],
893913
},
894914
},
895-
clientEntry: '/assets/entry.js',
896915
}
897916

898917
const evaluated = deserializeSerializedManifest(
@@ -930,7 +949,6 @@ describe('buildStartManifest', () => {
930949
],
931950
},
932951
},
933-
clientEntry: '/assets/entry.js',
934952
}
935953

936954
expect(
@@ -950,7 +968,6 @@ describe('buildStartManifest', () => {
950968
preloads: ['/assets/posts.js'],
951969
},
952970
},
953-
clientEntry: '/assets/entry.js',
954971
}
955972

956973
expect(
@@ -987,6 +1004,64 @@ describe('buildStartManifest', () => {
9871004
}).scriptFormat,
9881005
).toBe('iife')
9891006
})
1007+
1008+
test('buildStartManifest emits client entry assets as root route scripts', () => {
1009+
const entryChunk = makeChunk({
1010+
fileName: 'entry.js',
1011+
isEntry: true,
1012+
imports: ['runtime.js', 'vendor.js'],
1013+
})
1014+
const runtimeChunk = makeChunk({ fileName: 'runtime.js' })
1015+
const vendorChunk = makeChunk({ fileName: 'vendor.js' })
1016+
const routeTreeRoutes = {
1017+
__root__: {
1018+
filePath: '/routes/__root.tsx',
1019+
},
1020+
}
1021+
1022+
const moduleManifest = buildStartManifest({
1023+
clientBuild: normalizeTestBuild({
1024+
'entry.js': entryChunk,
1025+
'runtime.js': runtimeChunk,
1026+
'vendor.js': vendorChunk,
1027+
}),
1028+
routeTreeRoutes,
1029+
basePath: '/assets',
1030+
scriptFormat: 'module',
1031+
})
1032+
1033+
expect(moduleManifest.routes.__root__?.scripts).toEqual([
1034+
{
1035+
attrs: {
1036+
type: 'module',
1037+
async: true,
1038+
src: '/assets/entry.js',
1039+
},
1040+
},
1041+
])
1042+
1043+
const iifeManifest = buildStartManifest({
1044+
clientBuild: normalizeTestBuild({
1045+
'entry.js': entryChunk,
1046+
'runtime.js': runtimeChunk,
1047+
'vendor.js': vendorChunk,
1048+
}),
1049+
routeTreeRoutes,
1050+
basePath: '/assets',
1051+
scriptFormat: 'iife',
1052+
})
1053+
1054+
expect(iifeManifest.routes.__root__?.preloads).toEqual([
1055+
'/assets/entry.js',
1056+
'/assets/runtime.js',
1057+
'/assets/vendor.js',
1058+
])
1059+
expect(iifeManifest.routes.__root__?.scripts).toEqual([
1060+
{ attrs: { async: true, src: '/assets/runtime.js' } },
1061+
{ attrs: { async: true, src: '/assets/vendor.js' } },
1062+
{ attrs: { async: true, src: '/assets/entry.js' } },
1063+
])
1064+
})
9901065
})
9911066

9921067
describe('route tree dedupe in buildStartManifest', () => {

0 commit comments

Comments
 (0)