0
Fork 0
mirror of https://github.com/TanStack/router.git synced 2026-11-16 19:38:07 +00:00
router/e2e/react-router/basic-file-based-code-splitting
Manuel Schiller 523972e0df
fix: shared virtual module for code-split routes (#6659)
* fix: shared virtual module for code-split routes

When a route file has module-level bindings (variables, functions, classes) referenced by **both** split and non-split route properties, the bindings are duplicated — initialized independently in the reference file and in each virtual split file. This breaks singletons, causes double side effects, and wastes bundle size.

```tsx
const collection = { name: 'todos', preload: async () => {} }
export const Route = createFileRoute('/todos')({
  loader: async () => {
    await collection.preload()
  }, // stays in reference
  component: () => <div>{collection.name}</div>, // split to virtual
})
```
In this example, `collection` is initialized in **both** the reference file (because `loader` references it) and the `?tsr-split=component` virtual file (because the component references it). Two separate instances exist at runtime.

To solve this issue, this PR introduces a third virtual module `route.tsx?tsr-shared=1` that contains shared bindings. Both the reference and virtual files import from it.

When `sharedBindings` is empty (the common case — component-only helpers, loader-only code), no shared module exists and the system works exactly as today.

The reference transform runs first and will emit a static import of `?tsr-shared=1` **only when needed**. Since split chunks are only discovered via the reference file's emitted `import('...?tsr-split=...')`, the bundler will always process/evaluate the reference module first. Result: the shared module is evaluated once and then reused by any later-loaded split chunk.
History 2026-02-14 13:10:54 +01:00
..
src fix: shared virtual module for code-split routes (#6659) 2026-02-14 13:10:54 +01:00
tests fix: shared virtual module for code-split routes (#6659) 2026-02-14 13:10:54 +01:00
.gitignore chore(e2e): move hash related test utils to the shared package (#3782) 2025-03-16 14:24:39 +13:00
index.html chore(e2e,examples): tailwindcss v4 (#3235) 2025-01-26 17:50:21 +13:00
package.json chore: bump deps (#6408) 2026-01-18 02:14:48 +01:00
playwright.config.ts chore: use "preview" instead of "serve" to refer to "vite preview" (#5917) 2025-11-20 05:08:43 +01:00
tsconfig.json test(e2e): use locally hosted api over an external provider (#4725) 2025-07-22 00:15:28 +02:00
vite.config.ts docs: move to tailwind 4 w/ vite (#6168) 2025-12-21 04:13:49 +01:00