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

Open
opened 2026-04-15 22:37:11 -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: mainHead: fix/nextjs-streaming-body-duplex


📝 Commits (2)

  • 26494d9 fix(next-js): wrap incoming request with duplex: 'half' for streaming body support
  • cf3363c 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 with duplex: 'half' so streaming bodies stay readable.
    • Only wraps non-GET/HEAD requests that have a body; preserves method, headers, and body.

Written for commit cf3363c907. 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:** `main` ← **Head:** `fix/nextjs-streaming-body-duplex` --- ### 📝 Commits (2) - [`26494d9`](https://github.com/better-auth/better-auth/commit/26494d9c6ed309a00dab9ef439b628fed168e15f) fix(next-js): wrap incoming request with `duplex: 'half'` for streaming body support - [`cf3363c`](https://github.com/better-auth/better-auth/commit/cf3363c907881d63cc2144296a0065d012584c16) 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 with duplex: 'half' so streaming bodies stay readable. - Only wraps non-GET/HEAD requests that have a body; preserves method, headers, and body. <sup>Written for commit cf3363c907881d63cc2144296a0065d012584c16. 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-04-15 22:37:11 -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#24896