Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Astro Actions: POST Requests with Rewriting Throw 'The Body Has Already Been Consumed' Error #11470

Closed
1 task
robinjonsson opened this issue Jul 15, 2024 · 5 comments
Labels
needs response Issue needs response from OP needs triage Issue needs to be triaged

Comments

@robinjonsson
Copy link

Astro Info

Astro                    v4.11.5
Node                     v20.10.0
System                   macOS (arm64)
Package Manager          pnpm
Output                   server
Adapter                  none
Integrations             none

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

If you activate actions in a project that uses rewriting, you will not be able to consume the formData in a POST request.

Stack trace

TypeError
    at _Request.formData (node:internal/deps/undici/undici:4684:35)
    at async eval (/Users/robinjonsson/projects_demo/astro-actions-formdata/src/pages/en/post.astro:14:20)
    at async callComponentAsTemplateResultOrResponse (file:///Users/robinjonsson/projects_demo/astro-actions-formdata/node_modules/.pnpm/[email protected][email protected]/node_modules/astro/dist/runtime/server/render/astro/render.js:90:25)
    at async renderToAsyncIterable (file:///Users/robinjonsson/projects_demo/astro-actions-formdata/node_modules/.pnpm/[email protected][email protected]/node_modules/astro/dist/runtime/server/render/astro/render.js:132:26)
    at async renderPage (file:///Users/robinjonsson/projects_demo/astro-actions-formdata/node_modules/.pnpm/[email protected][email protected]/node_modules/astro/dist/runtime/server/render/page.js:31:24)
    at async lastNext (file:///Users/robinjonsson/projects_demo/astro-actions-formdata/node_modules/.pnpm/[email protected][email protected]/node_modules/astro/dist/core/render-context.js:135:25)
    at async callMiddleware (file:///Users/robinjonsson/projects_demo/astro-actions-formdata/node_modules/.pnpm/[email protected][email protected]/node_modules/astro/dist/core/middleware/callMiddleware.js:21:10)
    at async RenderContext.render (file:///Users/robinjonsson/projects_demo/astro-actions-formdata/node_modules/.pnpm/[email protected][email protected]/node_modules/astro/dist/core/render-context.js:163:22)
    at async handleRoute (file:///Users/robinjonsson/projects_demo/astro-actions-formdata/node_modules/.pnpm/[email protected][email protected]/node_modules/astro/dist/vite-plugin-astro-server/route.js:156:16)
    at async run (file:///Users/robinjonsson/projects_demo/astro-actions-formdata/node_modules/.pnpm/[email protected][email protected]/node_modules/astro/dist/vite-plugin-astro-server/request.js:40:14) {
  cause: TypeError: The body has already been consumed.
      at consumeBody (node:internal/deps/undici/undici:4575:19)
      at consumeBody.next (<anonymous>)
      at _Request.formData (node:internal/deps/undici/undici:4675:32)
      at eval (/Users/robinjonsson/projects_demo/astro-actions-formdata/src/pages/en/post.astro:14:41)
      at post (/Users/robinjonsson/projects_demo/astro-actions-formdata/node_modules/.pnpm/[email protected][email protected]/node_modules/astro/dist/runtime/server/astro-component.js:19:12)
      at callComponentAsTemplateResultOrResponse (file:///Users/robinjonsson/projects_demo/astro-actions-formdata/node_modules/.pnpm/[email protected][email protected]/node_modules/astro/dist/runtime/server/render/astro/render.js:90:31)
      at renderToAsyncIterable (file:///Users/robinjonsson/projects_demo/astro-actions-formdata/node_modules/.pnpm/[email protected][email protected]/node_modules/astro/dist/runtime/server/render/astro/render.js:132:32)
      at renderPage (file:///Users/robinjonsson/projects_demo/astro-actions-formdata/node_modules/.pnpm/[email protected][email protected]/node_modules/astro/dist/runtime/server/render/page.js:31:30)
      at lastNext (file:///Users/robinjonsson/projects_demo/astro-actions-formdata/node_modules/.pnpm/[email protected][email protected]/node_modules/astro/dist/core/render-context.js:135:31)
      at async callMiddleware (file:///Users/robinjonsson/projects_demo/astro-actions-formdata/node_modules/.pnpm/[email protected][email protected]/node_modules/astro/dist/core/middleware/callMiddleware.js:21:10)
}

What's the expected result?

The same result as when Astro actions are disabled.

Link to Minimal Reproducible Example

https://stackblitz.com/edit/withastro-astro-rdrsul

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Jul 15, 2024
@ematipico
Copy link
Member

Can you explain a bit better this use case? I need to understand if this is a real use case how you would use it. The actions feature enables a middleware that consumes the body of the Request and once it's consumed, it can't be read again.

@ematipico ematipico added the needs response Issue needs response from OP label Jul 18, 2024
@robinjonsson
Copy link
Author

I use rewrites and middleware for i18n support because the locales is not known at build time.
Partials for POST requests when you need to return HTML (like HTMX).
And the plan was to use actions for requests that don't need to return HTML.

Maybe it's possible to use the new Container API inside an action? 😄

@robinjonsson
Copy link
Author

I tested the Container API inside actions, and it works, that is cool. But it doesn't seem to be an option to render it as a partial?

@ematipico
Copy link
Member

But the example you provided doesn't use actions other than enabling the experimental flag, so I'm missing the point of the issue.

@robinjonsson
Copy link
Author

We can close this issue. I will use the Container API combined with API routes or actions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs response Issue needs response from OP needs triage Issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

2 participants