diff --git a/.changeset/hip-bags-argue.md b/.changeset/hip-bags-argue.md new file mode 100644 index 00000000..989dd9c0 --- /dev/null +++ b/.changeset/hip-bags-argue.md @@ -0,0 +1,5 @@ +--- +"@opennextjs/cloudflare": patch +--- + +fix: Ensure the protocol is correct for redirects in server actions during preview mode diff --git a/packages/cloudflare/src/cli/templates/init.ts b/packages/cloudflare/src/cli/templates/init.ts index 4ffe33d5..defec664 100644 --- a/packages/cloudflare/src/cli/templates/init.ts +++ b/packages/cloudflare/src/cli/templates/init.ts @@ -132,6 +132,12 @@ function populateProcessEnv(url: URL, env: CloudflareEnv) { port: url.port, }, }); + + /* We need to set this environment variable to make redirects work properly in preview mode. + * Next sets this in standalone mode during `startServer`. Without this the protocol would always be `https` here: + * https://github.com/vercel/next.js/blob/6b1e48080e896e0d44a05fe009cb79d2d3f91774/packages/next/src/server/app-render/action-handler.ts#L307-L316 + */ + process.env.__NEXT_PRIVATE_ORIGIN = url.origin; } /* eslint-disable no-var */