From b23d269662b6cea31beaae3c993ffbe117b6943c Mon Sep 17 00:00:00 2001 From: kiliman Date: Tue, 20 Dec 2022 18:06:43 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Make=20unique=20route=20id=20che?= =?UTF-8?q?ck=20optional?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #29 --- CHANGELOG.md | 4 ++++ src/index.ts | 37 ++++++++++++++++++++++++------------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ddc6a90..86ce659 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/index.ts b/src/index.ts index 470f589..df27a39 100644 --- a/src/index.ts +++ b/src/index.ts @@ -41,6 +41,7 @@ export type FlatRoutesOptions = { paramPrefixChar?: string ignoredRouteFiles?: string[] routeRegex?: RegExp + enableUniqueIdCheck?: boolean } export type DefineRoutesFunction = ( @@ -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 @@ -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,