Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access deployed url #1054

Closed
ericmasiello opened this issue Dec 28, 2017 · 16 comments
Closed

Access deployed url #1054

ericmasiello opened this issue Dec 28, 2017 · 16 comments

Comments

@ericmasiello
Copy link

Hi, is it possible to access to the url (host and port) that’s created when deploying an app to now somehow within the app such as through process.env?

@timneutkens
Copy link
Member

The url is provided as process.env.NOW_URL
Alternatively if you want to get the alias you can read it from the HOST header.
https://zeit.co/docs/features/env-and-secrets#default-variables

@cryptiklemur
Copy link

Is this in V2 at all? I can't find any reference too it, and it doesn't seem to be in the env vars

@juliangruber juliangruber reopened this Mar 25, 2019
@juliangruber juliangruber added triaged: bug Issue or PR has been reviewed by a maintainer and work is being tracked awaiting team response labels Mar 25, 2019
@juliangruber
Copy link
Contributor

@aequasi you're right that this isn't in V2. Can you make a case for adding it?

@juliangruber juliangruber added awaiting response triaged: feature request Issue or PR has been reviewed by a maintainer and work is being tracked and removed awaiting team response triaged: bug Issue or PR has been reviewed by a maintainer and work is being tracked labels Mar 25, 2019
@cryptiklemur
Copy link

One of my apps has an OAuth flow to it. The callbackUrl is hard-coded to my production alias.

Would love for this to support my staging aliases, and possibly multiple aliases per deploy.
I'm sure I could get it set up to use the URL from the request some how, but it'd be a lot easier to just have an env var for it

@styfle
Copy link
Member

styfle commented Mar 25, 2019

You can use one of these to find the requested URL

  • Serverless Function: req.headers.host
  • Browser: window.location.hostname

In your case (OAuth), I would suggested reading the host header for the callbackUrl.

@juliangruber juliangruber added awaiting response and removed awaiting team response triaged: feature request Issue or PR has been reviewed by a maintainer and work is being tracked labels Mar 25, 2019
@JJAshcraft
Copy link

I was able to get mine by using (example)
let URL = req.headers['x-now-deployment-url']
note: this does not give you a full url, you still need to prepend the https://

@juliangruber
Copy link
Contributor

@borekb
Copy link

borekb commented Aug 18, 2019

This is documented here: https://zeit.co/docs/v2/platform/upgrade-to-2-0#environment-variables

Up-to-date URL: https://zeit.co/docs/v2/advanced/platform/changes-in-now-2-0#environment-variables

(@juliangruber Feel free to delete this comment if you want to update your link.)

@compex-africa
Copy link

This is documented here: https://zeit.co/docs/v2/platform/upgrade-to-2-0#environment-variables

Up-to-date URL: https://zeit.co/docs/v2/advanced/platform/changes-in-now-2-0#environment-variables

(@juliangruber Feel free to delete this comment if you want to update your link.)

Url is now out of date

@dcastil
Copy link

dcastil commented Jan 18, 2020

Is there a way to get the deploy URL during build time at the moment?

If I want to enable a twitter card with an image for a website, I need to add a meta tag in the head with a full image URL.

<meta name="twitter:image" content="https://mysite.com/image.jpg">

Not having a deploy URL at hand makes it pretty hard to test this functionality in a deploy preview. On Netlify I can access the deploy URL via process.env.DEPLOY_PRIME_URL and I'm searching for a way to replicate this on now.

@hacknug
Copy link

hacknug commented Feb 9, 2020

I was also looking for this. The way I had it set up in Gridsome + Netlify was: siteUrl: process.env.BRANCH === 'master' ? process.env.URL : process.env.DEPLOY_URL, and it was quite useful to test and debug changes on branch and PR deploys. Would love to see something similar available on Now.

@alondahari
Copy link

I'm also looking for this. We are using jenkins for our CI and deploying to now to run end to end tests against it. Would love it if the url could be accessed from stdout.

@lucleray
Copy link
Member

lucleray commented Apr 28, 2020

You can get the URL both in Build/Runtime and in your CI/CD:

  • In Build or Runtime, use the VERCEL_URL System Environment Variable in your project and it will be available in your code (for example, in Node.js, process.env.VERCEL_URL).
  • In a CI/CD environment, when running vercel, stdout contains the deployment URL. For example, try vercel --confirm > my_url.txt and the URL will be written to a file.
  • Lastly, we also add a host header to the request when it is routed to your deployment. You can read it to know what url (domain or deployment url) is accessed by the visitor of your website.

@revskill10
Copy link

@lucleray In my case, process.env.VERCEL_URL is hpu-4tvpnc2z7.vercel.app while the production domain is at hpu.vercel.app. Do you know how to get the production url ?

@gunar
Copy link

gunar commented Nov 21, 2020

If you're going to use system environment variables such as VERCEL_URL, you'll need to enable Automatically expose System Environment Variables in your Project Settings.

image

@ChocolateLoverRaj
Copy link

VERCEL_URL is less reliable than host because VERCEL_URL is the 'inspect' url, which is wrong when you are on 'preview' url.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests