Skip to content

Commit 3a7e6a2

Browse files
authored
[DEV-19820] Fix - Special chars in slug matching (#864)
* Add `URL_PATTERN_SEGMENT_CHARSET` constant * Support `!` and `'` in pathname params parsing
1 parent d708cf3 commit 3a7e6a2

3 files changed

Lines changed: 13 additions & 3 deletions

File tree

packages/core/src/routing/Route.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Locale } from '@prezly/theme-kit-intl';
22
import UrlPattern from 'url-pattern';
33

4+
import { URL_PATTERN_SEGMENT_CHARSET } from './constants';
45
import type { ExtractPathParams } from './types';
56
import { normalizeUrl } from './utils';
67

@@ -50,8 +51,12 @@ export namespace Route {
5051
rewrite: string,
5152
{ check, generate, resolveLocale }: Options<Pattern, Match> = {},
5253
): Route<Pattern, Match> {
53-
const urlPattern = new UrlPattern(pattern);
54-
const rewritePattern = new UrlPattern(rewrite);
54+
const urlPattern = new UrlPattern(pattern, {
55+
segmentValueCharset: URL_PATTERN_SEGMENT_CHARSET,
56+
});
57+
const rewritePattern = new UrlPattern(rewrite, {
58+
segmentValueCharset: URL_PATTERN_SEGMENT_CHARSET,
59+
});
5560

5661
return {
5762
pattern,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const URL_PATTERN_SEGMENT_CHARSET = "a-zA-Z0-9-_~ %!'";

packages/core/src/routing/utils/generateUrlFromPattern.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { Locale } from '@prezly/theme-kit-intl';
22
import { omitUndefined } from '@technically/omit-undefined';
33
import UrlPattern from 'url-pattern';
44

5+
import { URL_PATTERN_SEGMENT_CHARSET } from '../constants';
6+
57
import { getShortestLocaleSlug } from './getShortestLocaleSlug';
68
import { normalizeUrl } from './normalizeUrl';
79

@@ -24,7 +26,9 @@ function toUrlPattern(pattern: string): UrlPattern {
2426

2527
if (cached) return cached;
2628

27-
const urlPattern = new UrlPattern(pattern);
29+
const urlPattern = new UrlPattern(pattern, {
30+
segmentValueCharset: URL_PATTERN_SEGMENT_CHARSET,
31+
});
2832

2933
CACHE.set(pattern, urlPattern);
3034

0 commit comments

Comments
 (0)