Most of the TanStack Router documentation is written for file-based routing and is intended to help you understand in more detail how to configure file-based routing and the technical details behind how it works. While file-based routing is the preferred and recommended way to configure TanStack Router, you can also use code-based routing if you prefer.
File-based routing is a way to configure your routes using the filesystem. Instead of defining your route structure via code, you can define your routes using a series of files and directories that represent the route hierarchy of your application. This brings a number of benefits:
While directories have long been used to represent route hierarchy, file-based routing introduces an additional concept of using the . character in the file-name to denote a route nesting. This allows you to avoid creating directories for few deeply nested routes and continue to use directories for wider route hierarchies. Let's take a look at some examples!
Directories can be used to denote route hierarchy, which can be useful for organizing multiple routes into logical groups and also cutting down on the filename length for large groups of deeply nested routes.
See the example below:
Filename | Route Path | Component Output |
---|---|---|
ʦ __root.tsx | <Root> | |
ʦ index.tsx | / (exact) | <Root><RootIndex> |
ʦ about.tsx | /about | <Root><About> |
ʦ posts.tsx | /posts | <Root><Posts> |
📂 posts | ||
┄ ʦ index.tsx | /posts (exact) | <Root><Posts><PostsIndex> |
┄ ʦ $postId.tsx | /posts/$postId | <Root><Posts><Post> |
📂 posts_ | ||
┄ 📂 $postId | ||
┄ ┄ ʦ edit.tsx | /posts/$postId/edit | <Root><EditPost> |
ʦ settings.tsx | /settings | <Root><Settings> |
📂 settings | <Root><Settings> | |
┄ ʦ profile.tsx | /settings/profile | <Root><Settings><Profile> |
┄ ʦ notifications.tsx | /settings/notifications | <Root><Settings><Notifications> |
ʦ _layout.tsx | <Root><Layout> | |
📂 _layout | ||
┄ ʦ layout-a.tsx | /layout-a | <Root><Layout><LayoutA> |
┄ ʦ layout-b.tsx | /layout-b | <Root><Layout><LayoutB> |
📂 files | ||
┄ ʦ $.tsx | /files/$ | <Root><Files> |
Flat routing gives you the ability to use .s to denote route nesting levels.
This can be useful when you have a large number of uniquely deeply nested routes and want to avoid creating directories for each one:
See the example below:
Filename | Route Path | Component Output |
---|---|---|
ʦ __root.tsx | <Root> | |
ʦ index.tsx | / (exact) | <Root><RootIndex> |
ʦ about.tsx | /about | <Root><About> |
ʦ posts.tsx | /posts | <Root><Posts> |
ʦ posts.index.tsx | /posts (exact) | <Root><Posts><PostsIndex> |
ʦ posts.$postId.tsx | /posts/$postId | <Root><Posts><Post> |
ʦ posts_.$postId.edit.tsx | /posts/$postId/edit | <Root><EditPost> |
ʦ settings.tsx | /settings | <Root><Settings> |
ʦ settings.profile.tsx | /settings/profile | <Root><Settings><Profile> |
ʦ settings.notifications.tsx | /settings/notifications | <Root><Settings><Notifications> |
ʦ _layout.tsx | <Root><Layout> | |
ʦ _layout.layout-a.tsx | /layout-a | <Root><Layout><LayoutA> |
ʦ _layout.layout-b.tsx | /layout-b | <Root><Layout><LayoutB> |
ʦ files.$.tsx | /files/$ | <Root><Files> |
It's extremely likely that a 100% directory or flat route structure won't be the best fit for your project, which is why TanStack Router allows you to mix both flat and directory routes together to create a route tree that uses the best of both worlds where it makes sense:
See the example below:
Filename | Route Path | Component Output |
---|---|---|
ʦ __root.tsx | <Root> | |
ʦ index.tsx | / (exact) | <Root><RootIndex> |
ʦ about.tsx | /about | <Root><About> |
ʦ posts.tsx | /posts | <Root><Posts> |
📂 posts | ||
┄ ʦ index.tsx | /posts (exact) | <Root><Posts><PostsIndex> |
┄ ʦ $postId.tsx | /posts/$postId | <Root><Posts><Post> |
┄ ʦ $postId.edit.tsx | /posts/$postId/edit | <Root><Posts><Post><EditPost> |
ʦ settings.tsx | /settings | <Root><Settings> |
ʦ settings.profile.tsx | /settings/profile | <Root><Settings><Profile> |
ʦ settings.notifications.tsx | /settings/notifications | <Root><Settings><Notifications> |
Both flat and directory routes can be mixed together to create a route tree that uses the best of both worlds where it makes sense.
Tip
If you find that the default file-based routing structure doesn't fit your needs, you can always use Virtual File Routes to control the source of your routes whilst still getting the awesome performance benefits of file-based routing.
To get started with file-based routing, you'll need to configure your project's bundler to use the TanStack Router Plugin or the TanStack Router CLI.
To enable file-based routing, you'll need to be using React with a supported bundler. See if your bundler is listed in the configuration guides below.
When using TanStack Router's file-based routing through one of the supported bundlers, our plugin will automatically generate your route configuration through your bundler's dev and build processes. It is the easiest way to use TanStack Router's route generation features.
If your bundler is not yet supported, you can reach out to us on Discord or GitHub to let us know. Till then, fear not! You can still use the @tanstack/router-cli package to generate your route tree file.
Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.