Skip to content

Commit

Permalink
🐛 Make unique route id check optional
Browse files Browse the repository at this point in the history
Fixes #29
  • Loading branch information
kiliman committed Dec 20, 2022
1 parent d24c040 commit b23d269
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## v0.5.3

- 🐛 Make unique route id check optional [#29](https://github.com/kiliman/remix-flat-routes/issues/29)

## v0.5.2

- 🐛 Fix flat-files folder support on Windows [#27](https://github.com/kiliman/remix-flat-routes/issues/27)
Expand Down
37 changes: 24 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export type FlatRoutesOptions = {
paramPrefixChar?: string
ignoredRouteFiles?: string[]
routeRegex?: RegExp
enableUniqueIdCheck?: boolean
}

export type DefineRoutesFunction = (
Expand All @@ -63,6 +64,7 @@ const defaultOptions: FlatRoutesOptions = {
paramPrefixChar: '$',
routeRegex:
/(([+]\/[a-zA-Z0-9_$.\[\]-]+)|\/((index|route|layout|page)|(_[a-zA-Z0-9_$.-]+)|([a-zA-Z0-9_$.\[\]-]+\.route)))\.(ts|tsx|js|jsx|md|mdx)$/,
enableUniqueIdCheck: true,
}
const defaultDefineRoutes = undefined

Expand Down Expand Up @@ -194,22 +196,31 @@ function _flatRoutes(
}
let index = childRoute.index
let fullPath = childRoute.path
let uniqueRouteId = (fullPath || '') + (index ? '?index' : '')

if (uniqueRouteId) {
if (uniqueRoutes.has(uniqueRouteId)) {
throw new Error(
`Path ${JSON.stringify(fullPath)} defined by route ${JSON.stringify(
childRoute.id,
)} conflicts with route ${JSON.stringify(
uniqueRoutes.get(uniqueRouteId),
)}`,
)
} else {
uniqueRoutes.set(uniqueRouteId, childRoute.id)
// add option to check for unique route ids
// this is copied from remix default convention
// but it is currently breaking some flat routes
// so until we can figure out a better way to do this
// make it optional to unblock users
if (options?.enableUniqueIdCheck) {
let uniqueRouteId = (fullPath || '') + (index ? '?index' : '')

if (uniqueRouteId) {
if (uniqueRoutes.has(uniqueRouteId)) {
throw new Error(
`Path ${JSON.stringify(
fullPath,
)} defined by route ${JSON.stringify(
childRoute.id,
)} conflicts with route ${JSON.stringify(
uniqueRoutes.get(uniqueRouteId),
)}`,
)
} else {
uniqueRoutes.set(uniqueRouteId, childRoute.id)
}
}
}

if (index) {
let invalidChildRoutes = Object.values(routeMap).filter(
routeInfo => routeInfo.parentId === childRoute.id,
Expand Down

0 comments on commit b23d269

Please sign in to comment.