https://github.com/seamapi/next-route-matcher
A route matching utility decides how to match a pathname based on filesystem route strings in NextJS style
https://github.com/seamapi/next-route-matcher
Last synced: 7 months ago
JSON representation
A route matching utility decides how to match a pathname based on filesystem route strings in NextJS style
- Host: GitHub
- URL: https://github.com/seamapi/next-route-matcher
- Owner: seamapi
- Created: 2022-07-20T00:56:58.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2025-02-10T20:52:20.000Z (about 1 year ago)
- Last Synced: 2025-10-01T09:53:53.821Z (7 months ago)
- Language: TypeScript
- Size: 205 KB
- Stars: 3
- Watchers: 4
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# NextJS Router Matcher
Want to use [nextjs-style routes](https://nextjs.org/docs/routing/dynamic-routes)
without the [next module](https://github.com/vercel/next.js/)? This module lets
you easily match routes. This code was adapted directly from the NextJS
implementation.
## Installation
```bash
yarn add next-route-matcher
```
## Usage
```ts
import nextRouteMatcher from "next-route-matcher"
const routeMatcher = nextRouteMatcher([
"/health",
"/api/nested",
"/api/items/[item_name]",
"/api/[someslug]/list",
"/api/[slug1]/deepnest/[slug2]/image.png",
"/api/[someslug]/greetings/[...anything]",
])
routeMatcher("/health")
// {
// matchedRoute: "/health",
// routeParams: {},
// }
routeMatcher("/api/nested")
// {
// matchedRoute: "/api/nested",
// routeParams: {},
// }
routeMatcher("/api/items/someitem")
// {
// matchedRoute: "/api/items/[item_name]",
// routeParams: { item_name: "someitem" },
// }
routeMatcher("/api/someslug/list")
// {
// matchedRoute: "/api/[someslug]/list",
// routeParams: { someslug: "someslug" },
// }
routeMatcher("/api/slug1/deepnest/slug2/image.png")
// {
// matchedRoute: "/api/[slug1]/deepnest/[slug2]/image.png",
// routeParams: {
// slug1: "slug1"
// slug2: "slug2",
// }
// },
routeMatcher("/api/someslug/greetings/something/somethingelse.txt")
// {
// matchedRoute: "/api/[someslug]/greetings/[...anything]"
// routeParams: {
// someslug: "someslug",
// anything: ["something", "somethingelse.txt"],
// },
// }
```