question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Specify homepage based on environment?

See original GitHub issue

Is this a bug report?

No.

Hi, all. I’m wondering what’s the reason to have homepage be specified as the top-level parameter of package.json rather than allow it to be set with environment variable through a command line or .env.* files? My specific usecase is this:

I have two environments - staging and prod - serving two versions of my app with a following url scheme: proxy.myhost.com/my-app-staging and proxy.myhost.com/my-app-prod (the proxy machine aggregates a lot of apps and having a subdomain for each is not feasible). I build them by invoking either yarn build-staging or yarn build-prod, but the problem is that there is no way to specify homepage based on which script I am running. I was imagining defining "build-staging": "HOMEPAGE=/my-app-staging <rest of the build command>", but it doesn’t seem to be possible right now?

I’m also using react-router, so setting "homepage": "." is not an option for me. (it results in client trying to request proxy.myhost.com/my-app-staging/some-dynamic-route/static/css/main.css upon navigating to a deep link).

Right now I’m just awk’ing the package.json on every build which I hardly can call a solution.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

1reaction
theZiegercommented, Oct 24, 2018

I guess @miraage was trying to give you the hint to do something like this?

"scripts": {
    "build": "react-scripts build",
    "build_staging": "PUBLIC_URL=https://proxy.myhost.com/my-app-staging/ react-scripts build",
  },
1reaction
spolakhcommented, Jun 20, 2018

@miraage thanks for chiming in! I think I did not specify the exact problem clear: when CRA builds index.html it appends the paths to js and css bundles based on homepage (which PUBLIC_URL is sourced from), right?

So here are the options that I currently have (homepage -> resulting path to js bundle when navigating directly to proxy.myhost.com/my-app-staging/some-dynamic-path):

  1. Don’t set homepage at all (in effect the same as set it to /) -> proxy.myhost.com/static/js/main.js - fails because proxy doesn’t know which app to request main.js from
  2. . -> proxy.myhost.com/my-app-staging/some-dynamic-path/static/js/main.js - fails because it’s an invalid nav path
  3. proxy.myhost.com/my-app-staging -> proxy.myhost.com/my-app-staging/static/js/main.js - works, but then there is no way to build prod version of the app from the same json config (it will point to staging static resources).

Unless I’m not seeing what you are proposing?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting homepage as a variable in package.json and make it ...
I have A and B project, both projects have individual roots for Production Build, but they share a same root for Test Build....
Read more >
Using environment variables in a React application
In this article we'll see how to use environment variables in a React application. Specifically, we'll look at two kinds of environments variables:...
Read more >
Home – Climate Change: Vital Signs of the Planet
Vital Signs of the Planet: Global Climate Change and Global Warming. Current news and data streams about global warming and climate change from...
Read more >
Climate Change | US EPA
Learn more about the objectives of the EPA Climate Change website. EPA is committed to advancing the goals of environmental justice for all...
Read more >
Deployment | Create React App
To override this, specify the homepage in your package.json , for example ... PR's are built automatically for staging environment previews.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found