Skip to content
On this page

Code Splitting

The file where you create your router should typically be kept in the main bundle of your application. Since a Type Route route is just a piece of application state the strategies you'll need to ensure code splitting works properly while rendering your application will not change. In React applications React provides the lazy utility for dynamically imported components. Which components specifically should be used in conjunction with the lazy utility will vary from application to application. In many cases it will be appropriate to wrap the top level page components that correspond to each route with this helper.


import { createRouter, defineRoute, param } from "type-route";

export const { useRoute, routes } = createRouter({
  home: defineRoute("/"),
  user: defineRoute(
      userId: param.path.string,
    (p) => `/user/${p.userId}`


import React from "react";

export default function HomePage() {
  return <div>Home</div>;


import React from "react";
import { Route } from "type-route";
import { routes } from "./router";

type Props = {
  route: Route<typeof routes.user>;

export default function UserPage(props: Props) {
  const { route } = props;

  return <div>User {route.params.userId}</div>;


import React, { useEffect } from "react";

const HomePage = React.lazy(() => import("./HomePage"));
const UserPage = React.lazy(() => import("./UserPage"));

function App() {
  const route = useRoute();

  return (
        <a {...routes.home().link}>Home</a>
        <a {...routes.user({ userId: "abc" }).link}>User "abc"</a>
      <React.Suspense fallback={<div>Loading</div>}>
        { === "home" && <HomePage />}
        { === "user" && <UserPage route={route} />}
        { === false && <div>Not Found</div>}

Type Route is a Zilch project