question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Edge function / middleware error: "TypeError: Unable to parse URL" when deployed to Vercel

See original GitHub issue

What version of Next.js are you using?

12.0.3

What version of Node.js are you using?

v16.10.0 (local)

What browser are you using?

Chrome

What operating system are you using?

macOS

How are you deploying your application?

Vercel

Describe the Bug

When testing locally, the following _middleware.ts works perfectly:

export async function middleware(req: NextRequest) {
  const { pathname: redirectUrl } = req.nextUrl;
  const {  userTypeId } = req.cookies;

  if (!userTypeId || Number(userTypeId) !== UserTypeEnum.FIRST_TIME) {
    return Response.redirect("/play");
  }
  return NextResponse.next();
}

However, when deployed to Vercel I get the following error in my function logs:

TypeError: Unable to parse URL: /play
at Object.d [as handler]
at t.V
at Object.a

Expected Behavior

I would expect the page to redirect or continue without the error occuring.

To Reproduce

Create _middleware like above and deploy to vercel and try hit a route that the middleware is linked to

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
javivelascocommented, Nov 30, 2021

@adrianmiles The problem here is that the implementation of Response for Next.js is allowing you to provide a relative URL as a redirection target, while in Vercel production this is not allowed. In this context, we must wonder what’s the hostname for the redirection.

Typically one would expect the hostname to be the same where the request is being performed against, and this is information that you as a user have but the Response does not have on its prototype. For this reason, my recommendation would be that you change the pathname in the incoming request or you specify the host retrieving such information from the request.

What doesn’t make sense for sure is that this works locally but does not work in production for Vercel. I think the best way to approach this is to make not possible for the Response.redirect method to be called with a relative URL.

0reactions
github-actions[bot]commented, Feb 18, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Edge Middleware Quickstart – Vercel Docs
To test your middleware, use the Vercel CLI to deploy your project with vercel deploy . Once deployed, open the production url, and...
Read more >
0 - Stack Overflow
"TypeError: Failed to parse URL from (URL).vercel.app/api/getMessages" when building Next.js 13 with TypeScript on Vercel.
Read more >
API Routes: Request Helpers - Next.js
API Routes provide built-in request helpers which parse the incoming request ( req ):. req.cookies - An object containing the cookies sent by...
Read more >
How to Build a Full Stack NFT Marketplace - V2 (2022)
You must provide RPC url of the testnet where you want to deploy NFT ... I am getting this Error after createItem function...
Read more >
Using Next.js' middleware and Edge Functions - LogRocket Blog
Middleware is a Next.js feature that solves basic problems like authentication and geolocation with the help of Vercel Edge Functions.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found