Skip to content

[Feature] Memory usage when uploading a large file #1008

@harveyjing

Description

@harveyjing

Describe the bug

I know this can be circumvented by persigned url or third party storage service. But I just wanna know why.
The scenario is uploading a large file(10GB) through the Workers using Opennextjs framework. Supporse the upload api is /api/upload and in it I added some logs. Like blow:

export async function POST(request: Request) {

    console.log("starting upload", Date.now());

    const headers_0 = request.headers;
    console.log("headers_0", headers_0, Date.now());

    console.log("starting geting the body", Date.now());
    const body = await request.body;
    console.log("got the body", Date.now());

    return new Response("Uploaded", { status: 200 });
}

Then start the opennextjs server by pnpm run preview.
Then I upload a 600MB file using command blow:

curl -X POST http://localhost:8787/api/upload --data-binary @./600MB.zip --limit-rate 100M

And I watch the console out and wait for about 6 seconds the console will print out the starting upload logs.
The worked process's memory is bloating up to more than 1GB.
It seems the server buffered the request then enter the POST handler.
I was confusing why.

I wanna use streaming upload but this make it impossiable.

So I tested with other ways to see their behaviours.

  1. Native Workers (Hello World template):
    I implememnt the same api in the fetch function.
    I upload the same file using the same command and see the console logs printed out instantly without waiting for 6 seconds. [GOOD]
    Memory usage is very low(20~30MB) in the worked process.
  2. creat-next-app:
    This is the official Nextjs scaffold tool. I added the upload api and start the production server using pnpm run start.
    I upload the same file using the same command and see the console logs printed out instantly without waiting for 6 seconds. [GOOD]

Any information would be appreciated.

Steps to reproduce

Prerequest: Create a project using pnpm create cloudflare@latest my-next-app --framework=next

  1. Create file ./api/upload/route.ts under app folder.
  2. Add this route blow:
export async function POST(request: Request) {

    console.log("starting upload", Date.now());

    const headers_0 = request.headers;
    console.log("headers_0", headers_0, Date.now());

    console.log("starting geting the body", Date.now());
    const body = await request.body;
    console.log("got the body", Date.now());

    return new Response("Uploaded", { status: 200 });
}
  1. pnpm preview.
  2. curl -X POST http://localhost:8787/api/upload --data-binary @./600MB.zip --limit-rate 100M
    Watch the console output. It only print out logs after about 6 seconds.

Expected behavior

It should print out logs instantly.

@opennextjs/cloudflare version

1.13.1

Wrangler version

4.50.0

next info output

✗ % pnpm exec next info
exec next info
npm warn Unknown env config "verify-deps-before-run". This will stop working in the next major version of npm.
Using vars defined in .dev.vars
npm warn Unknown env config "verify-deps-before-run". This will stop working in the next major version of npm.
/bin/sh: yarn: command not found

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.1.0: Mon Oct 20 19:34:05 PDT 2025; root:xnu-12377.41.6~2/RELEASE_ARM64_T6041
  Available memory (MB): 49152
  Available CPU cores: 14
Binaries:
  Node: 22.14.0
  npm: 11.4.1
  Yarn: N/A
  pnpm: 10.12.1
Relevant Packages:
  next: 15.5.6 // An outdated version detected (latest is 16.0.4), upgrade is highly recommended!
  eslint-config-next: 15.4.6
  react: 19.1.0
  react-dom: 19.1.0
  typescript: 5.9.3
Next.js Config:
  output: N/A
 ⚠ An outdated version detected (latest is 16.0.4), upgrade is highly recommended!
   Please try the latest canary version (`npm install next@canary`) to confirm the issue still exists before creating a new issue.
   Read more - https://nextjs.org/docs/messages/opening-an-issue

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions