diff --git a/.changeset/shy-laws-wonder.md b/.changeset/shy-laws-wonder.md new file mode 100644 index 000000000..a58919754 --- /dev/null +++ b/.changeset/shy-laws-wonder.md @@ -0,0 +1,10 @@ +--- +"@opennextjs/cloudflare": patch +--- + +fix: patch the createInlinedDataReadableStream function not to use the type: "bytes" option in ReadableStream constructor + +workerd seems not to correctly support the type: "bytes" option in ReadableStream constructor, that breaks inlined flight data streaming over 4kB. +By removing the type: "bytes" option, the inlined flight data will not be split into multiple chunks, and the streaming will work correctly. + +(fixes #1225) diff --git a/packages/cloudflare/src/cli/build/open-next/createServerBundle.ts b/packages/cloudflare/src/cli/build/open-next/createServerBundle.ts index c556b6026..8107c79cc 100644 --- a/packages/cloudflare/src/cli/build/open-next/createServerBundle.ts +++ b/packages/cloudflare/src/cli/build/open-next/createServerBundle.ts @@ -27,6 +27,7 @@ import type { Plugin } from "esbuild"; import { getOpenNextConfig } from "../../../api/config.js"; import { normalizePath } from "../../utils/normalize-path.js"; +import { patchFlightReadableStreamType } from "../patches/plugins/flight-readable-stream-type.js"; import { patchResRevalidate } from "../patches/plugins/res-revalidate.js"; import { patchTurbopackRuntime } from "../patches/plugins/turbopack.js"; import { patchUseCacheIO } from "../patches/plugins/use-cache.js"; @@ -209,6 +210,7 @@ async function generateBundle( awsPatches.patchBackgroundRevalidation, awsPatches.patchNodeEnvironment, // Cloudflare specific patches + patchFlightReadableStreamType, patchResRevalidate, patchUseCacheIO, patchTurbopackRuntime, diff --git a/packages/cloudflare/src/cli/build/patches/plugins/flight-readable-stream-type.spec.ts b/packages/cloudflare/src/cli/build/patches/plugins/flight-readable-stream-type.spec.ts new file mode 100644 index 000000000..0c3979519 --- /dev/null +++ b/packages/cloudflare/src/cli/build/patches/plugins/flight-readable-stream-type.spec.ts @@ -0,0 +1,37 @@ +import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js"; +import { describe, expect, test } from "vitest"; + +import { rule } from "./flight-readable-stream-type.js"; + +describe("patchFlightReadableStreamType", () => { + test("removes type property of ReadableStream constructor only in createInlinedDataReadableStream", () => { + const code = ` +new ReadableStream({type: "bytes", start(e){ e.enqueue(n); }}); + +function tl(e,t,r){ + let n = t ? '