Part 02 / Topic 5 of 9
Catch-all segments: [...terms]
Adding an ellipsis inside the brackets, [...terms], turns a dynamic segment into a catch-all: it matches one or more path segments, not just one. harness/app/store/search/[...terms]/page.tsx matches /store/search/silk (terms: ["silk"]) and equally /store/search/kurta/cotton (terms: ["kurta", "cotton"]) and any deeper path after /store/search/.
import Link from "next/link";
import { products } from "../../lib/products";
export function generateStaticParams() {
return [{ terms: ["silk"] }, { terms: ["pottery"] }];
}
export default async function SearchPage({ params }: PageProps<"/store/search/[...terms]">) {
const { terms } = await params;
const query = terms.join(" ").toLowerCase();
const matches = products.filter(
(product) =>
product.name.toLowerCase().includes(query) ||
product.category.toLowerCase().includes(query),
);
// ...
}
Real responses, both run against the harness:
$ curl -s http://localhost:PORT/store/search/silk | grep -o '<main[^>]*>.*</main>'
<main style="font-family:system-ui;padding:2rem;max-width:560px"><h1>Search: <!-- -->silk</h1><ul><li><a href="/store/products/p2">Banarasi Silk Saree</a></li></ul><p><a href="/store">Back to Apna Bazaar</a></p></main>
$ curl -s http://localhost:PORT/store/search/pottery/blue | grep -o '<main[^>]*>.*</main>'
<main style="font-family:system-ui;padding:2rem;max-width:560px"><h1>Search: <!-- -->pottery / blue</h1><p>No products matched “<!-- -->pottery blue<!-- -->”.</p><p><a href="/store">Back to Apna Bazaar</a></p></main>
The <!-- --> comments are the same React hydration markers Part 1 section 7 already explained — not a bug.
A required catch-all like [...terms] does not match the parent path with nothing after it. Proven for real:
$ curl -s -o /dev/null -w "%{http_code}\n" http://localhost:PORT/store/search
404
If you wanted /store/search itself to also match (showing, say, a "type something" page), the folder name needs one more pair of brackets: [[...terms]], an optional catch-all. The only difference between the two is exactly this — whether the bare parent path matches:
| Route | /shop |
/shop/a |
/shop/a/b |
|---|---|---|---|
app/shop/[...slug]/page.tsx (required) |
404 | matches, slug: ['a'] |
matches, slug: ['a', 'b'] |
app/shop/[[...slug]]/page.tsx (optional) |
matches, slug: undefined |
matches, slug: ['a'] |
matches, slug: ['a', 'b'] |
This table is not built into the harness — Apna Bazaar only needed the required form — but the rule is directly from the installed Next.js package's own dynamic-routes reference, not guessed.
Full file: examples/04-search-catch-all-segment-page.tsx.
Done here? The practice set for Part 2 — including the solutions — is on the part page.