Edge function / middleware error: "TypeError: Unable to parse URL" when deployed to Vercel
See original GitHub issueWhat 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:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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.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.