Catch-All Routes
Learn how to use catch-all routes to match multiple URL segments in Bini.js. Perfect for documentation, nested categories, and flexible URL structures.
Overview
Catch-all routes allow you to match multiple URL segments in a single route. They are defined using the [...name] syntax, where the parameter becomes an array of the matched segments.
Match any number of URL segments
Access segments as an array
Perfect for documentation and nested categories
Handle language prefixes with variable paths
useParams() is auto-imported in all pages — no import statement needed to access catch-all parameters.What are Catch-All Routes?
Catch-all routes are a powerful feature that allows you to match any number of URL segments after a specific path. They are defined using the [...name] syntax in folder or file names.
Key Characteristics
- Matches multiple segments: Any number of URL segments after the parent path
- Array parameter: The parameter becomes an array of all matched segments
- Lower priority: Static and dynamic routes are matched first
- Optional version: Use
[[...name]]for optional catch-all
In this example, /docs/getting-started matches with slug = ['getting-started'], while /docs/guides/routing/basics matches with slug = ['guides', 'routing', 'basics'].
Basic Usage
Create a catch-all route by naming a folder or file with square brackets and three dots: [...name].
Catch-All Examples
The route will match any URL that starts with the parent path and has at least one segment. This is different from optional catch-all routes, which match even with zero segments.
[...slug] requires at least one segment. Use [[...slug]] for optional catch-all that matches the parent path too.Accessing Parameters
Use useParams() (auto-imported) to access the catch-all parameter as an array. The parameter name becomes a property on the params object.
Basic Access
| URL | slug value |
|---|---|
| /docs/getting-started | ['getting-started'] |
| /docs/api/reference | ['api', 'reference'] |
| /docs/guides/routing/basics | ['guides', 'routing', 'basics'] |
| /docs/advanced/custom/hooks | ['advanced', 'custom', 'hooks'] |
With Data Fetching
Nested Catch-All Routes
Catch-all routes can be combined with other dynamic and static segments to create complex routing patterns.
Combining with Dynamic Segments
Optional Catch-All Routes
Use [[...name]] to make the catch-all optional. The route will match both the parent path and any nested paths.
Optional Catch-All Example
| URL | slug value |
|---|---|
| /shop | undefined (or empty array) |
| /shop/clothing | ['clothing'] |
| /shop/clothing/shirts | ['clothing', 'shirts'] |
/docs) should show a landing page, and nested paths (/docs/getting-started) show specific content.File-Based Catch-All Routes
Catch-all routes can also be defined as flat files without folders. This reduces folder nesting for simpler use cases.
Flat File Examples
Route Priority
Catch-all routes have lower priority than static routes and dynamic single segments. The router resolves matches in this order:
- Static routesexact matches — e.g.,
/blog/featured - Dynamic single segments
[slug]— e.g.,/blog/:slug - Catch-all segments
[...slug]— e.g.,/blog/* - Optional catch-all segments
[[...slug]]— e.g.,/docs/*(optional)
Priority Example
Consider this folder structure with overlapping routes:
| URL | Matched Route | Priority |
|---|---|---|
| /blog/featured | featured/page.tsx | Static |
| /blog/hello-world | [slug]/page.tsx | Dynamic |
| /blog/2024/01/hello-world | [...slug]/page.tsx | Catch-all |
This priority system ensures predictable routing behavior and prevents conflicts between different route types.
Use Cases
Catch-all routes are ideal for:
Multi-level documentation with variable depth
/docs/guides/routing/basicsNested category structures
/products/electronics/phones/iphoneDate-based archives
/blog/2024/01/hello-worldLanguage prefixes with variable paths
/en/docs/getting-startedAdditional Use Cases
- CMS Content: Content pages with flexible URL structures
- API Versioning: API routes with version segments like
/api/v1/users/123 - File Browser: Directory browsing with arbitrary depth
- Wiki Pages: Multi-level wiki documentation
- Path-Based Navigation: Any URL structure where depth varies
Complete Example
Here is a comprehensive example showing all catch-all route patterns in a real-world application:
Route Mapping
| Pattern | Example URL | Type |
|---|---|---|
| /blog/featured | /blog/featured | Static |
| /blog/:slug | /blog/hello-world | Dynamic Single |
| /blog/* | /blog/2024/01/hello-world | Catch-all |
| /docs/* (optional) | /docs | Optional Catch-all |
| /docs/* (optional) | /docs/getting-started | Optional Catch-all |
| /products/:category/* | /products/electronics/phones/iphone | Nested Catch-all |
| /shop/* (optional) | /shop/clothing/shirts | Optional Catch-all |
| /api/v1/* | /api/v1/users/123 | Flat File Catch-all |