Skip to content

Commit 14d9ff0

Browse files
committed
feat: safe building route urls
1 parent 5d07e92 commit 14d9ff0

4 files changed

Lines changed: 19 additions & 1 deletion

File tree

.changeset/polite-tables-swim.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mobx-route": minor
3+
---
4+
5+
added `fallbackPath` parameter for cases when `path-to-regexp` thrown an error

.changeset/quiet-candles-happen.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"mobx-route": minor
3+
---
4+
5+
safe building route urls using `path-to-regexp` lib

src/core/config/config.types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ export interface RouteGlobalConfig {
88
mergeQuery?: boolean;
99
createUrl?: UrlCreateParamsFn;
1010
formatLinkHref?: (href: string) => string;
11+
fallbackPath?: string;
1112
}

src/core/route/route.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,14 @@ export class Route<
343343
routeConfig.get().createUrl?.(defaultUrlCreateParams, this.query.data) ??
344344
defaultUrlCreateParams;
345345

346-
const path = this._compiler(this.processParams(urlCreateParams.params));
346+
let path: string;
347+
348+
try {
349+
path = this._compiler(this.processParams(urlCreateParams.params));
350+
} catch (e) {
351+
console.error('Error while compiling route path', e);
352+
path = this.config.fallbackPath ?? routeConfig.get().fallbackPath ?? '/';
353+
}
347354

348355
const url = `${urlCreateParams.baseUrl || ''}${this.isHash ? '#' : ''}${path}`;
349356

0 commit comments

Comments
 (0)