diff --git a/package-lock.json b/package-lock.json index 1e30a56e4..0ae79eedb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15646,6 +15646,16 @@ "dev": true, "license": "ISC" }, + "node_modules/path-to-regexp": { + "version": "8.3.0", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-8.3.0.tgz", + "integrity": "sha512-7jdwVIRtsP8MYpdXSwOS0YdD0Du+qOoF/AEPIt88PcCFrZCzx41oxku1jD88hZBwbNUIEfpqvuhjFaMAqMTWnA==", + "license": "MIT", + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, "node_modules/path-type": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/path-type/-/path-type-3.0.0.tgz", @@ -19204,7 +19214,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", @@ -19215,21 +19225,6 @@ "history": "^5.3.0" } }, - "packages/inferno-router/node_modules/isarray": { - "version": "0.0.1", - "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", - "integrity": "sha512-D2S+3GLxWH+uhrNEcoh/fnmYeP8E8/zHl644d/jdA0g2uyXvy3sb0qxotE+ne0LtccHknQzWwZEzhak7oJ0COQ==", - "license": "MIT" - }, - "packages/inferno-router/node_modules/path-to-regexp": { - "version": "1.9.0", - "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-1.9.0.tgz", - "integrity": "sha512-xIp7/apCFJuUHdDLWe8O1HIkb0kQrOMb/0u6FXQjemHn/ii5LrIzU6bdECnsiTF/GjZkMEKg1xdiZwNqDYlZ6g==", - "license": "MIT", - "dependencies": { - "isarray": "0.0.1" - } - }, "packages/inferno-server": { "version": "9.0.10", "license": "MIT", diff --git a/packages/inferno-router/package.json b/packages/inferno-router/package.json index a9d4ebfa3..0d7af6be9 100644 --- a/packages/inferno-router/package.json +++ b/packages/inferno-router/package.json @@ -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", diff --git a/packages/inferno-router/src/matchPath.ts b/packages/inferno-router/src/matchPath.ts index ee32bf10f..c343b96a2 100644 --- a/packages/inferno-router/src/matchPath.ts +++ b/packages/inferno-router/src/matchPath.ts @@ -1,22 +1,11 @@ -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] = {}); @@ -24,8 +13,13 @@ const compilePath = (pattern, options): { re: any; keys: pathToRegexKey[] } => { 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) { @@ -52,6 +46,20 @@ export function matchPath(pathname, options: any): Match | 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); @@ -59,8 +67,6 @@ export function matchPath(pathname, options: any): Match | null { return null; } - const loaderData = initialData[path]; - const [url, ...values] = match; const isExact = pathname === url; @@ -73,10 +79,11 @@ export function matchPath(pathname, options: any): Match | 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 }; -} +} \ No newline at end of file