[PR #8468] fix(next-js): support streaming request bodies in App Router #7996

Open
opened 2026-03-13 13:56:09 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/8468
Author: @yocontra
Created: 3/7/2026
Status: 🔄 Open

Base: canaryHead: fix/nextjs-streaming-body-duplex


📝 Commits (2)

  • 2398060 fix(next-js): wrap incoming request with duplex: 'half' for streaming body support
  • 8eb48f7 Update packages/better-auth/src/integrations/next-js.ts

📊 Changes

1 file changed (+27 additions, -1 deletions)

View changed files

📝 packages/better-auth/src/integrations/next-js.ts (+27 -1)

📄 Description

Summary

  • Adds a toPlainRequest helper in toNextJsHandler that re-creates the incoming Request with duplex: 'half'
  • Fixes an issue where Next.js App Router forwards a Request whose ReadableStream body becomes unreadable because the runtime loses the internal "disturbed" flag between layers
  • Without this fix, POST requests with bodies (e.g. sign-in, sign-up) fail silently or throw when better-auth tries to consume the body

Problem

When using toNextJsHandler with Next.js App Router route handlers, the Request object passed by Next.js may have a streaming body that cannot be consumed. This is because Next.js internally forwards the request between layers, and the duplex property is not preserved, causing the body stream to be treated as already consumed.

Solution

Before forwarding the request to the auth handler, wrap it in a new Request constructed with duplex: 'half'. This ensures the body stream is properly initialized and readable. Only requests with bodies (non-GET, non-HEAD) include the body and duplex option.

Test plan

  • Verify POST requests to auth endpoints (sign-in, sign-up, etc.) work correctly in Next.js App Router
  • Verify GET requests continue to work as before
  • Verify no regression in Pages Router usage

Summary by cubic

Fixes unreadable streaming request bodies in Next.js App Router by recreating the Request with duplex: 'half' only when a body is present. Restores POST auth flows (sign-in, sign-up) without affecting GET, HEAD, or Pages Router.

  • Bug Fixes
    • Added toPlainRequest to rebuild the Request and keep the body stream readable.
    • Only wraps when method is not GET/HEAD and request.body exists; preserves method, headers, and body.
    • Handles Next.js losing the internal “disturbed” flag when forwarding requests.

Written for commit 8eb48f747c3d864ac979f90791846352e7bcd48d. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/8468 **Author:** [@yocontra](https://github.com/yocontra) **Created:** 3/7/2026 **Status:** 🔄 Open **Base:** `canary` ← **Head:** `fix/nextjs-streaming-body-duplex` --- ### 📝 Commits (2) - [`2398060`](https://github.com/better-auth/better-auth/commit/239806069ae9c0059315526c3cf4ef4b6af573aa) fix(next-js): wrap incoming request with `duplex: 'half'` for streaming body support - [`8eb48f7`](https://github.com/better-auth/better-auth/commit/8eb48f747c3d864ac979f90791846352e7bcd48d) Update packages/better-auth/src/integrations/next-js.ts ### 📊 Changes **1 file changed** (+27 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/integrations/next-js.ts` (+27 -1) </details> ### 📄 Description ## Summary - Adds a `toPlainRequest` helper in `toNextJsHandler` that re-creates the incoming `Request` with `duplex: 'half'` - Fixes an issue where Next.js App Router forwards a `Request` whose `ReadableStream` body becomes unreadable because the runtime loses the internal "disturbed" flag between layers - Without this fix, POST requests with bodies (e.g. sign-in, sign-up) fail silently or throw when better-auth tries to consume the body ## Problem When using `toNextJsHandler` with Next.js App Router route handlers, the `Request` object passed by Next.js may have a streaming body that cannot be consumed. This is because Next.js internally forwards the request between layers, and the `duplex` property is not preserved, causing the body stream to be treated as already consumed. ## Solution Before forwarding the request to the auth handler, wrap it in a new `Request` constructed with `duplex: 'half'`. This ensures the body stream is properly initialized and readable. Only requests with bodies (non-GET, non-HEAD) include the body and duplex option. ## Test plan - [ ] Verify POST requests to auth endpoints (sign-in, sign-up, etc.) work correctly in Next.js App Router - [ ] Verify GET requests continue to work as before - [ ] Verify no regression in Pages Router usage <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Fixes unreadable streaming request bodies in Next.js App Router by recreating the Request with duplex: 'half' only when a body is present. Restores POST auth flows (sign-in, sign-up) without affecting GET, HEAD, or Pages Router. - **Bug Fixes** - Added toPlainRequest to rebuild the Request and keep the body stream readable. - Only wraps when method is not GET/HEAD and request.body exists; preserves method, headers, and body. - Handles Next.js losing the internal “disturbed” flag when forwarding requests. <sup>Written for commit 8eb48f747c3d864ac979f90791846352e7bcd48d. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 13:56:09 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#7996