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

Setting homepage in package.json doesn't set PUBLIC_URL on build #2575

Closed
glennreyes opened this issue Jun 20, 2017 · 5 comments
Closed

Setting homepage in package.json doesn't set PUBLIC_URL on build #2575

glennreyes opened this issue Jun 20, 2017 · 5 comments

Comments

@glennreyes
Copy link
Contributor

glennreyes commented Jun 20, 2017

Description

Following the docs here (Building for Relative Paths), so setting homepage field in package.json doesn't set PUBLIC_URL during the build.

Expected behavior

%PUBLIC_URL% in build/index.html should be replaced with URL set in package.json's homepage field.

package.json:

{
  ...
  "homepage": "https://example.com",
  ...
}

build/index.html:

...
<script type="text/javascript" src="https://example.com/static/js/main.ec7f8972.js">
...

Actual behavior

%PUBLIC_URL% will be replaced with nothing.

package.json:

{
  ...
  "homepage": "https://example.com",
  ...
}

build/index.html:

...
<script type="text/javascript" src="/static/js/main.ec7f8972.js">
...

Environment

Run these commands in the project folder and fill in their results:

  1. npm ls react-scripts (if you haven’t ejected): 1.0.7
  2. node -v: 8.1.2
  3. npm -v: 5.0.3

Then, specify:

  1. Operating system: macOS 10.12.5
  2. Browser and version: Not relevant

Reproducible Demo

  1. create-react-app my-app (I used create-react-app 1.3.1)
  2. cd my-app
  3. Add "homepage": "https://example.com" to package.json
  4. yarn build
  5. build/index.html doesn't prepend URL to the paths in the script tag

But setting PUBLIC_URL=https://example.com in .env and build will produce the expected behaviour (adds URL to the script tag). Setting homepage in package.json should do the same.

@Timer
Copy link
Contributor

Timer commented Jun 20, 2017

This is functioning correctly. Only the path of the URL is interpolated into %PUBLIC_URL%, unless if you explicitly use the PUBLIC_URL environment variable.

Would you like to send an update to the docs to clarify this?

@heyimalex
Copy link
Contributor

@glennreyes Also, is there a particular reason you want it to work like this instead of how it works now?

@glennreyes
Copy link
Contributor Author

Only the path of the URL is interpolated into %PUBLIC_URL%, unless if you explicitly use the PUBLIC_URL environment variable

Thanks! Makes sense to just inject the path. It's also already very clear in the docs.

@heyimalex No, I actually prefer how it is right now.

That said, I will close this issue.

@cbravo
Copy link

cbravo commented Feb 27, 2018

@Timer if this is the correct use of public_url what is the correct way of setting full URL for the meta tags that require it such as og:image?

ruupert added a commit to UniversityOfHelsinkiCS/labtool that referenced this issue Mar 10, 2018
@vaibhavhrt
Copy link

vaibhavhrt commented Jan 8, 2019

People like me who are looking for something like this in in build:

<script type="text/javascript" src="https://dsomething.cloudfront.net/static/js/main.ec7f8972.js">

1. Quick Solution

Build your project like this:
(windows)

set PUBLIC_URL=https://dsomething.cloudfront.net&&npm run build

(linux)

PUBLIC_URL=https://dsomething.cloudfront.net npm run build

(mac)
-- not sure --

And you will get

<script type="text/javascript" src="https://dsomething.cloudfront.net/static/js/main.ec7f8972.js">

in your built index.html

2. Permanent Solution

Create a file called .env at your project root(same place where package.json is located).
In this file write this(no quotes around the url):

PUBLIC_URL=https://dsomething.cloudfront.net

Build your project as usual (npm run build)
This will also generate index.html with:

<script type="text/javascript" src="https://dsomething.cloudfront.net/static/js/main.ec7f8972.js">

3. Weird Solution

Add this in your package.json

"homepage": "http://://dsomething.cloudfront.net",

Then index.html will be generated with:

<script type="text/javascript" src="//dsomething.cloudfront.net/static/js/main.ec7f8972.js">

Which is basically the same as:

<script type="text/javascript" src="https://dsomething.cloudfront.net/static/js/main.ec7f8972.js">

in my understanding.

@lock lock bot locked and limited conversation to collaborators Jan 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants