0
Fork 0
mirror of https://github.com/TanStack/router.git synced 2026-11-16 19:38:07 +00:00
router/docs/guide/code-splitting.md
Tanner Linsley 8fab8b4175 feat: loader + ephemeral loaderData
Breaking Changes:

- `load` has been renamed back (I know...) to `loader`. Don't kill me. This again stays true to the nature of its functionality.
- `loader` can once again return data, but it is stored ephemerally on the route match and re-loaded on every navigation.
- If you choose to store data in `loaderData` from the `loader` function, you'll likely want to employ some level of caching to ensure you're only fetching data when necessary and resolving as early as possible if you have any meaningful cached data to display to the user. TanStack Query is a great tool to do this.
2023-11-29 01:16:52 -07:00

3 KiB

title
Code Splitting

Using lazyRouteComponent

Synchronous components are usually defined as functions and passed to the route.component option:

function MyComponent() {
  return <div>My Component</div>
}

const route = new Route({
  component: MyComponent,
})

But with TanStack Router, a route component can have a static preload method attached to it which will get called and awaited when the route is preloaded and/or loaded.

If we were to set this up manually (don't do this), it would look something like this:

const MyComponent = React.lazy(() => import('./MyComponent'))

MyComponent.preload = async () => {
  await import('./MyComponent')
}

const route = new Route({
  component: MyComponent,
})

This is a bit noisy and repetitive, so the TanStack Router exports a lazyRouteComponent wrapper that you can use to simplify this process:

import { lazyRouteComponent } from '@tanstack/router-react'

const route = new Route({
  component: lazyRouteComponent(() => import('./MyComponent')),
})

The lazyRouteComponent wrapper not only implements React.lazy() under the hood, but automatically sets up the component with a preload method and promise management for you.

Handling Named exports with lazyRouteComponent

The lazyRouteComponent wrapper also allows you to easily load components that are named exports from a module. To use this functionality, provide the name of the exported component as a second argument to the function:

const route = new Route({
  component: lazyRouteComponent(() => import('./MyComponent'), 'NamedExport'),
})

lazyRouteComponent is also type safe, so as to ensure that you are only able to provide valid named exports from the module, which helps to prevent runtime errors.

Data Loader Splitting

Regardless of which data loading library you decide to go with, you may end up with a lot of data loaders that could potentially contribute to a large bundle size. If this is the case, you can code split your data loading logic using the Route's loader option. While this process makes it difficult to maintain type-safety with the parameters passed to your loader, you can always use the generic LoaderContext type to get most of the way there:

import { LoaderContext } from '@tanstack/react-router'

const route = new Route({
  path: '/my-route',
  component: MyComponent,
  loader: (...args) => import('./loader').then((d) => d.loader(...args)),
})

// In another file...
export const loader = async (context: LoaderContext) => {
  /// ...
}

Again, this process can feel heavy-handed, so TanStack Router exports another utility called lazyFn which is very similar to lazyRouteComponent that can help simplify this process:

import { lazyFn } from '@tanstack/react-router'

const route = new Route({
  path: '/my-route',
  component: MyComponent,
  loader: lazyFn(() => import('./loader'), 'loader'),
})

// In another file...
export const loader = async (context: LoaderContext) => {
  /// ...
}