Pages Deployment URL in process

Hey there, I’m trying to get the URL for my preview & production deployments. In the docs it says there’s a CF_PAGES_URL env var but I can’t access it via process.env.CF_PAGES_URL.

My project is a standard CRA.

Thanks for any ideas

I’ve also tried providing the variable to the build command as REACT_APP_SITE_URL=$CF_PAGES_URL npm run build but to no avail

I know that variable is in there… it doesn’t give you the aliased URL, though. I think it will continue to show the <id>.<project>.pages.dev format.

I can confirm those variables are there, though. I use the others easily.

1 Like

The current URL where the deployment is deployed would be more than enough. But I can’t seem to get it from anywhere.

Each tool requires different ways to access env variables. They are normal variables…

I know they exist and can be used, the build image is the same, so unsure how to help you there :frowning:

1 Like

CRA allows injecting envs through specifying them with a prefix: Adding Custom Environment Variables | Create React App

So you could do something like REACT_APP_PAGES_URL=$CF_PAGES_URL npm run build to have it accessible at process.env.REACT_APP_PAGES_URL

1 Like

Yeah, reading those docs seems like they just ignore all non REACT_APP_ env variables.

Yep, that’s exactly what I did. My build command was

REACT_APP_SITE_URL=$CF_PAGES_URL npm run build

. But still, I can’t get the value of CF_PAGES_URL.

Funnily when my build command is

REACT_APP_SITE_URL=$CF_PAGES_URL REACT_APP_SHA=$CF_PAGES_COMMIT_SHA npm run build

I can get the value of CF_PAGES_COMMIT_SHA

It’s working for me

1 Like

Seems to work fine with Vite, create-react-app is not working. Thanks for the help, I’ll head in the direction of create-react-app debugging

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.