Invalid URL in middleware rewrite from edge runtime server action #66837
Labels
bug
Issue was opened via the bug report template.
locked
Middleware
Related to Next.js Middleware.
Runtime
Related to Node.js or Edge Runtime with Next.js.
Link to the code that reproduces this issue
https://github.com/chanceaclark/next-server-actions-redirect
To Reproduce
pnpm run dev
/server-action/edge
⨯ 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
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". Installing14.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 ax-middleware-subrequest
header appended in Edge Runtime only, pointing to a file instead of a URLapp/server-action/edge/page
.Middleware runs on each request in this order:
POST http://localhost:3000/server-action/edge
GET http://localhost:3000/redirect
Error [TypeError]: Invalid URL
(Assuming this is following thex-middleware-subrequest
)I could potentially see the
NextResponse.rewrite(...)
on the first middleware request (POST http://localhost:3000/server-action/edge
) triggering thatx-middleware-subrequest
, but would need some guidance here.The text was updated successfully, but these errors were encountered: