10000 Fix decode path params in cache interceptor by conico974 · Pull Request #868 · opennextjs/opennextjs-aws · GitHub
[go: up one dir, main page]

Skip to content

Fix decode path params in cache interceptor #868

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 36 additions & 0 deletions packages/open-next/src/core/routing/cacheInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,39 @@ async function generateResult(
};
}

/**
*
* https://github.com/vercel/next.js/blob/34039551d2e5f611c0abde31a197d9985918adaf/packages/next/src/shared/lib/router/utils/escape-path-delimiters.ts#L2-L10
*/
function escapePathDelimiters(
segment: string,
escapeEncoded?: boolean,
): string {
return segment.replace(
new RegExp(`([/#?]${escapeEncoded ? "|%(2f|23|3f|5c)" : ""})`, "gi"),
(char: string) => encodeURIComponent(char),
);
}

/**
*
* 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 {
return escapePathDelimiters(decodeURIComponent(segment), true);
} catch (e) {
// If decodeURIComponent fails, we return the original segment
return segment;
}
})
.join("/");
}

export async function cacheInterceptor(
event: InternalEvent,
): Promise<InternalEvent | InternalResult> {
Expand All @@ -147,6 +180,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
0