From c64941d2b22fc07e20278cbd92cf814e30c2ca97 Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Mon, 3 Feb 2025 11:13:54 +0100 Subject: [PATCH 1/2] fix(cloudflare): enable trailiing slash redirect event.url is a valid URL within cloudflare vs a path otherwise --- .changeset/lovely-trains-teach.md | 5 +++++ packages/open-next/src/core/routing/matcher.ts | 7 +++++-- 2 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 .changeset/lovely-trains-teach.md diff --git a/.changeset/lovely-trains-teach.md b/.changeset/lovely-trains-teach.md new file mode 100644 index 000000000..f51ab220c --- /dev/null +++ b/.changeset/lovely-trains-teach.md @@ -0,0 +1,5 @@ +--- +"@opennextjs/aws": patch +--- + +fix(cloudflare): enable trailiing slash redirect diff --git a/packages/open-next/src/core/routing/matcher.ts b/packages/open-next/src/core/routing/matcher.ts index 8c0dabad0..ebf9169ef 100644 --- a/packages/open-next/src/core/routing/matcher.ts +++ b/packages/open-next/src/core/routing/matcher.ts @@ -255,8 +255,8 @@ export function handleRewrites( function handleTrailingSlashRedirect( event: InternalEvent, ): false | InternalResult { - const url = new URL(event.url, "http://localhost"); - const emptyBody = emptyReadableStream(); + // When rawPath is `//domain`, `url.host` would be `domain`. + const url = new URL(event.rawPath, "http://localhost"); if ( // Someone is trying to redirect to a different origin, let's not do that @@ -267,6 +267,9 @@ function handleTrailingSlashRedirect( ) { return false; } + + const emptyBody = emptyReadableStream(); + if ( NextConfig.trailingSlash && !event.headers["x-nextjs-data"] && From 269cbac8f599497df79a2eebeebcb72d54ed540d Mon Sep 17 00:00:00 2001 From: Victor Berchet Date: Mon, 3 Feb 2025 11:23:45 +0100 Subject: [PATCH 2/2] Update packages/open-next/src/core/routing/matcher.ts Co-authored-by: conico974 --- packages/open-next/src/core/routing/matcher.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/open-next/src/core/routing/matcher.ts b/packages/open-next/src/core/routing/matcher.ts index ebf9169ef..32a127bdd 100644 --- a/packages/open-next/src/core/routing/matcher.ts +++ b/packages/open-next/src/core/routing/matcher.ts @@ -256,6 +256,7 @@ function handleTrailingSlashRedirect( event: InternalEvent, ): false | InternalResult { // When rawPath is `//domain`, `url.host` would be `domain`. + // https://github.com/opennextjs/opennextjs-aws/issues/355 const url = new URL(event.rawPath, "http://localhost"); if (