Skip to main content
0 reactions

How to Create a Catch-All Splat Route in TanStack Start

Card titled Catch-All Splat Routes, labelled TanStack Start routing, with the route /files/$ above two rows: /files/report.pdf to report.pdf and /files/photos/cat.jpg to photos/cat.jpg.
How to Create a Catch-All Splat Route in TanStack Start

A catch-all route matches every address that starts a certain way, however many parts follow, such as /files/report.pdf and /files/photos/cat.jpg. TanStack Start calls it a splat route: a route file named $.tsx, whose params hold the rest of the address under _splat.

This post builds a page that shows which file the address asked for.

Syntax

SyntaxTypeScript
// src/routes/<folder>/$.tsx
export const Route = createFileRoute('/<folder>/$')({
  component: <Component>,
})

function <Component>() {
  const { _splat } = Route.useParams()
  // _splat holds the rest of the address
}
  • $.tsx: a file named only $ makes a splat route. It catches everything after /<folder>/, slashes included.
  • _splat: the name the rest of the address gets in the route’s params.
  • <Component>: the React component that shows the page.

See “Splat / Catch-All Routes” in TanStack Router’s Routing Concepts (opens in a new tab).

The example is a small TanStack Start app with only a home page, set up as in How to Build a TanStack Start App from Scratch Without the CLI. Any TanStack Start app works the same way.

Step 1: Create the Route File

In src/routes, create a folder named files, and in it a file named $.tsx:

src/routes/files/$.tsxTypeScript
import { createFileRoute } from '@tanstack/react-router'

export const Route = createFileRoute('/files/$')({
  component: Files,
})

function Files() {
  const { _splat } = Route.useParams()
  return <p>You asked for: {_splat}</p>
}

const { _splat } = Route.useParams() reads the rest of the address, and the page shows it.

Step 2: Open a Few Addresses

Start the dev server with npm run dev, then open http://localhost:3000/files/photos/cat.jpg:

A browser page that shows the line You asked for: photos/cat.jpg for the address /files/photos/cat.jpg.
The splat route catches everything after /files/, slashes included.

The route caught both parts after /files/, with the slash between them. One part works too: http://localhost:3000/files/report.pdf shows this:

A browser page that shows the line You asked for: report.pdf for the address /files/report.pdf.
One level deep works too: _splat holds report.pdf.

Common Mistakes

MistakeWhat happensDo this instead
Using $name.tsx when the value can contain slashes/files/photos/cat.jpg doesn’t match, because a path param stops at the next /Use a splat route, $.tsx
Reading the value from params['*']It works in TanStack Router 1 only to stay compatible, and version 2 removes itRead _splat

FAQ

What does /files itself show? The splat route matches it too, with _splat as empty text, so the page shows You asked for: with nothing after it.

Are spaces in the address decoded? Yes. /files/my%20notes/day%201.txt gives my notes/day 1.txt.

Why $ and not *? TanStack Router’s docs explain that * doesn’t work well in file names and command-line tools, so it uses $ (Routing Concepts (opens in a new tab)).

Conclusion

A file named $.tsx catches every address under its folder, and _splat holds the rest of the address, slashes and all.

Additional Resources

TanStack, React and other names mentioned are trademarks of their respective owners. JD Bots isn’t affiliated with or endorsed by TanStack.

0 comments