Skip to content

Commit dfb4f9a

Browse files
docs(solid-router): router-monorepo-simple-lazy example (#5853)
* docs(solid-router): router-monorepo-simple-lazy example * lockfile * ci: apply automated fixes --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 1cc3801 commit dfb4f9a

File tree

32 files changed

+720
-0
lines changed

32 files changed

+720
-0
lines changed

docs/router/config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,10 @@
686686
"label": "Monorepo basic",
687687
"to": "framework/solid/examples/router-monorepo-simple"
688688
},
689+
{
690+
"label": "Monorepo basic (with lazy loading)",
691+
"to": "framework/solid/examples/router-monorepo-simple-lazy"
692+
},
689693
{
690694
"label": "Monorepo with Solid Query",
691695
"to": "framework/solid/examples/router-monorepo-solid-query"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
node_modules
2+
.DS_Store
3+
dist
4+
dist-ssr
5+
*.local
6+
*.js
7+
8+
/test-results/
9+
/playwright-report/
10+
/blob-report/
11+
/playwright/.cache/
12+
13+
pnpm-workspace.yaml
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"startCommand": "cp pnpm-workspace.yaml.example pnpm-workspace.yaml && pnpm install && pnpm dev"
3+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"files.watcherExclude": {
3+
"**/routeTree.gen.ts": true
4+
},
5+
"search.exclude": {
6+
"**/routeTree.gen.ts": true
7+
},
8+
"files.readonlyInclude": {
9+
"**/routeTree.gen.ts": true
10+
}
11+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Example of a monorepo with router and feature libraries, using lazy loading
2+
3+
To run this example:
4+
5+
- `npm install` or `yarn`
6+
- `npm dev` or `yarn dev`
7+
8+
A challenge with TanStack Router in a monorepo setup is that it requires TypeScript type augmentations. However, if you set this up directly in the final app, the links inside the libraries won’t be type-safe. To solve this in a monorepo, you need a separate library just for the router, without any components, and then integrate it with the app.
9+
10+
This example showcases this approach using the following packages:
11+
12+
- `packages/router` is the router library
13+
- `packages/post-feature` is the posts UI library
14+
- `packages/app` is the app
15+
16+
With this approach, we can use loaders in the router and the feature library without creating circular dependencies.
17+
18+
Since the router library re-exports the router components, importing them in the feature library ensures they remain type-safe, as they’re linked to the TypeScript augmentations.
19+
20+
Finally, in the app, we can create a map of routes to components ([`packages/app/src/main.tsx`](./packages/app/src/main.tsx)), which ties the router to the components.
21+
22+
## How lazy loading works
23+
24+
Eath feature exports a `createLazyRoute` function that returns a lazy route. This lazy route is then used in the router map to bind the lazy route to the actual route. This allows library to define their component, pending, error and nod found components directly.
25+
26+
The types on the ([`packages/app/src/main.tsx`](./packages/app/src/main.tsx)) are used to map the route to the lazy route, and enforce they match the route path.
27+
28+
**Note: if you match a lazy route with a different id of the route, you will get a runtime error, hence the mapped types to ensure we are also warned by typescript.**
29+
30+
Here is what it looks like in the monorepo:
31+
32+
![graph](./assets/graph.png)
33+
34+
## Stackblitz limitation
35+
36+
### Typescript IDE feedback
37+
38+
Due to a limitation on Stackblitz, the example's types are not properly inferred in the IDE, however as soon as you click on fork in the bottom right corner, the types should be correctly inferred.
71.8 KB
Loading
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "router-solid-mono-simple-lazy",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"post-feature": "pnpm --filter @router-solid-mono-simple-lazy/post-feature",
6+
"router": "pnpm --filter @router-solid-mono-simple-lazy/router",
7+
"app": "pnpm --filter @router-solid-mono-simple-lazy/app",
8+
"dev": "pnpm router build && pnpm post-feature build && pnpm app dev"
9+
},
10+
"dependencies": {
11+
"@tanstack/solid-router": "^1.135.2",
12+
"@tanstack/solid-router-devtools": "^1.135.2",
13+
"@tanstack/router-plugin": "^1.135.2",
14+
"solid-js": "^1.9.10",
15+
"redaxios": "^0.5.1"
16+
},
17+
"devDependencies": {
18+
"@types/node": "^22.7.4",
19+
"vite-plugin-solid": "^2.11.10",
20+
"typescript": "^5.7.2",
21+
"vite": "^7.1.7",
22+
"vite-plugin-dts": "^4.5.4"
23+
},
24+
"keywords": [],
25+
"author": "",
26+
"license": "ISC"
27+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Vite App</title>
7+
</head>
8+
<body>
9+
<div id="app"></div>
10+
<script type="module" src="/src/main.tsx"></script>
11+
</body>
12+
</html>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "@router-solid-mono-simple-lazy/app",
3+
"private": true,
4+
"type": "module",
5+
"scripts": {
6+
"dev": "vite --port=3001",
7+
"build": "vite build && tsc --noEmit",
8+
"serve": "vite preview",
9+
"start": "vite"
10+
},
11+
"dependencies": {
12+
"@router-solid-mono-simple-lazy/post-feature": "workspace:*",
13+
"@router-solid-mono-simple-lazy/router": "workspace:*",
14+
"react": "^19.0.0",
15+
"react-dom": "^19.0.0"
16+
},
17+
"devDependencies": {
18+
"@types/react": "^19.0.8",
19+
"@types/react-dom": "^19.0.3",
20+
"@vitejs/plugin-react": "^4.3.4",
21+
"typescript": "^5.7.2",
22+
"@tanstack/solid-router-devtools": "^1.135.2",
23+
"postcss": "^8.5.1",
24+
"autoprefixer": "^10.4.20",
25+
"tailwindcss": "^3.4.17",
26+
"vite": "^7.1.7",
27+
"vite-plugin-dts": "^4.5.4"
28+
},
29+
"nx": {
30+
"targets": {
31+
"dev": {
32+
"dependsOn": [
33+
"^build"
34+
]
35+
}
36+
}
37+
}
38+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { render } from 'solid-js/web'
2+
import { RouterProvider } from '@tanstack/solid-router'
3+
import { router } from '@router-solid-mono-simple-lazy/router'
4+
import { RootComponent } from './rootComponent'
5+
import type {
6+
RouteById,
7+
RouterIds,
8+
} from '@router-solid-mono-simple-lazy/router'
9+
import type { LazyRoute } from '@tanstack/solid-router'
10+
import './style.css'
11+
12+
// Generic to enforce that the route returned matches the route path
13+
type LazyRouteFn<TRoutePath extends RouterIds> = () => Promise<
14+
LazyRoute<RouteById<(typeof router)['routeTree'], TRoutePath>>
15+
>
16+
17+
type RouterMap = {
18+
// __root__ is a special route that isn't lazy loaded, so we need to manually bind it
19+
// You could consider adding null to the returned type to have routes without rendering
20+
[K in Exclude<RouterIds, '__root__'>]: LazyRouteFn<K>
21+
}
22+
23+
const routerMap: RouterMap = {
24+
'/': () =>
25+
import('@router-solid-mono-simple-lazy/post-feature/post-list').then(
26+
(d) => d.PostRoute,
27+
),
28+
'/$postId': () =>
29+
import('@router-solid-mono-simple-lazy/post-feature/post-id-page').then(
30+
(d) => d.PostIdRoute,
31+
),
32+
}
33+
34+
// Given __root__ is a special route that isn't lazy loaded, we need to update it manually
35+
router.routesById['__root__'].update({
36+
component: RootComponent,
37+
})
38+
39+
Object.entries(routerMap).forEach(([path, component]) => {
40+
const foundRoute = router.routesById[path as RouterIds]
41+
// Bind the lazy route to the actual route
42+
foundRoute.lazy(component)
43+
})
44+
45+
// Render the app
46+
const rootElement = document.getElementById('app')!
47+
48+
if (!rootElement.innerHTML) {
49+
render(() => <RouterProvider router={router} />, rootElement)
50+
}

0 commit comments

Comments
 (0)