-
Hi, I am trying to setup SEO for site. It will need a full URL like : https://mysite.com/product/a-product |
Beta Was this translation helpful? Give feedback.
Replies: 19 comments 38 replies
-
Maybe use something like: More info here: https://vercel.com/docs/environment-variables |
Beta Was this translation helpful? Give feedback.
-
I created a repository to debug the env vars of the Next hosted on Vercel, and this is still a current problem... Note that |
Beta Was this translation helpful? Give feedback.
-
To anyone coming here to know how to have an env variable of NEXT_PUBLIC_URL containing:
Here's how to do it "the right way" as of September 2021. For development:
For preview URLs:
For production (yourdomain.com), create a new env variable on the Vercel dashboard, only for the production env: I wish this were easier, and we could define the main domain for a project exposed in a different env variable. Cc @leerob maybe for feedback. Thanks! Bonus: If you want to override the development defaults, use |
Beta Was this translation helpful? Give feedback.
-
currently leading to issues on auto-detecting the Vercel environment in NextAuth.js |
Beta Was this translation helpful? Give feedback.
-
Would this work? // in some util
export const getBaseUrl = () => {
if (process.env.NEXT_PUBLIC_VERCEL_ENV === "production")
return "https://your-production.url";
if (process.env.NEXT_PUBLIC_VERCEL_ENV === "preview")
return `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`;
return "http://localhost:3000";
}; |
Beta Was this translation helpful? Give feedback.
-
adding on the code-based solution: export function getBaseUrl() {
return process.env.NEXT_PUBLIC_SITE_URL ?? process.env.NEXT_PUBLIC_VERCEL_URL
}; Priority on environment variable, fallback to vercel's exposed url. |
Beta Was this translation helpful? Give feedback.
-
I been struging with the same thing, because My fix: Set env variable next.config.js
build-url.ts
usage
|
Beta Was this translation helpful? Give feedback.
-
In order to have the deployment URL at build-time, both locally in development mode, and when built locally, and in preview-mode pointing to the dynamic Vercel deployment, and pointing to the custom domain when deployed in production, I've resorted to use:
This is the simplest solution I've found. |
Beta Was this translation helpful? Give feedback.
-
Hey, we are using the following implementation: const VERCEL_URL = process.env.NEXT_PUBLIC_VERCEL_URL
? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`
: "";
const RAILWAY_STATIC_URL = process.env.RAILWAY_STATIC_URL
? `https://${process.env.RAILWAY_STATIC_URL}`
: "";
const HEROKU_URL = process.env.HEROKU_APP_NAME
? `https://${process.env.HEROKU_APP_NAME}.herokuapp.com`
: "";
const RENDER_URL = process.env.RENDER_EXTERNAL_URL
? `https://${process.env.RENDER_EXTERNAL_URL}`
: "";
export const WEBAPP_URL =
process.env.NEXT_PUBLIC_WEBAPP_URL ||
VERCEL_URL ||
RAILWAY_STATIC_URL ||
HEROKU_URL ||
RENDER_URL ||
"http://localhost:3000"; In the local environment is should fallback to |
Beta Was this translation helpful? Give feedback.
-
I still can't get the current URL (Custom Domain, Branch Link, Deployment Link). I don't want to hardcode the URL. |
Beta Was this translation helpful? Give feedback.
-
I also wanted to share my way of getting the base URL in my NextJS projects, because while some of the above solutions solve most of my requirements, there are some cases where it wasn't sufficient. My problemI have a production custom domain (
The main issue the other solutions don't solve for me are for 2 and 3. Since SolutionsThe following solutions can be used regardless of where you need the baseURL (client, server and even SSG). However, none of them account for multi-custom-domain deployments, which I'll explain below. Using
|
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Vercel should provide an example in their docs on how to achieve this for common use cases... meta tags, favicon, sitemaps, etc. |
Beta Was this translation helpful? Give feedback.
-
But how do I determine if its the VERCEL_URL or the VERCEL_BRANCH_URL? Anyone has an idea? |
Beta Was this translation helpful? Give feedback.
-
I came up with the following a while back for BooleanArt: export const BASE_URL =
process.env.NEXT_PUBLIC_VERCEL_ENV == null ||
process.env.NEXT_PUBLIC_VERCEL_ENV === "development"
? "http://localhost:3000"
: process.env.NEXT_PUBLIC_VERCEL_ENV === "preview"
? `https://${process.env.NEXT_PUBLIC_VERCEL_URL}`
: `https://${process.env.NEXT_PUBLIC_VERCEL_PROJECT_PRODUCTION_URL}`; Also takes into account: vercel/vercel#11633 |
Beta Was this translation helpful? Give feedback.
-
4 years later. I'll save you all the trouble. Just don't use vercel hosting
…On Wed, Apr 30, 2025, 7:59 PM Leonardo Faria ***@***.***> wrote:
Thanks for this, but I found a weird scenario:
Vercel has a bot that posts deploy preview comments in a GitHub PR: the
URL in that case is
https://REPONAME-git-BRANCHNAME-RANDOM-pistachiosoftware.vercel.app/
NEXT_PUBLIC_VERCEL_URL generates https://PROJECT-RANDOM-ORG.vercel.app/
—
Reply to this email directly, view it on GitHub
<#16429 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABXIJPV6NJJBWB5QBCQNP324FWWHAVCNFSM4QHDG4G2U5DIOJSWCZC7NNSXTOSENFZWG5LTONUW63SDN5WW2ZLOOQ5TCMRZHE4TSMZR>
.
You are receiving this because you commented.Message ID: <vercel/next.
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
if you're using Vercel: here's what I use. I simplified it a but, must be working. export function getDeploymentHostName(customDomainForProduction: boolean) {
const env = process.env.VERCEL_ENV ?? 'development';
// console.log('ENV: ', env);
let deploymentUrl;
if (env === 'development') {
deploymentUrl = 'localhost:3000'; // your local hostname and port
// if using webhooks proxy tunnels:
// deploymentUrl = process.env.NGROK_URL ?? 'localhost:3000';
} else if (env === 'production' && customDomainForProduction) {
deploymentUrl = process.env.VERCEL_PROJECT_PRODUCTION_URL ?? '';
} else {
deploymentUrl = process.env.VERCEL_URL ?? '';
}
// console.log('ENV: ', { env, deploymentUrl, vercel: process.env.VERCEL_URL });
if (deploymentUrl === '') {
throw new Error('Deployment URL couldn\'t be determined');
}
return deploymentUrl;
} Usually you will want a http(s) prefix, then pass the hostname to this function: export function buildUrlPath(urlBase: string, path: string): string {
const result = [urlBase, path].map((s) => trimSlash(s)).join('/');
if (urlBase.startsWith('/')) {
return `/${result}`;
} else if (urlBase.startsWith('http')) {
return result;
} else {
return `https://${result}`;
}
} |
Beta Was this translation helpful? Give feedback.
-
Have there been any solutions to this yet? |
Beta Was this translation helpful? Give feedback.
To anyone coming here to know how to have an env variable of NEXT_PUBLIC_URL containing:
Here's how to do it "the right way" as of September 2021.
For development:
For preview URLs:
For production (yourdomain.com), create a new env variable on the Vercel dashboard, only for the production env:
I wish this were easier, and we could define the main domain for a proj…