Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fast-clouds-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opennextjs/aws": patch
---

decode path params in cache interceptor
25 changes: 25 additions & 0 deletions packages/open-next/src/core/routing/cacheInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,28 @@ async function generateResult(
},
};
}
/**
*
* SSG cache key needs to be decoded, but some characters needs to be properly escaped
* https://github.com/vercel/next.js/blob/34039551d2e5f611c0abde31a197d9985918adaf/packages/next/src/server/lib/router-utils/decode-path-params.ts#L11-L26
*/
function decodePathParams(pathname: string): string {
return pathname
.split("/")
.map((segment) => {
try {
// https://github.com/vercel/next.js/blob/34039551d2e5f611c0abde31a197d9985918adaf/packages/next/src/shared/lib/router/utils/escape-path-delimiters.ts#L2-L10
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: what about copy the original escapePathDelimiters from next and call from here.

It would be easier to check that the code is in sync / update when needed.

return decodeURIComponent(segment).replace(
/([\/#?]|%(2f|23|3f|5c))/gi,
(char: string) => encodeURIComponent(char),
);
} catch (e) {
// If decodeURIComponent fails, we return the original segment
return segment;
}
})
.join("/");
}

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

// Then we decode the path params
localizedPath = decodePathParams(localizedPath);

debug("Checking cache for", localizedPath, PrerenderManifest);

const isISR =
Expand Down
Loading