Skip to content

Commit f42488a

Browse files
authored
fix: decode path params in builder (#5382)
User got encoded Cyrillic slugs when tested in builder. Now decoded the result of URLPattern parsing.
1 parent 0376075 commit f42488a

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

apps/builder/app/builder/shared/url-pattern.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,25 @@ import { expect, test } from "vitest";
22
import {
33
compilePathnamePattern,
44
isPathnamePattern,
5+
matchPathnamePattern,
56
tokenizePathnamePattern,
67
validatePathnamePattern,
78
} from "./url-pattern";
89

10+
test("decode matched params", () => {
11+
expect(matchPathnamePattern("/blog/:slug", "/blog/привет")).toEqual({
12+
slug: "привет",
13+
});
14+
expect(
15+
matchPathnamePattern(
16+
"/blog/:slug",
17+
"/blog/%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82"
18+
)
19+
).toEqual({
20+
slug: "привет",
21+
});
22+
});
23+
924
test("check pathname is pattern", () => {
1025
expect(isPathnamePattern("/:name")).toEqual(true);
1126
expect(isPathnamePattern("/:slug*")).toEqual(true);

apps/builder/app/builder/shared/url-pattern.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
import { matchPathnameParams } from "@webstudio-is/sdk";
22
import { URLPattern } from "urlpattern-polyfill";
3+
34
export { isPathnamePattern } from "@webstudio-is/sdk";
45

56
const baseUrl = "http://url";
67

8+
const tryDecode = (encoded: string) => {
9+
try {
10+
return decodeURIComponent(encoded);
11+
} catch {
12+
return encoded;
13+
}
14+
};
15+
716
export const matchPathnamePattern = (pattern: string, pathname: string) => {
817
try {
9-
return new URLPattern({ pathname: pattern }).exec({ pathname })?.pathname
10-
.groups;
18+
const groups = new URLPattern({ pathname: pattern }).exec({ pathname })
19+
?.pathname.groups;
20+
if (groups) {
21+
const decodedGroups: Record<string, undefined | string> = {};
22+
for (const [name, value] of Object.entries(groups)) {
23+
if (value) {
24+
decodedGroups[name] = tryDecode(value);
25+
}
26+
}
27+
return decodedGroups;
28+
}
1129
} catch {
1230
// empty block
1331
}

apps/builder/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
"strip-indent": "^4.0.0",
120120
"tiny-invariant": "^1.3.3",
121121
"title-case": "^4.3.2",
122-
"urlpattern-polyfill": "^10.0.0",
122+
"urlpattern-polyfill": "^10.1.0",
123123
"use-debounce": "^10.0.4",
124124
"valid-filename": "^4.0.0",
125125
"warn-once": "^0.1.1",

pnpm-lock.yaml

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

0 commit comments

Comments
 (0)