[GH-ISSUE #739] dev server: POST/PATCH API routes return 500 (fetch failed in miniflare dispatchFetch) #160

Closed
opened 2026-05-06 12:37:45 +02:00 by BreizhHardware · 2 comments

Originally created by @unitedcolorsofg on GitHub (Apr 1, 2026).
Original GitHub issue: https://github.com/cloudflare/vinext/issues/739

Summary

All POST and PATCH API routes return HTTP 500 in the vinext dev server. GET requests to the same routes work correctly. The error originates from miniflare's dispatchFetch inside @cloudflare/vite-plugin.

Production builds are unaffected -- this is dev server only.

Cross-posted to cloudflare/workers-sdk for the miniflare/vite-plugin side.

Environment

  • vinext: 0.0.38
  • vite: 7.3.1
  • @cloudflare/vite-plugin: 1.30.2
  • miniflare: 4.20260317.3
  • wrangler: 4.78.0
  • Node: v22+
  • OS: Linux (Debian 13)

Reproduction

Any API route that calls an auth session check (better-auth api.getSession()) on a POST or PATCH request crashes. The same route responds correctly to GET (405, no handler) and works fine in production builds.

Minimal example (app/api/notifications/mark-all-read/route.ts):

import { NextResponse } from 'next/server';
import { auth } from '@/auth';
import { markAllAsRead } from '@/lib/notifications';

export async function POST() {
  const session = await auth();
  if (!session?.user?.id) {
    return NextResponse.json({ success: false, error: 'Unauthorized' }, { status: 401 });
  }
  const count = await markAllAsRead(session.user.id);
  return NextResponse.json({ success: true, data: { markedCount: count } });
}
# Returns 500 (expected 401 for unauthenticated request)
curl -s -o /dev/null -w "%{http_code}" -X POST http://127.0.0.1:3000/api/notifications/mark-all-read

# Returns 405 (correct -- no GET handler)
curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:3000/api/notifications/mark-all-read

Error

Internal server error: fetch failed
  at Object.processResponse (undici/lib/web/fetch/index.js:237:16)
  at async fetch4 (miniflare/dist/src/index.js:57394:20)
  at async _Miniflare.dispatchFetch (miniflare/dist/src/index.js:87257:22)
  at async @cloudflare/vite-plugin/dist/index.mjs:1601:19

Investigation

  • 12 POST/PATCH routes are affected; all GET routes work
  • A freshly created debug route importing the same auth + db + service modules works for POST, suggesting possible module graph or HMR state issue with established routes
  • Removing unused request parameters from handlers: no effect
  • Adding 127.0.0.1 to better-auth trustedOrigins: no effect
  • Vite 8 was attempted but is blocked by separate issues (#540, #585)
  • The crash occurs inside @cloudflare/vite-plugin's request dispatch to miniflare/workerd, not in application code

Impact

All Playwright E2E tests targeting POST/PATCH endpoints fail (12 failures out of an otherwise passing suite).

Originally created by @unitedcolorsofg on GitHub (Apr 1, 2026). Original GitHub issue: https://github.com/cloudflare/vinext/issues/739 ## Summary All POST and PATCH API routes return HTTP 500 in the vinext dev server. GET requests to the same routes work correctly. The error originates from miniflare's `dispatchFetch` inside `@cloudflare/vite-plugin`. Production builds are unaffected -- this is dev server only. Cross-posted to cloudflare/workers-sdk for the miniflare/vite-plugin side. ## Environment - vinext: 0.0.38 - vite: 7.3.1 - @cloudflare/vite-plugin: 1.30.2 - miniflare: 4.20260317.3 - wrangler: 4.78.0 - Node: v22+ - OS: Linux (Debian 13) ## Reproduction Any API route that calls an auth session check (better-auth `api.getSession()`) on a POST or PATCH request crashes. The same route responds correctly to GET (405, no handler) and works fine in production builds. Minimal example (`app/api/notifications/mark-all-read/route.ts`): ```ts import { NextResponse } from 'next/server'; import { auth } from '@/auth'; import { markAllAsRead } from '@/lib/notifications'; export async function POST() { const session = await auth(); if (!session?.user?.id) { return NextResponse.json({ success: false, error: 'Unauthorized' }, { status: 401 }); } const count = await markAllAsRead(session.user.id); return NextResponse.json({ success: true, data: { markedCount: count } }); } ``` ```bash # Returns 500 (expected 401 for unauthenticated request) curl -s -o /dev/null -w "%{http_code}" -X POST http://127.0.0.1:3000/api/notifications/mark-all-read # Returns 405 (correct -- no GET handler) curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:3000/api/notifications/mark-all-read ``` ## Error ``` Internal server error: fetch failed at Object.processResponse (undici/lib/web/fetch/index.js:237:16) at async fetch4 (miniflare/dist/src/index.js:57394:20) at async _Miniflare.dispatchFetch (miniflare/dist/src/index.js:87257:22) at async @cloudflare/vite-plugin/dist/index.mjs:1601:19 ``` ## Investigation - 12 POST/PATCH routes are affected; all GET routes work - A freshly created debug route importing the same `auth` + `db` + service modules works for POST, suggesting possible module graph or HMR state issue with established routes - Removing unused `request` parameters from handlers: no effect - Adding `127.0.0.1` to better-auth `trustedOrigins`: no effect - Vite 8 was attempted but is blocked by separate issues (#540, #585) - The crash occurs inside `@cloudflare/vite-plugin`'s request dispatch to miniflare/workerd, not in application code ## Impact All Playwright E2E tests targeting POST/PATCH endpoints fail (12 failures out of an otherwise passing suite).
Author
Owner

@unitedcolorsofg commented on GitHub (Apr 1, 2026):

Cross-posted to cloudflare/workers-sdk: https://github.com/cloudflare/workers-sdk/issues/13189

<!-- gh-comment-id:4169701873 --> @unitedcolorsofg commented on GitHub (Apr 1, 2026): Cross-posted to cloudflare/workers-sdk: https://github.com/cloudflare/workers-sdk/issues/13189
Author
Owner

@unitedcolorsofg commented on GitHub (Apr 1, 2026):

Duplicate

<!-- gh-comment-id:4170932442 --> @unitedcolorsofg commented on GitHub (Apr 1, 2026): Duplicate
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/vinext#160
No description provided.