Catch-all routes let you handle many URL paths with one page. It helps when you want to show different content based on parts of the URL.
Catch-all routes with [...param] in NextJS
pages/[...param].js
// or in app router
app/[...param]/page.jsThe [...param] syntax creates a catch-all route that matches any path under that folder.
The parameter param will be an array of strings representing each part of the URL.
[...param].export default function Page({ params }) { return <div>{JSON.stringify(params.param)}</div>; }
/docs/nextjs/routing, the param array contains each path segment after /docs.URL: /docs/nextjs/routing params.param = ['nextjs', 'routing']
export default function Page({ params }) { const path = params.param?.join(' / ') ?? 'home'; return <h1>Path: {path}</h1>; }
This Next.js page uses a catch-all route to show each part of the URL as a list. If no extra path is given, it shows 'Home'.
import React from 'react'; export default function CatchAllPage({ params }) { // params.param is an array of URL parts const parts = params.param ?? []; return ( <main> <h1>Catch-All Route</h1> <p>You visited these parts of the URL:</p> <ul> {parts.length > 0 ? ( parts.map((part, i) => <li key={i}>{part}</li>) ) : ( <li>Home (no extra path)</li> )} </ul> </main> ); }
If you want to match zero or more segments, use [[...param]]. For one or more segments only, use [...param].
The params object is passed automatically to your page component in Next.js App Router.
Use this feature to keep your app structure simple and flexible for many URL patterns.
Catch-all routes use [...param] to match many URL paths with one page.
The captured parameter is an array of URL parts you can use to show dynamic content.
This helps build flexible and scalable routing in Next.js apps.
Practice
[...param] do in Next.js?Solution
Step 1: Understand catch-all route behavior
A catch-all route like[...param]matches any URL path that starts with the route prefix and captures all remaining parts.Step 2: Recognize parameter type
The captured parameter is an array of strings representing each part of the URL after the prefix.Final Answer:
It matches any URL path that starts with the route prefix and captures the rest as an array. -> Option AQuick Check:
Catch-all routes capture URL parts as arrays [OK]
- Thinking catch-all captures a single string instead of an array
- Believing catch-all matches only exact paths
- Confusing catch-all with optional catch-all routes
/blog?Solution
Step 1: Recall catch-all route syntax
Catch-all routes use square brackets with three dots:[...param]to capture multiple segments.Step 2: Match filename to route
To capture all paths under/blog, the file must be namedblog/[...param].js.Final Answer:
blog/[...param].js -> Option CQuick Check:
Catch-all filename uses [...param] syntax [OK]
- Using single brackets without dots for catch-all
- Confusing optional catch-all [[...param]] with catch-all
- Omitting brackets or dots in filename
pages/shop/[...items].js and the URL /shop/electronics/phones/smartphones, what will be the value of items inside getServerSideProps?Solution
Step 1: Identify catch-all parameter capture
The catch-all route[...items]captures all URL parts after/shopas an array.Step 2: Extract URL parts after /shop
From the URL/shop/electronics/phones/smartphones, the parts after/shopareelectronics,phones, andsmartphones.Final Answer:
['electronics', 'phones', 'smartphones'] -> Option DQuick Check:
Catch-all params = array of URL parts after prefix [OK]
- Including the route prefix in the array
- Expecting a string instead of an array
- Missing some URL parts in the array
pages/docs/[...slug].js. The developer tries to access slug.join('-') but gets an error. What is the likely cause?Solution
Step 1: Understand catch-all behavior with no extra path
If the URL is exactly/docswith no extra parts, the catch-allslugparameter is undefined.Step 2: Reason about calling
Callingjoinon undefinedjoinon undefined causes an error becausejoinis an array method.Final Answer:
The slug parameter is undefined because the URL is /docs with no extra parts. -> Option AQuick Check:
Catch-all param undefined if no extra URL parts [OK]
- Assuming catch-all param is always an array
- Confusing catch-all with optional catch-all routes
- Not checking for undefined before using array methods
/profile and any deeper paths like /profile/settings/privacy. Which filename should you use to make the parameter optional and avoid errors when no extra path is given?Solution
Step 1: Understand optional catch-all syntax
To match both/profileand deeper paths, use optional catch-all syntax with double brackets:[[...params]].Step 2: Recognize difference from regular catch-all
Regular catch-all[...params]requires at least one path segment; optional catch-all[[...params]]allows zero or more segments.Final Answer:
profile/[[...params]].js -> Option BQuick Check:
Optional catch-all uses double brackets [[...param]] [OK]
- Using single brackets for optional catch-all
- Expecting regular catch-all to match empty paths
- Naming files without brackets
Ready to prove it?
Pass the test and this pattern moves unknown
