Skip to content

[BUG] middleware http request method dropped #201

@mattboardman

Description

@mattboardman

Describe the bug

When making a POST request with middleware (in this case I'm using @clerk/nextjs) the request method is not copied into the NextRequest which defaults the method to GET. This raises some sort of spec error:

[wrangler:err] TypeError: Request with a GET or HEAD method cannot have a body.
    at new CustomRequest (file:///home/mattboardman/goto/.open-next/server-functions/default/handler.mjs:28:9)
    at new nt (file:///home/mattboardman/goto/.next/server/src/webpack:/_N_E/node_modules/@clerk/backend/dist/chunk-PLL3MNZH.mjs:1865:1)
    at createClerkRequest (file:///home/mattboardman/goto/.next/server/src/webpack:/_N_E/node_modules/@clerk/backend/dist/chunk-PLL3MNZH.mjs:1908:1)
    at null.<anonymous> (file:///home/mattboardman/goto/.next/server/src/webpack:/_N_E/node_modules/@clerk/nextjs/dist/esm/server/clerkMiddleware.js:69:28)
    at async eY (file:///home/mattboardman/goto/.next/server/src/webpack:/_N_E/node_modules/next/dist/esm/server/web/adapter.js:160:1)
    at async Object.edgeFunctionHandler (file:///home/mattboardman/goto/node_modules/@opennextjs/aws/dist/core/edgeFunctionHandler.js:19:20)
    at async handleMiddleware (file:///home/mattboardman/goto/node_modules/@opennextjs/aws/dist/core/routing/middleware.js:45:20)
    at async routingHandler (file:///home/mattboardman/goto/node_modules/@opennextjs/aws/dist/core/routingHandler.js:63:27)
    at null.<anonymous> (async file:///home/mattboardman/goto/.wrangler/tmp/dev-7D2hCz/worker.js:164903:20)
    at null.<anonymous> (async file:///home/mattboardman/goto/.wrangler/tmp/dev-7D2hCz/worker.js:163561:20)

The bug can be corrected by explicitly adding the method field here:

    if (init) {
      init = {
        ...init,
        cache: undefined,
        // FIX
        method: init.method,
        // https://github.com/cloudflare/workerd/issues/2746
        // https://github.com/cloudflare/workerd/issues/3245
        body: init.body instanceof __cf_stream.Readable ? ReadableStream.from(init.body) : init.body,
      };

I don't understand why the method field isn't being copied over since the spread operator should do that already, but I debugged it manually and confirmed the method is undefined after this bit of code.

Steps to reproduce

  1. Using some middleware that intercepts requests (e.g. "@clerk/nextjs": "6.9.6")
// src/middleware.ts
import { clerkMiddleware } from '@clerk/nextjs/server'

export default clerkMiddleware(async (auth, request, event) => {}, {
    debug: true,
    publishableKey: process.env.NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY,
    secretKey: process.env.CLERK_SECRET_KEY,
});

export const config = {
  matcher: [
    // Skip Next.js internals and all static files, unless found in search params
    '/((?!_next|[^?]*\\.(?:html?|css|js(?!on)|jpe?g|webp|png|gif|svg|ttf|woff2?|ico|csv|docx?|xlsx?|zip|webmanifest)).*)',
    // Always run for API routes
    '/(api|trpc)(.*)',
    '/api/webhooks(.*)',
  ],
}
  1. Make a post request to any of the api routes.
curl -X POST -H "Content-Type: application/json" -d '{}' localhost:3000/api/ui/search 
  1. POST request fails with the error above.

Expected behavior

The http method should be defined and not default to GET.

@opennextjs/cloudflare version

0.3.0

Wrangler version

3.99.0

next info output

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Thu Dec 21 04:01:45 UTC 2023
  Available memory (MB): 64215
  Available CPU cores: 16
Binaries:
  Node: 18.19.0
  npm: 10.2.3
  Yarn: N/A
  pnpm: 9.15.0
Relevant Packages:
  next: 14.2.21 // An outdated version detected (latest is 15.1.2), upgrade is highly recommended!
  eslint-config-next: 14.2.5
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.7.2
Next.js Config:
  output: N/A

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions