Skip to content

Commit 8d75b44

Browse files
authored
Merge pull request #146 from episerver/bugfix/CMS-46443-loose-address
Handle URL paths with and without trailing slashes
2 parents d49fb2f + 50138b4 commit 8d75b44

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

packages/optimizely-cms-sdk/src/graph/filters.ts

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,51 @@
66
* This is used internally in the SDK
77
*/
88

9+
/** Returns two versions of the same path. One with trailing slash and one without it */
10+
function normalizePath(path: string) {
11+
if (path.endsWith('/')) {
12+
return {
13+
pathWithTrailingSlash: path,
14+
pathWithoutTrailingSlash: path.slice(0, -1),
15+
};
16+
} else {
17+
return {
18+
pathWithTrailingSlash: path + '/',
19+
pathWithoutTrailingSlash: path,
20+
};
21+
}
22+
}
23+
924
/**
1025
* Creates a {@linkcode ContentInput} object that filters results by a specific URL path.
1126
*
1227
* @param path - The URL path to filter by.
1328
* @returns A `GraphQueryArguments` object with a `where` clause that matches the given path.
1429
*/
1530
export function pathFilter(path: string, host?: string): ContentInput {
31+
const { pathWithTrailingSlash, pathWithoutTrailingSlash } =
32+
normalizePath(path);
33+
1634
return {
1735
where: {
18-
_metadata: {
19-
url: {
20-
default: {
21-
eq: path,
36+
_or: [
37+
{
38+
_metadata: {
39+
url: {
40+
default: { eq: pathWithTrailingSlash },
41+
base: host ? { eq: host } : undefined,
42+
},
2243
},
23-
base: host ? { eq: host } : undefined,
2444
},
25-
},
45+
{
46+
_metadata: {
47+
url: {
48+
default: { eq: pathWithoutTrailingSlash },
49+
base: host ? { eq: host } : undefined,
50+
},
51+
},
52+
},
53+
],
2654
},
2755
};
2856
}

0 commit comments

Comments
 (0)