Router Docs
Router Examples
- Quickstart (file-based)
- Quickstart (code-based)
- Basic (file-based)
- Basic + Solid Query (file-based)
- Basic + Solid Query (code-based)
- Basic + SSR (file-based)
- Basic + SSR Streaming (file-based)
- Kitchen Sink (code-based)
- Kitchen Sink (file-based)
- Kitchen Sink + Solid Query (file-based)
- Kitchen Sink + Solid Query (code-based)
- Location Masking
- Authenticated Routes
- Scroll Restoration
- Deferred Data
- Navigation Blocking
- View Transitions
- Framer Motion
- With tRPC
- Monorepo basic
- Monorepo basic (with lazy loading)
- Monorepo with Solid Query
Solid Example: Router Monorepo Simple Lazy
import { render } from 'solid-js/web'
import { RouterProvider } from '@tanstack/solid-router'
import { router } from '@router-solid-mono-simple-lazy/router'
import { RootComponent } from './rootComponent'
import type {
RouteById,
RouterIds,
} from '@router-solid-mono-simple-lazy/router'
import type { LazyRoute } from '@tanstack/solid-router'
import './style.css'
// Generic to enforce that the route returned matches the route path
type LazyRouteFn<TRoutePath extends RouterIds> = () => Promise<
LazyRoute<RouteById<(typeof router)['routeTree'], TRoutePath>>
>
type RouterMap = {
// __root__ is a special route that isn't lazy loaded, so we need to manually bind it
// You could consider adding null to the returned type to have routes without rendering
[K in Exclude<RouterIds, '__root__'>]: LazyRouteFn<K>
}
const routerMap: RouterMap = {
'/': () =>
import('@router-solid-mono-simple-lazy/post-feature/post-list').then(
(d) => d.PostRoute,
),
'/$postId': () =>
import('@router-solid-mono-simple-lazy/post-feature/post-id-page').then(
(d) => d.PostIdRoute,
),
}
// Given __root__ is a special route that isn't lazy loaded, we need to update it manually
router.routesById['__root__'].update({
component: RootComponent,
})
Object.entries(routerMap).forEach(([path, component]) => {
const foundRoute = router.routesById[path as RouterIds]
// Bind the lazy route to the actual route
foundRoute.lazy(component)
})
// Render the app
const rootElement = document.getElementById('app')!
if (!rootElement.innerHTML) {
render(() => <RouterProvider router={router} />, rootElement)
}