How to get domain from `process.env`
Unanswered
Yellow-horned horntail posted this in #help-forum
Yellow-horned horntailOP
Currently when you deploy Nextjs thorugh CLI or connect from github
For example :
Checkout : https://frames-se2.vercel.app console (I just logged
It logs :
What breaks having gated URL ?
When having metadata setup, we have logic something like this :
^ Now because vercel returns gated URL this breaks Open graph displaying images properly
Are their any better way to handle metadata then this ?
PS:
process.env.NEXT_PUBLIC_VERCEL_URL and process.env.VERCEL_URL both gives the gated URL 😦 instead of giving public URL For example :
Checkout : https://frames-se2.vercel.app console (I just logged
process.env.NEXT_PUBLIC_VERCEL_URL )It logs :
The Vercel URL is: frames-se2-avnvagd82-technophile04s-projects.vercel.app which is gated URL What breaks having gated URL ?
When having metadata setup, we have logic something like this :
const baseUrl = process.env.VERCEL_URL
? `https://${process.env.VERCEL_URL}`
: `http://localhost:${process.env.PORT || 3000}`;
const imageUrl = `${baseUrl}/thumbnail.jpg`;
const title = "Scaffold-ETH 2 App";
const titleTemplate = "%s | Scaffold-ETH 2";
const description = "Built with 🏗 Scaffold-ETH 2";
export const metadata: Metadata = {
metadataBase: new URL(baseUrl),
title: {
default: title,
template: titleTemplate,
},
description,
openGraph: {
title: {
default: title,
template: titleTemplate,
},
description,
images: [
{
url: imageUrl,
},
],
},
twitter: {
card: "summary_large_image",
images: [imageUrl],
title: {
default: title,
template: titleTemplate,
},
description,
},
};^ Now because vercel returns gated URL this breaks Open graph displaying images properly
Are their any better way to handle metadata then this ?
PS:
process.env.NEXT_PUBLIC_VERCEL_URL used return public URL around 1 or 2 months ago and recently it started logging gated URL8 Replies
if you want to get it from env variables, then create a new env var with the value of your main domain
@joulev just hardcode your main domain directly. `metadataBase: new URL("https://frames-se2.vercel.app"),`
Yellow-horned horntailOP
Yup that’s what we have been doing did while but it’s really PITA that every time you change domain you have to remember to update it
Also it’s very strange that vercel did this recently, previously it used to work nicely
Also it’s very strange that vercel did this recently, previously it used to work nicely
@Yellow-horned horntail Yup that’s what we have been doing did while but it’s really PITA that every time you change domain you have to remember to update it
Also it’s very strange that vercel did this recently, previously it used to work nicely
do you change your domain that often for this to be a pain?
i dont recall
i dont recall
NEXT_PUBLIC_VERCEL_URL producing the public domain. i have always used it for ages now to get the generated deployment URL, unique for every deployment. and i don't see how that env var could work for returning public domain since a project can have an unlimited number of production domains, not just one.for each deployment, there is only one
NEXT_PUBLIC_VERCEL_URL, only at most one NEXT_PUBLIC_VERCEL_BRANCH_URL, but can have any number of domains associated with itI wish there was a better way to atleast set your metadataBase, since thats mostly the only reason why i have a NEXT_PUBLIC_SITE_DOMAIN
it's intentionally designed to be this way, so that social media sites can cache your metadata information. if you change your metadata every deployment by using the auto-generated domain, caching is impossible – and as explained above since you can have more than one production domain, it is impossible to have an automated env var for production domains
Oh, I never thought it as that way, it does make sense now, thanks Joulev :D