Skip to content

Unexpected middleware behavior: Cannot use Astro.rewrite after the request body has been read #13844

@xriter

Description

@xriter

Astro Info

Astro                    v5.7.13
Node                     v20.19.1
System                   Linux (x64)
Package Manager          npm
Output                   server
Adapter                  @astrojs/node
Integrations             none

Describe the Bug

In my middleware there might be some intentional rewrites. But I ran into some unexpected behavior where I got the error:

Astro.rewrite() cannot be used if the request body has already been read. If you need to read the body, first clone the request.

I created a minimal reproducible example on Stackblitz.

  • Check out the src/middleware.ts file.
  • Then open the preview and submit the form.
  • The error will show.

I don't understand why though. The error mentions "If you need to read the body, first clone the request", but I am doing that. Why/where is Astro.rewrite() called anyway?

Am I misunderstanding this, or could this be a bug?
Perhaps someone could enlighten me? 😇

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-8mju777p?file=src%2Fmiddleware.ts

Metadata

Metadata

Assignees

No one assigned

    Labels

    - P3: minor bugAn edge case that only affects very specific usage (priority)feat: routingRelated to Astro routing (scope)help wantedPlease help with this issue!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions