Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
chanceaclark opened this issue Jun 13, 2024 · 1 comment · Fixed by #67148
Closed

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

chanceaclark opened this issue Jun 13, 2024 · 1 comment · Fixed by #67148
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.

Comments

@chanceaclark
Copy link
Contributor

chanceaclark commented Jun 13, 2024

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.

@chanceaclark chanceaclark added the bug Issue was opened via the bug report template. label Jun 13, 2024
@github-actions github-actions bot added Middleware Related to Next.js Middleware. Runtime Related to Node.js or Edge Runtime with Next.js. labels Jun 13, 2024
@huozhi huozhi closed this as completed in 7f72971 Jun 24, 2024
Copy link
Contributor

github-actions bot commented Jul 9, 2024

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.

@github-actions github-actions bot added the locked label Jul 9, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 9, 2024
huozhi added a commit that referenced this issue Jul 9, 2024
### What

Add isEdgeRendering check condition for rewrite logic in edge adpator 

Fixes #66837 
Closes NEXT-3545

### Why

From headers `x-middleware-rewrite`, it's still relative url `/rewrite`,
reconstructing the url will lead to crash as it doesn't contain host.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
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.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant