Route File Naming (v2)

You can opt in to the new route file naming convention with a future flag in Remix config.

  1. /** @type {import('@remix-run/dev').AppConfig} */
  2. module.exports = {
  3. future: {
  4. v2_routeConvention: true,
  5. },
  6. };

While you can configure routes in remix.config.js, most routes are created with this file system convention. Add a file, get a route.

Please note that you can use either .js, .jsx, .ts or .tsx file extensions. We’ll stick with .tsx in the examples to avoid duplication.

Root Route

  1. app/
  2. ├── routes/
  3. └── root.tsx

The file in app/root.tsx is your root layout, or “root route” (very sorry for those of you who pronounce those words the same way!). It works just like all other routes, so you can export a loader, action, etc.

The root route typically looks something like this. It serves as the root layout of the entire app, all other routes will render inside the <Outlet />.

  1. import {
  2. Links,
  3. Meta,
  4. Outlet,
  5. Scripts,
  6. ScrollRestoration,
  7. } from "@remix-run/react";
  8. export default function Root() {
  9. return (
  10. <html lang="en">
  11. <head>
  12. <Links />
  13. <Meta />
  14. </head>
  15. <body>
  16. <Outlet />
  17. <ScrollRestoration />
  18. <Scripts />
  19. </body>
  20. </html>
  21. );
  22. }

Basic Routes

Any JavaScript or TypeScript files in the app/routes/ directory will become routes in your application. The filename maps to the route’s URL pathname, except for _index.tsx which is the index route for the root route.

  1. app/
  2. ├── routes/
  3. ├── _index.tsx
  4. └── about.tsx
  5. └── root.tsx
URLMatched Routes
/_index.tsx
/aboutabout.tsx

Note that these routes will be rendered in the outlet of app/root.tsx because of nested routing.

Dot Delimiters

Adding a . to a route filename will create a / in the URL.

  1. app/
  2. ├── routes/
  3. ├── _index.tsx
  4. ├── about.tsx
  5. ├── concerts.trending.tsx
  6. ├── concerts.salt-lake-city.tsx
  7. └── concerts.san-diego.tsx
  8. └── root.tsx
URLMatched Route
/concerts/trendingconcerts.trending.tsx
/concerts/salt-lake-cityconcerts.salt-lake-city.tsx
/concerts/san-diegoconcerts.san-diego.tsx

The dot delimiter also creates nesting, see the nesting section for more information.

Dynamic Segments

Usually your URLs aren’t static but data-driven. Dynamic segments allow you to match segments of the URL and use that value in your code. You create them with the $ prefix.

  1. app/
  2. ├── routes/
  3. ├── _index.tsx
  4. ├── about.tsx
  5. ├── concerts.$city.tsx
  6. └── concerts.trending.tsx
  7. └── root.tsx
URLMatched Route
/concerts/trendingconcerts.trending.tsx
/concerts/salt-lake-cityconcerts.$city.tsx
/concerts/san-diegoconcerts.$city.tsx

Remix will parse the value from the URL and pass it to various APIs. We call these values “URL Parameters”. The most useful places to access the URL params are in loaders and actions.

  1. export function loader({ params }: LoaderArgs) {
  2. return fakeDb.getAllConcertsForCity(params.city);
  3. }

You’ll note the property name on the params object maps directly to the name of your file: $city.tsx becomes params.city.

Routes can have multiple dynamic segments, like concerts.$city.$date, both are accessed on the params object by name:

  1. export function loader({ params }: LoaderArgs) {
  2. return fake.db.getConcerts({
  3. date: params.date,
  4. city: params.city,
  5. });
  6. }

See the routing guide for more information.

Nested Routes

Nested Routing is the general idea of coupling segments of the URL to component hierarchy and data. You can read more about it in the Routing Guide.

You create nested routes with dot delimiters. If the filename before the . matches another route filename, it automatically becomes a child route to the matching parent. Consider these routes:

  1. app/
  2. ├── routes/
  3. ├── _index.tsx
  4. ├── about.tsx
  5. ├── concerts._index.tsx
  6. ├── concerts.$city.tsx
  7. ├── concerts.trending.tsx
  8. └── concerts.tsx
  9. └── root.tsx

All the routes that start with concerts. will be child routes of concerts.tsx and render inside the parent route’s outlet.

URLMatched RouteLayout
/_index.tsxroot.tsx
/aboutabout.tsxroot.tsx
/concertsconcerts._index.tsxconcerts.tsx
/concerts/trendingconcerts.trending.tsxconcerts.tsx
/concerts/salt-lake-cityconcerts.$city.tsxconcerts.tsx

Note you typically want to add an index route when you add nested routes so that something renders inside the parent’s outlet when users visit the parent URL directly.

Nested URLs without Layout Nesting

Sometimes you want the URL to be nested, but you don’t want the automatic layout nesting. You can opt out of nesting with a trailing underscore on the parent segment:

  1. app/
  2. ├── routes/
  3. ├── _index.tsx
  4. ├── about.tsx
  5. ├── concerts.$city.tsx
  6. ├── concerts.trending.tsx
  7. ├── concerts.tsx
  8. └── concerts_.mine.tsx
  9. └── root.tsx
URLMatched RouteLayout
/index.tsxroot.tsx
/concerts/mineconcerts.mine.tsxroot.tsx
/concerts/trendingconcerts.trending.tsxconcerts.tsx
/concerts/salt-lake-cityconcerts.$city.tsxconcerts.tsx

Note that /concerts/mine does not nest with concerts.tsx anymore, but root.tsx. The trailing_ underscore creates a path segment, but it does not create layout nesting.

Think of the trailing_ underscore as the long bit at the end of your parent’s signature, writing you out of the will, removing the segment that follows from the layout nesting.

Nested Layouts without Nested URLs

We call these Pathless Routes

Sometimes you want to share a layout with a group of routes without adding any path segments to the URL. A common example is a set of authentication routes that have a different header/footer than the public pages or the logged in app experience. You can do this with a _leading underscore.

  1. app/
  2. ├── routes/
  3. ├── _auth.login.tsx
  4. ├── _auth.register.tsx
  5. ├── _auth.tsx
  6. ├── _index.tsx
  7. ├── concerts.$city.tsx
  8. └── concerts.tsx
  9. └── root.tsx
URLMatched RouteLayout
/_index.tsxroot.tsx
/login_auth.login.tsx_auth.tsx
/register_auth.register.tsx_auth.tsx
/concerts/salt-lake-cityconcerts.$city.tsxconcerts.tsx

Think of the _leading underscore as a blanket you’re pulling over the filename, hiding the filename from the URL.

Optional Segments

Wrapping a route segment in parentheses will make the segment optional.

  1. app/
  2. ├── routes/
  3. ├── ($lang)._index.tsx
  4. ├── ($lang).$productId.tsx
  5. └── ($lang).categories.tsx
  6. └── root.tsx
URLMatched Route
/($lang)._index.tsx
/categories($lang).categories.tsx
/en/categories($lang).categories.tsx
/fr/categories($lang).categories.tsx
/american-flag-speedo($lang)._index.tsx
/en/american-flag-speedo($lang).$productId.tsx
/fr/american-flag-speedo($lang).$productId.tsx

You may wonder why /american-flag-speedo is matching the ($lang)._index.tsx route instead of ($lang).$productId.tsx. This is because when you have an optional dynamic param segment followed by another dynamic param, Remix cannot reliably determine if a single-segment URL such as /american-flag-speedo should match /:lang /:productId. Optional segments match eagerly and thus it will match /:lang. If you have this type of setup it’s recommended to look at params.lang in the ($lang)._index.tsx loader and redirect to /:lang/american-flag-speedo for the current/default language if params.lang is not a valid language code.

Splat Routes

While dynamic segments match a single path segment (the stuff between two / in a URL), a splat route will match the rest of a URL, including the slashes.

  1. app/
  2. ├── routes/
  3. ├── _index.tsx
  4. ├── $.tsx
  5. ├── about.tsx
  6. └── files.$.tsx
  7. └── root.tsx
URLMatched Route
/_index.tsx
/beef/and/cheese$.tsx
/filesfiles.$.tsx
/files/talks/remix-conf_old.pdffiles.$.tsx
/files/talks/remix-conf_final.pdffiles.$.tsx
/files/talks/remix-conf-FINAL-MAY_2022.pdffiles.$.tsx

Similar to dynamic route parameters, you can access the value of the matched path on the splat route’s params with the "*" key.

  1. export function loader({ params }) {
  2. const filePath = params["*"];
  3. return fake.getFileInfo(filePath);
  4. }

Escaping Special Characters

If you want one of the special characters Remix uses for these route conventions to actually be a part of the URL, you can escape the conventions with [] characters.

FilenameURL
routes/sitemap[.]xml.tsx/sitemap.xml
routes/[sitemap.xml].tsx/sitemap.xml
routes/weird-url.[_index].tsx/weird-url/_index
routes/dolla-bills-[$].tsx/dolla-bills-$
routes/[[so-weird]].tsx/[so-weird]

Folders for Organization

Routes can also be folders with a route.tsx file inside defining the route module. The rest of the files in the folder will not become routes. This allows you to organize your code closer to the routes that use them instead of repeating the feature names across other folders.

The files inside a folder have no meaning for the route paths, the route path is completely defined by the folder name

Consider these routes:

  1. routes/
  2. _landing._index.tsx
  3. _landing.about.tsx
  4. _landing.tsx
  5. app._index.tsx
  6. app.projects.tsx
  7. app.tsx
  8. app_.projects.$id.roadmap.tsx

Some, or all of them can be folders holding their own route module inside.

  1. routes/
  2. _landing._index/
  3. route.tsx
  4. scroll-experience.tsx
  5. _landing.about/
  6. employee-profile-card.tsx
  7. get-employee-data.server.tsx
  8. route.tsx
  9. team-photo.jpg
  10. _landing/
  11. header.tsx
  12. footer.tsx
  13. route.tsx
  14. app._index/
  15. route.tsx
  16. stats.tsx
  17. app.projects/
  18. get-projects.server.tsx
  19. project-card.tsx
  20. project-buttons.tsx
  21. route.tsx
  22. app/
  23. primary-nav.tsx
  24. route.tsx
  25. footer.tsx
  26. app_.projects.$id.roadmap/
  27. route.tsx
  28. chart.tsx
  29. update-timeline.server.tsx
  30. contact-us.tsx

Note that when you turn a route module into a folder, the route module becomes folder/route.tsx, all other modules in the folder will not become routes. For example:

  1. # these are the same route:
  2. routes/app.tsx
  3. routes/app/route.tsx
  4. # as are these
  5. routes/app._index.tsx
  6. routes/app._index/route.tsx

Scaling

Our general recommendation for scale is to make every route a folder and put the modules used exclusively by that route in the folder, then put the shared modules outside of routes folder elsewhere. This has a couple benefits:

  • Easy to identify shared modules, so tread lightly when changing them
  • Easy to organize and refactor the modules for a specific route without creating “file organization fatigue” and cluttering up other parts of the app

More Flexibility

While we like this file convention, we recognize that at a certain scale many organizations won’t like it. You can always define your routes programmatically in the remix config.

There’s also the Flat Routes third-party package with configurable options beyond the defaults in Remix.