Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/inferno-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"dependencies": {
"history": "^5.3.0",
"inferno": "9.0.10",
"path-to-regexp": "^1.9.0"
"path-to-regexp": "^8.3.0"
},
"devDependencies": {
"inferno-server": "9.0.10",
Expand Down
45 changes: 26 additions & 19 deletions packages/inferno-router/src/matchPath.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
import pathToRegexp from 'path-to-regexp';
import { pathToRegexp, Keys } from 'path-to-regexp';
import { type Match } from './Route';

const patternCache = {};
const cacheLimit = 10000;
let cacheCount = 0;

interface pathToRegexKey {
name: string | number;
prefix: string;
delimiter: string;
optional: boolean;
repeat: boolean;
pattern: string;
partial: boolean;
asterisk: boolean;
}

const compilePath = (pattern, options): { re: any; keys: pathToRegexKey[] } => {
const compilePath = (pattern, options): { re: any; keys: Keys } => {
const cacheKey = `${options.end}${options.strict}${options.sensitive}`;
const cache = patternCache[cacheKey] || (patternCache[cacheKey] = {});

if (cache[pattern]) {
return cache[pattern];
}

const keys = [];
const re = pathToRegexp(pattern, keys, options);
options.trailing = options.end && !options.strict;
pattern = pattern.replace(/\?/, '{.:optqspunct}');
if (!options.exact && !options.strict) {
pattern = pattern.replace(/\/$/, '{.:optendslash}');
}

const { regexp: re, keys } = pathToRegexp(pattern, options)
const compiledPattern = { re, keys };

if (cacheCount < cacheLimit) {
Expand All @@ -52,15 +46,27 @@ export function matchPath(pathname, options: any): Match<any> | null {
loader,
initialData = {},
} = options;

const loaderData = initialData[path];

if (path === '*' || path === '/*') {
return {
isExact: false,
loader,
loaderData,
params: [],
path,
url: '/'
};
}

const { re, keys } = compilePath(path, { end: exact, strict, sensitive });
const match = re.exec(pathname);

if (!match) {
return null;
}

const loaderData = initialData[path];

const [url, ...values] = match;
const isExact = pathname === url;

Expand All @@ -73,10 +79,11 @@ export function matchPath(pathname, options: any): Match<any> | null {
loader,
loaderData,
params: keys.reduce((memo, key, index) => {
memo[key.name] = values[index];
if (values[index] !== undefined)
memo[key.name] = values[index];
return memo;
}, {}),
path, // the path pattern used to match
url: path === '/' && url === '' ? '/' : url, // the matched portion of the URL
};
}
}
Loading