Skip to content

Only in Bun: Astro middleware rewrite: [ERROR] Body already used #19939

@xriter

Description

@xriter

What version of Bun is running?

1.2.14+6a363a38d

What platform is your computer?

Darwin 24.5.0 arm64 arm

What steps can reproduce the bug?

more minimal repo

original report:

This is a continuation of this issue: withastro/astro#13844

So I tried creating the most minimal reproduction locally and I found that the issue might actually be with Bun (which I use to run astro). Somehow Bun is already consuming the request body somewhere?

// src/middleware.ts
import { sequence } from "astro/middleware";

export const onRequest = sequence((ctx, next) => {
	if (ctx.url.pathname == "/oldlink") {
		return next("/newlink");
	}
	return next();
});
// src/pages/newlink.ts
import type { APIRoute } from "astro";

export const POST: APIRoute = async ({ request }) => {
	const data = await request.json();
	return new Response(JSON.stringify(data), { status: 200 });
};
// package.json
///...
"scripts": {
    "devnode": "astro dev"
    "devbun": "bunx --bun astro dev"
}
//...

When I run this setup with Node (npm run devnode or bun run devnode), it works.
But with Bun (bun run devbun), I get [ERROR] Body already used.

What is the expected behavior?

To see the posted json.

What do you see instead?

[ERROR] Body already used

Additional information

Here is the minimal reproducible example:
https://stackblitz.com/edit/github-8mju777p-4e8ddvcl?file=src%2Fpages%2Findex.astro

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingconfirmed bugWe can reproduce this issueweb:fetch

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions