Skip to content

Commit 5016bb7

Browse files
committed
update path-to-regexp
1 parent 3f97285 commit 5016bb7

3 files changed

Lines changed: 21 additions & 31 deletions

File tree

package-lock.json

Lines changed: 8 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/inferno-router/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"dependencies": {
5151
"history": "^5.3.0",
5252
"inferno": "9.0.4",
53-
"path-to-regexp": "^1.9.0"
53+
"path-to-regexp": "^8.3.0"
5454
},
5555
"devDependencies": {
5656
"inferno-server": "9.0.4",

packages/inferno-router/src/matchPath.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,25 @@
1-
import pathToRegexp from 'path-to-regexp';
1+
import { pathToRegexp, Keys } from 'path-to-regexp';
22
import { type Match } from './Route';
33

44
const patternCache = {};
55
const cacheLimit = 10000;
66
let cacheCount = 0;
77

8-
interface pathToRegexKey {
9-
name: string | number;
10-
prefix: string;
11-
delimiter: string;
12-
optional: boolean;
13-
repeat: boolean;
14-
pattern: string;
15-
partial: boolean;
16-
asterisk: boolean;
17-
}
18-
19-
const compilePath = (pattern, options): { re: any; keys: pathToRegexKey[] } => {
8+
const compilePath = (pattern, options): { re: any; keys: Keys } => {
209
const cacheKey = `${options.end}${options.strict}${options.sensitive}`;
2110
const cache = patternCache[cacheKey] || (patternCache[cacheKey] = {});
2211

2312
if (cache[pattern]) {
2413
return cache[pattern];
2514
}
2615

27-
const keys = [];
28-
const re = pathToRegexp(pattern, keys, options);
16+
options.trailing = options.end && !options.strict;
17+
pattern = pattern.replace(/\?/, '{.:optqspunct}');
18+
if (!options.exact && !options.strict) {
19+
pattern = pattern.replace(/\/$/, '{.:optendslash}');
20+
}
21+
22+
const { regexp: re, keys } = pathToRegexp(pattern, options)
2923
const compiledPattern = { re, keys };
3024

3125
if (cacheCount < cacheLimit) {
@@ -73,10 +67,11 @@ export function matchPath(pathname, options: any): Match<any> | null {
7367
loader,
7468
loaderData,
7569
params: keys.reduce((memo, key, index) => {
76-
memo[key.name] = values[index];
70+
if (values[index] !== undefined)
71+
memo[key.name] = values[index];
7772
return memo;
7873
}, {}),
7974
path, // the path pattern used to match
8075
url: path === '/' && url === '' ? '/' : url, // the matched portion of the URL
8176
};
82-
}
77+
}

0 commit comments

Comments
 (0)