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..32a127bdd 100644 --- a/packages/open-next/src/core/routing/matcher.ts +++ b/packages/open-next/src/core/routing/matcher.ts @@ -255,8 +255,9 @@ 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`. + // https://github.com/opennextjs/opennextjs-aws/issues/355 + 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 +268,9 @@ function handleTrailingSlashRedirect( ) { return false; } + + const emptyBody = emptyReadableStream(); + if ( NextConfig.trailingSlash && !event.headers["x-nextjs-data"] &&