Following the docs here  (Building for Relative Paths), so setting homepage field in package.json doesn't set PUBLIC_URL during the build.
%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">
...
%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">
...
Run these commands in the project folder and fill in their results:
npm ls react-scripts (if you haven鈥檛 ejected): 1.0.7node -v: 8.1.2npm -v: 5.0.3Then, specify:
create-react-app my-app (I used create-react-app 1.3.1)cd my-app"homepage": "https://example.com" to package.jsonyarn buildbuild/index.html doesn't prepend URL to the paths in the script tagBut 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.
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?
@glennreyes Also, is there a particular reason you want it to work like this instead of how it works now?
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.
@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?
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">
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
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">
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.
Most helpful comment
@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?