-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from ckastbjerg/issue-12-support-catch-all-routes
Support catch all routes
- Loading branch information
Showing
23 changed files
with
174 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { useRouter } from "hooks"; | ||
|
||
const CatchAll = () => { | ||
const router = useRouter(); | ||
const slug = router.query.slug as string[]; | ||
return <div>Slugs: {slug.join(",")}</div>; | ||
}; | ||
|
||
export default CatchAll; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,27 @@ | ||
import { Link } from "components"; | ||
import { useRouter } from "hooks"; | ||
|
||
const Home = () => { | ||
const { push } = useRouter(); | ||
return <button onClick={() => push("/users")}>Show users</button>; | ||
return ( | ||
<> | ||
<button onClick={() => push("/users")}>Show users</button> | ||
<Link to="/optional-catch-all">Optional catch all (no path)</Link> | ||
<Link to={{ route: "/optional-catch-all", path: "/a/b/c" }}> | ||
Optional catch all | ||
</Link> | ||
<Link to={{ route: "/catch-all", path: "/a/b/c" }}>Catch all</Link> | ||
<Link | ||
to={{ | ||
route: "/nested-catch-all/[dynamic]/slugs", | ||
params: { dynamic: 1 }, | ||
path: "/a/b/c", | ||
}} | ||
> | ||
Nested catch all (with params) | ||
</Link> | ||
</> | ||
); | ||
}; | ||
|
||
export default Home; |
14 changes: 14 additions & 0 deletions
14
example/src/pages/nested-catch-all/[dynamic]/slugs/[...slug].tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { useRouter } from "hooks"; | ||
|
||
const CatchAll = () => { | ||
const router = useRouter(); | ||
const { dynamic, slug } = router.query; | ||
return ( | ||
<div> | ||
<div>dynamic: {dynamic}</div> | ||
<div>Slugs: {(slug as string[]).join(",")}</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default CatchAll; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { useRouter } from "hooks"; | ||
|
||
const OptionalCatchAll = () => { | ||
const router = useRouter(); | ||
const slug = router.query.slug; | ||
return <div>{slug ? (slug as string[]).join(",") : "no slug"}</div>; | ||
}; | ||
|
||
export default OptionalCatchAll; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
const isCatchAllParam = (param: string) => param.match(/\.\.\./); | ||
const getNextRouteUrlParams = (href: string) => { | ||
const params = href.match(/\[([^\]]+)\]/g); | ||
return params?.map((param) => param.replace("[", "").replace("]", "")); | ||
const paramStrings = href.match(/\[([^\]]+)\]/g); | ||
const params = paramStrings | ||
?.filter((param) => !isCatchAllParam(param)) | ||
.map((param) => param.replace("[", "").replace("]", "")); | ||
|
||
return !!params?.length ? params : undefined; | ||
}; | ||
|
||
export default getNextRouteUrlParams; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import getNextPageRoute from "./getNextPageRoute"; | ||
import getNextRouteUrlParams from "./getNextRouteUrlParams"; | ||
import { Page } from "./types"; | ||
import { getIsCatchAllRoute, getIsOptionalCatchAllRoute } from "./utils"; | ||
|
||
const getRoutes = (fileNames: string[]): Page[] => { | ||
return fileNames.map((fileName) => { | ||
return { | ||
route: getNextPageRoute(fileName), | ||
params: getNextRouteUrlParams(fileName), | ||
isCatchAllRoute: getIsCatchAllRoute(fileName), | ||
isOptionalCatchAllRoute: getIsOptionalCatchAllRoute(fileName), | ||
}; | ||
}); | ||
}; | ||
|
||
export default getRoutes; |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,8 @@ | ||
export type Page = { | ||
route: string; | ||
params?: string[]; | ||
isCatchAllRoute: boolean; | ||
isOptionalCatchAllRoute: boolean; | ||
}; | ||
|
||
export type ApiRoute = { | ||
route: string; | ||
params?: string[]; | ||
}; | ||
export type ApiRoute = Page; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const getIsCatchAllRoute = (route: string) => !!route.match(/\[\.\.\./); | ||
export const getIsOptionalCatchAllRoute = (route: string) => | ||
!!route.match(/\[\[\.\.\./); |
Oops, something went wrong.