# Optional Catch-all ([[...slug]])

> Catch-all routes that don't crash on the root path.

Subject: Next.js | Lesson 16 of 65 | 4 min read
Interactive lesson: https://codewithanimation.in/nextjs/optional-catch-all
Previous: [Catch-all Routes ([...slug])](https://codewithanimation.in/nextjs/catch-all-routes.md) | Next: [Route Groups (...)](https://codewithanimation.in/nextjs/route-groups.md)

## The double brackets

An Optional Catch-all route uses double square brackets: `[[...slug]]`.

It behaves exactly like a standard Catch-all route (`[...slug]`), capturing any number of nested URL segments into an array. However, it has one major superpower: it makes the segments *optional*.

## Consolidating routes

With a standard `[...slug]`, if a user navigates to the root folder (e.g., `/docs`), Next.js throws a 404 error because it requires at least one parameter.

To fix that, developers used to create two files: `app/docs/page.tsx` (for the root) and `app/docs/[...slug]/page.tsx` (for everything else). Optional Catch-all routes let you handle both scenarios in a single file.

## Checking for undefined

When a user visits the root path of an Optional Catch-all route, the `params` object won't contain the dynamic key.

For example, in Next.js 15, if you have `app/docs/[[...slug]]/page.tsx` and the user visits `/docs`, after you `await params`, the `slug` variable will evaluate to `undefined`.

## Common mistake: The undefined crash

Because `slug` can be `undefined`, you must defensively check it before attempting to use Array methods on it.

If you blindly write `const path = (await params).slug.join('/');`, your app will crash with a `Cannot read properties of undefined (reading 'join')` error the moment a user hits the root page.

## Tip: Default fallback

A clean way to handle this in JavaScript is to provide a fallback array when destructuring.

Using `const { slug = [] } = await params;` guarantees that `slug` is always an array, even on the root path, saving you from writing endless `if (slug)` checks.

## Try it: The Root Bypass

Review the diagram contrasting a standard Catch-all with an Optional Catch-all. Notice how the double brackets gracefully handle the root path by injecting undefined instead of throwing a 404.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/optional-catch-all

## Check yourself

### Question 1

If your route is app/docs/[[...slug]]/page.tsx, what is the value of 'slug' when a user visits exactly /docs?

- A. An empty array: []
- B. undefined
- C. A 404 error is thrown
- D. null

Correct answer: B

Because the parameter is optional and no segments were provided in the URL, the `slug` key will be `undefined` in the awaited params object.

- Why not A: Next.js omits the key entirely, it does not inject an empty array.
- Why not C: That happens with a single-bracket Catch-all route ([...slug]).
- Why not D: It evaluates to undefined, not null.

### Question 2

What is the safest way to destructure the params object in an Optional Catch-all route to avoid crashes?

- A. const { slug } = await params;
- B. const slug = (await params).slug || '';
- C. const { slug = [] } = await params;
- D. It is impossible; you must use an if-statement

Correct answer: C

Providing a default empty array (`= []`) ensures that even if `slug` is `undefined` (at the root path), your code will always have a valid array to work with.

- Why not A: slug could be undefined, which crashes if you call .map() or .join() on it.
- Why not B: This makes it a string, which breaks array logic you might rely on for nested routes.
- Why not D: Default parameter destructuring works perfectly here.

## Remember this

- Optional catch-all routes use double brackets: `[[...slug]]`.
- They match deeply nested segments AND the root folder itself.
- If matched at the root, the parameter value will be `undefined`.
- Always provide a fallback (like `slug = []`) to prevent runtime crashes.
