0

I need to acces static contents (images) so I found the PUBLIC_URL variable is the way it's done in React.
Can the path "/public" be changed to something else?
I ask this because I added the PUBLIC_URL=https://yourdomain.whatever/public and all is fine, but I noticed that npm start open the browser in http://localhost:8000/public .
If I remove or comment that PUBLIC_URL variable it returns to start from http://localhost:8000 and images are available statically from /public.

So, why (and how) have I to define the PUBLI_URL variable?
Do I actually need it or it's fine to skip because already there by default?
Why the npm start open the http://localhost:8000/public url ?

Alex 75
  • 2,798
  • 1
  • 31
  • 48

1 Answers1

3

I don't think you require to access PUBLIC_URL directly and don't think it is good practice to change it. As you may know, that index.html is single page that react always renders, which is inside the public directory. So no surprise that when you compile code, it look for index.html in PUBLIC_URL and renders it. If you want to access certain asset and want to add its path in index.html directly, then you can use PUBLIC_URL to define asset path, otherwise to access asset in your react code, you can directly access it by providing path as: /path/to/asset/inside/public. For example, If I have image abc.jpeg inside img folder in public, then I can access it directly in my react code as:

<img src="/img/abc.jpeg" />
YATIN GUPTA
  • 916
  • 9
  • 17
  • Thanks. your example is clear and works fine. I'm wondering why the tutorial I followed (and also [here](https://stackoverflow.com/questions/62520904/react-public-url-env-variable) for example) makes use of ``process.env.PUBLIC_URL``. – Alex 75 May 16 '21 at 15:14