Type error for: process.env.VERCEL_URL
Answered
Ladder-backed Woodpecker posted this in #help-forum
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Ladder-backed WoodpeckerOP
I am getting this error message on one of my routes:
It is saying that the base URL in my middleware is invalid:
Is the VERCEL_URL environment variable something I need to set up myself in the Vercel project settings similar to database keys? I thought this was automatically done?
Appreciate any help in fixing this one.
TypeError: Invalid base URL string.
at (middleware.js:16:6)
at (node_modules/next/dist/esm/server/web/adapter.js:152:19)
It is saying that the base URL in my middleware is invalid:
if (session == null && req.nextUrl.pathname !== "/") {
return NextResponse.redirect(
new URL("/", process.env.VERCEL_URL || "http://localhost:3000")
);
}
Is the VERCEL_URL environment variable something I need to set up myself in the Vercel project settings similar to database keys? I thought this was automatically done?
Appreciate any help in fixing this one.
Answered by fuma 💙 joulev
VERCEL_URL
doesn’t contain the protocol schema, it should be https://${process.env.VERCEL_URL}
instead.Btw, in middleware. we usually use
new URL(“/“, req.url)
instead of getting the absolute path8 Replies
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Ladder-backed WoodpeckerOP
This is what I am getting
![Image](https://cdn.discordapp.com/attachments/1141395825067561031/1141396313037082814/Screenshot_2023-08-16_at_11.41.17_AM.png)
![Avatar](https://cdn.discordapp.com/avatars/572329183334891520/3ea2e2e134a2327e2b8a7738876a17b5.webp?size=256)
fuma 💙 joulev
VERCEL_URL
doesn’t contain the protocol schema, it should be https://${process.env.VERCEL_URL}
instead.Btw, in middleware. we usually use
new URL(“/“, req.url)
instead of getting the absolute pathAnswer
![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Ladder-backed WoodpeckerOP
Ah I see. Thanks for that. (I am a new dev).
In your middleware suggestion, would the req.url be the base URL? For example, if someone going to
www.example.com/authgatedstuff
needs to be redirected to www.example.com/
. Would the req.url here be just www.example.com
?![Avatar](https://cdn.discordapp.com/avatars/572329183334891520/3ea2e2e134a2327e2b8a7738876a17b5.webp?size=256)
fuma 💙 joulev
Yes, the second parameter of
new URL
is base url![Avatar](https://cdn.discordapp.com/embed/avatars/1.png)
Ladder-backed WoodpeckerOP
I see thank you!
Btw, off topic, I was looking at your profile and saw that you are in HK. I am from HK too! (But don't live there now)
![Avatar](https://cdn.discordapp.com/avatars/572329183334891520/3ea2e2e134a2327e2b8a7738876a17b5.webp?size=256)
fuma 💙 joulev
#off-topic