Skip to content

Commit 6812e72

Browse files
committed
WIP try out newer path-to-regexp
1 parent 36ecf9f commit 6812e72

File tree

4 files changed

+631
-67
lines changed

4 files changed

+631
-67
lines changed

packages/fastify-renderer/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"@vitejs/plugin-react-refresh": "^1.3.6",
6565
"fastify-plugin": "^4.5.1",
6666
"http-errors": "^1.8.1",
67-
"path-to-regexp": "^6.2.1",
67+
"path-to-regexp": "^8.3.0",
6868
"sanitize-filename": "^1.6.3",
6969
"stream-template": "^0.0.10",
7070
"vite": "^2.9.15",
Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
1-
import { Key, pathToRegexp } from 'path-to-regexp'
1+
import { match, MatchFunction } from 'path-to-regexp'
22
import { MatcherFn } from 'wouter'
33

4+
type ParamData = Record<string, string | string[]>
5+
46
/*
5-
* This function specifies how strings like /app/:users/:items* are transformed into regular expressions to pass into path-to-regexp.
7+
* This function creates a matcher function for a given path pattern.
68
*
79
* @param {string} path — a path like "/:foo/:bar"
8-
* @return {{ keys: [], regexp: RegExp }}
10+
* @return {MatchFunction<ParamData>} — a function that matches paths and extracts params
911
*/
10-
const convertPathToRegexp = (path: string) => {
11-
const keys: Key[] = []
12-
const regexp = pathToRegexp(path, keys)
13-
return { keys, regexp }
12+
const createMatcher = (path: string): MatchFunction<ParamData> => {
13+
return match(path, { decode: decodeURIComponent })
1414
}
1515

16-
const cache: Record<string, ReturnType<typeof convertPathToRegexp>> = {}
16+
const cache: Record<string, MatchFunction<ParamData>> = {}
1717

18-
// obtains a cached regexp version of the pattern
19-
const getRegexp = (pattern: string) => cache[pattern] || (cache[pattern] = convertPathToRegexp(pattern))
18+
// obtains a cached matcher function for the pattern
19+
const getMatcher = (pattern: string) => cache[pattern] || (cache[pattern] = createMatcher(pattern))
2020

2121
export const matcher: MatcherFn = (pattern, path) => {
22-
const { regexp, keys } = getRegexp(pattern || '')
23-
const out = regexp.exec(path.split('#')[0].split('?')[0])
24-
25-
if (!out) return [false, null]
22+
const matchFn = getMatcher(String(pattern || ''))
23+
const pathStr = String(path)
24+
const cleanPath = pathStr.split('#')[0].split('?')[0]
25+
const result = matchFn(cleanPath)
2626

27-
// formats an object with matched params
28-
const params = keys.reduce((params, key, i) => {
29-
params[key.name] = out[i + 1]
30-
return params
31-
}, {})
27+
if (!result) return [false, null]
3228

33-
return [true, params]
29+
// return matched params
30+
return [true, result.params as Record<string, string>]
3431
}

packages/test-apps/simple-react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"react": "*",
1515
"react-dom": "*",
1616
"wouter": "^2.7.5",
17-
"path-to-regexp": "^6.2.1"
17+
"path-to-regexp": "^8.3.0"
1818
},
1919
"devDependencies": {
2020
"@playwright/test": "^1.39.0",

0 commit comments

Comments
 (0)