Site icon JD Bots

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

Syntax

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

function <Component>() {
  const { _splat } = Route.useParams()
  // _splat holds the rest of the address
}

See “Splat / Catch-All Routes” in TanStack Router’s Routing Concepts.

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/$.tsx

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:

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:

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).

Conclusion

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

Additional Resources

Related

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

Exit mobile version