Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React homepage attribute not working

In my package.json file I have "homepage" : "http://myname.project.com/myapp",

I build the app using npm run build and the output confirms that a /build directory was created. Output:

102.97 KB build/static/js/main.bfa46b52.js

24.37 KB build/static/css/main.cfe8a47e.css

The project was built assuming it is hosted at /myapp/. You can control this with the homepage field in your package.json.

The build folder is ready to be deployed.

If I start the app using npm start or serve -s build the app will start and the index.html page will load, but the references to static resources from index.html are broken (/myapp/static/js/main.js and /myapp/static/css/main.css). I can navigate directly to to them without the /myapp (localhost:5000/static/js/main.js works; localhost:5000/myapp/static/js/main.js does not work)

If I deploy the build folder to a different plain old apache server under a /myapp directory it seems to work as expected. Is there any way I can configure serve -s build to serve up pages from a /myapp directory?

The app is currently one page with no links and no implementation of react-router or anything equivalent. The team had been using webpack, but removed it. The app gets bundled using react-scripts.

like image 643
user1521567 Avatar asked Aug 16 '17 02:08

user1521567


People also ask

Where is Public_url in React?

json or the PUBLIC_URL environment variable that must be set before building the project. When running the react-scripts build script, the %PUBLIC_URL% placeholders inside the index. html file are replaced with the environment variable string.

How do I use image src in React?

Option 1: import the image into the component Put the image file somewhere under the src folder. This alone will not automatically make it available, so you have to import the image into the React component where you're using it. import companyLogo from './path/to/logo.


1 Answers

It wasn't clear to me that two things were happening.

  1. Setting the homepage attribute made modifications to index.html so that it would behave properly when deployed to /myapp directory.
  2. Running npm start or serve -s build/ still deployed my application from the root directory of the built in static server.

The homepage attribute doesn't have anything to do with where your app actually gets deployed. To address this issue I used a postbuild script move the contents of my /build folder to /build/myapp folder. Then it works as expected.

So more discussion about this here: https://github.com/facebookincubator/create-react-app/issues/1354#issuecomment-311506599

like image 178
user1521567 Avatar answered Oct 08 '22 11:10

user1521567