Skip to content

Commit

Permalink
Add support for parallel and intercept routes
Browse files Browse the repository at this point in the history
  • Loading branch information
tatethurston committed Sep 20, 2024
1 parent 5484840 commit 8e4c9a3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import { RouteLiteral } from "nextjs-routes";
```

- Refine types for `usePathname` and `useParams` from `"next/navigation"`

## 2.2.1

- Fix route generation on Windows. See [#187](https://github.com/tatethurston/nextjs-routes/issues/187). Thanks @AkanoCA!
Expand Down
4 changes: 2 additions & 2 deletions packages/nextjs-routes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"author": "Tate <tatethurston@gmail.com>",
"repository": {
"type": "git",
"url": "https://github.com/tatethurston/nextjs-routes"
"url": "git+https://github.com/tatethurston/nextjs-routes.git"
},
"type": "module",
"exports": {
Expand All @@ -21,7 +21,7 @@
"./package.json": "./package.json"
},
"bin": {
"nextjs-routes": "./dist/cli.js"
"nextjs-routes": "dist/cli.js"
},
"scripts": {
"build": "tsc && webpack && chmod +x dist/cli.js",
Expand Down
26 changes: 21 additions & 5 deletions packages/nextjs-routes/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,13 +429,24 @@ function commonProcessing(paths: string[], opts: Opts): string[] {
);
}

const APP_DIRECTORY_ROUTABLE = ["page", "route"];
const APP_DIRECTORY_ROUTABLE_DIRECTORIES = ["page", "route"];

const APP_INTERCEPTING_ROUTE = ["(.)", "(..)", "(..)(..)", "(...)"];

function appDirectoryRoutable(file: string): boolean {
const name = parse(file).name;
return (
// only consider page and route
APP_DIRECTORY_ROUTABLE_DIRECTORIES.includes(name) &&
// remove any filepaths that contain intercepts
!APP_INTERCEPTING_ROUTE.some((intercept) => file.includes(intercept))
);
}

export function getAppRoutes(files: string[], opts: Opts): string[] {
return (
commonProcessing(files, opts)
// app pages must be named 'page'
.filter((file) => APP_DIRECTORY_ROUTABLE.includes(parse(file).name))
.filter(appDirectoryRoutable)
.map((file) =>
// transform filepath to url path
file
Expand All @@ -444,8 +455,13 @@ export function getAppRoutes(files: string[], opts: Opts): string[] {
.filter(
(segment) => !(segment.startsWith("(") && segment.endsWith(")")),
)
// remove page
.filter((file) => !APP_DIRECTORY_ROUTABLE.includes(parse(file).name))
// remove page + route from path
.filter(
(segment) =>
!APP_DIRECTORY_ROUTABLE_DIRECTORIES.includes(parse(segment).name),
)
// remove slots
.filter((segment) => !segment.startsWith("@"))
.join("/"),
)
// handle index page
Expand Down

0 comments on commit 8e4c9a3

Please sign in to comment.