#Type error for: process.env.VERCEL_URL

10 messages · Page 1 of 1 (latest)

rare totem
#

I am getting this error message on one of my routes: 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:

    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.
uneven crowBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

rare totem
#

This is what I am getting

restive wren
#

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 path

rare totem
#

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?

restive wren
#

Yes, the second parameter of new URL is base url

rare totem
#

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)

restive wren
#

#off-topic