8000 decode path params in cache interceptor · opennextjs/opennextjs-aws@13dcdc3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 13dcdc3

Browse files
author
Nicolas Dorseuil
committed
decode path params in cache interceptor
1 parent ae1afbb commit 13dcdc3

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

.changeset/fast-clouds-shout.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opennextjs/aws": patch
3+
---
4+
5+
decode path params in cache interceptor

packages/open-next/src/core/routing/cacheInterceptor.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,28 @@ async function generateResult(
129129
},
130130
};
131131
}
132+
/**
133+
*
134+
* SSG cache key needs to be decoded, but some characters needs to be properly escaped
135+
* https://github.com/vercel/next.js/blob/34039551d2e5f611c0abde31a197d9985918ad 8000 af/packages/next/src/server/lib/router-utils/decode-path-params.ts#L11-L26
136+
*/
137+
function decodePathParams(pathname: string): string {
138+
return pathname
139+
.split("/")
140+
.map((segment) => {
141+
try {
142+
// https://github.com/vercel/next.js/blob/34039551d2e5f611c0abde31a197d9985918adaf/packages/next/src/shared/lib/router/utils/escape-path-delimiters.ts#L2-L10
143+
return decodeURIComponent(segment).replace(
144+
/([\/#?]|%(2f|23|3f|5c))/gi,
145+
(char: string) => encodeURIComponent(char),
146+
);
147+
} catch (e) {
148+
// If decodeURIComponent fails, we return the original segment
149+
return segment;
150+
}
151+
})
152+
.join("/");
153+
}
132154

133155
export async function cacheInterceptor(
134156
event: InternalEvent,
@@ -147,6 +169,9 @@ export async function cacheInterceptor(
147169
// We also need to remove trailing slash
148170
localizedPath = localizedPath.replace(/\/$/, "");
149171

172+
// Then we decode the path params
173+
localizedPath = decodePathParams(localizedPath);
174+
150175
debug("Checking cache for", localizedPath, PrerenderManifest);
151176

152177
const isISR =

0 commit comments

Comments
 (0)
0