Edit this page.md

Router

The @lazarv/react-server runtime includes a fully typed router with compile-time type safety for every route, link, parameter, and search param. Define routes with createRoute and createRouter for typed Link components, typed hooks, schema validation, and IDE autocompletion — on both server and client. Routes can be server-rendered, client-only, or shared between environments.

The runtime also includes a file-system based router which builds on top of the typed routing primitives. You just omit the entrypoint when using the react-server CLI and the router will be used automatically. If you prefer a third-party solution, you can use React Router or TanStack Router instead. Check out the examples collection for how to use these third-party routers.

You can learn about how to define your routes and how to configure the router to create layouts, pages and different types of routes like nested routes, dynamic routes and catch-all routes.

You can also create outlets to create reusable parts of your application that can be used in different layouts, while error handling and loading components are also supported in the router.

This router also supports creating middlewares and API route handlers to create middlewares and API endpoints using the file-system and all the rules you already learned when creating pages and layouts.

This documentation is also the best example that you can use Markdown or MDX files as pages in your application. This is a very simple way to create static pages in your application.

Next you can learn how to generate a static website using the combination of the router and the runtime. You can define all the routes you want to generate and the router will generate the static files for you. Not only for pages, but any static file, like a sitemap.xml for your website.

The router includes built-in scroll restoration with zero-flash page reload, nested container support, and per-route customization — enable it with a single config option or the <ScrollRestoration> component.

Finally learn how to configure the router to fit your needs. You can define the root path for your routes, the public path for your static files and you can include specific files in the routing or exclude files and directories from the routing using glob patterns.

See how @lazarv/react-server stacks up against Next.js, TanStack Router, and React Router in the comparison table.