vercel / next.js

The React Framework
https://nextjs.org
MIT License
125.09k stars 26.72k forks source link

Invalid URL in middleware rewrite from edge runtime server action #66837

Closed chanceaclark closed 2 months ago

chanceaclark commented 3 months ago

Link to the code that reproduces this issue

https://github.com/chanceaclark/next-server-actions-redirect

To Reproduce

  1. pnpm run dev
  2. Navigate to /server-action/edge
  3. Submit the form
  4. Console should display this error:
⨯ Error [TypeError]: Invalid URL
    at new NodeError (node:internal/errors:405:5)
    at new URL (node:internal/url:676:13)
    at parseURL (webpack-internal:///(ssr)/./node_modules/.pnpm/next@14.1.1-canary.17_react-dom@18.3.1_react@18.3.1/node_modules/next/dist/esm/server/web/next-url.js:15:12)
    at new NextURL (webpack-internal:///(ssr)/./node_modules/.pnpm/next@14.1.1-canary.17_react-dom@18.3.1_react@18.3.1/node_modules/next/dist/esm/server/web/next-url.js:29:18)
    at adapter (webpack-internal:///(ssr)/./node_modules/.pnpm/next@14.1.1-canary.17_react-dom@18.3.1_react@18.3.1/node_modules/next/dist/esm/server/web/adapter.js:211:28)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async runWithTaggedErrors (file:///Users/chancellor.clark/repositories/next-server-actions-redirect/node_modules/.pnpm/next@14.1.1-canary.17_react-dom@18.3.1_react@18.3.1/node_modules/next/dist/server/web/sandbox/sandbox.js:99:24)
    at async DevServer.runEdgeFunction (file:///Users/chancellor.clark/repositories/next-server-actions-redirect/node_modules/.pnpm/next@14.1.1-canary.17_react-dom@18.3.1_react@18.3.1/node_modules/next/dist/server/next-server.js:1180:24)
    at async NextNodeServer.handleCatchallRenderRequest (file:///Users/chancellor.clark/repositories/next-server-actions-redirect/node_modules/.pnpm/next@14.1.1-canary.17_react-dom@18.3.1_react@18.3.1/node_modules/next/dist/server/next-server.js:247:37)
    at async DevServer.handleRequestImpl (file:///Users/chancellor.clark/repositories/next-server-actions-redirect/node_modules/.pnpm/next@14.1.1-canary.17_react-dom@18.3.1_react@18.3.1/node_modules/next/dist/server/base-server.js:807:17)
    at async (file:///Users/chancellor.clark/repositories/next-server-actions-redirect/node_modules/.pnpm/next@14.1.1-canary.17_react-dom@18.3.1_react@18.3.1/node_modules/next/dist/server/dev/next-dev-server.js:331:20)
    at async Span.traceAsyncFn (file:///Users/chancellor.clark/repositories/next-server-actions-redirect/node_modules/.pnpm/next@14.1.1-canary.17_react-dom@18.3.1_react@18.3.1/node_modules/next/dist/trace/trace.js:151:20)
    at async DevServer.handleRequest (file:///Users/chancellor.clark/repositories/next-server-actions-redirect/node_modules/.pnpm/next@14.1.1-canary.17_react-dom@18.3.1_react@18.3.1/node_modules/next/dist/server/dev/next-dev-server.js:328:24)
    at async invokeRender (file:///Users/chancellor.clark/repositories/next-server-actions-redirect/node_modules/.pnpm/next@14.1.1-canary.17_react-dom@18.3.1_react@18.3.1/node_modules/next/dist/server/lib/router-server.js:163:21)
    at async handleRequest (file:///Users/chancellor.clark/repositories/next-server-actions-redirect/node_modules/.pnpm/next@14.1.1-canary.17_react-dom@18.3.1_react@18.3.1/node_modules/next/dist/server/lib/router-server.js:342:24)
    at async requestHandlerImpl (file:///Users/chancellor.clark/repositories/next-server-actions-redirect/node_modules/.pnpm/next@14.1.1-canary.17_react-dom@18.3.1_react@18.3.1/node_modules/next/dist/server/lib/router-server.js:366:13)
    at async Server.requestListener (file:///Users/chancellor.clark/repositories/next-server-actions-redirect/node_modules/.pnpm/next@14.1.1-canary.17_react-dom@18.3.1_react@18.3.1/node_modules/next/dist/server/lib/start-server.js:140:13)
 ○ Compiling /_error ...
 ✓ Compiled /_error in 529ms (620 modules)

Current vs. Expected behavior

Current

Redirect + rewrite in edge runtime server action causes a request to an invalid URL.

Expected

Redirect + rewrite in either edge or nodejs runtime should not cause an error to an invalid URL.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.5.0: Wed May  1 20:14:38 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6020
Binaries:
  Node: 18.19.1
  npm: 10.2.4
  Yarn: 1.22.19
  pnpm: 8.15.6
Relevant Packages:
  next: 14.1.1-canary.17 // There is a newer canary version (15.0.0-canary.28) available, please upgrade! 
  eslint-config-next: 14.2.4
  react: 18.3.1
  react-dom: 18.3.1
  typescript: 5.4.5
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Middleware, Runtime

Which stage(s) are affected? (Select all that apply)

next dev (local), next build (local), next start (local), Vercel (Deployed)

Additional context

I ran a git bisect an nailed it down to #60798 that introduced the "issue". Installing 14.1.1-canary.16 demonstrates that the "issue" in not present in that version. I say "issue" in quotes as that change seems like it is valid behavior, although I am unsure how underlying function calls behave with this change.

One thing I did notice is that the middleware request to GET http://localhost:3000/redirect contains a x-middleware-subrequest header appended in Edge Runtime only, pointing to a file instead of a URL app/server-action/edge/page.

Middleware runs on each request in this order:

  1. POST http://localhost:3000/server-action/edge
  2. GET http://localhost:3000/redirect
  3. Error [TypeError]: Invalid URL (Assuming this is following the x-middleware-subrequest)

I could potentially see the NextResponse.rewrite(...) on the first middleware request (POST http://localhost:3000/server-action/edge) triggering that x-middleware-subrequest, but would need some guidance here.

github-actions[bot] commented 2 months ago

This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.