I'm deploying an app on a host that has the following setup:
I need to deploy under a custom root path /app for my React app that will sit under this umbrella. I'm using react-router v5 and create-react-app.
Problem
When I build the app (I'm using vercel's serve), I get a blank page. When I go to localhost:5000/app/, nothing shows up.
I did all the suggestions from here and here, but still can't get my app to load.
I'm also confused: what's the difference between using react-router's basename and CRA's homepage field? Should I be using both, or one or the other?
EDIT: Potentially found the problem. Setting homepage=/app also changes the paths for my JS bundle, which it wasn't recognizing (hence the blank page). I manually added a app folder inside my build dir, like so: build/app/static and it worked. Shouldn't CRA do this automatically?
My setup
app.tsx
<Router basename={process.env.PUBLIC_URL}>
...
</Router>
package.json
scripts: {
"build-prod": "GENERATE_SOURCEMAP=false REACT_APP_ENVIRONMENT=production react-app-rewired build",
},
...
"homepage": "/app",
Command to serve the prod build locally
> npm run build-prod && serve -s build -l tcp://0.0.0.0:5000
The project was built assuming it is hosted at /app/.
You can control this with the homepage field in your package.json.
The build folder is ready to be deployed.
Find out more about deployment here:
bit.ly/CRA-deploy
I navigate to http://0.0.0.0:5000/app/ and get a blank page (no network calls).
What I tried
- set
homepage: "/app"inpackage.jsonsource - set the basename for react-router source
- The CRA docs shows an example using the full path of the website. That didn't work either:
"homepage": "https://example.com/app",
process.env.PUBLIC_URLvalue coming in correctly? I could not find it in the build command, or if you have included it in a .env file. Essentially,homepageis used to access your assets relative to the value provided, e.g. your CSS files and images. Whereas thebasenamevalue is used by react-router to handle client-side routing relative to the value provided. Both should be included to make the app run correctly.process.env.PUBLIC_URL, I am seeing/app. Ah, I see - thanks for the clarification onhomepage/basename!. So I see part of the problem - addinghomepage=/appalso changes the JS bundle's path fromstatic/js/main.jstoapp/static/js/main.js. I manually added anappfolder inside mybuilddir and moved mystaticfolder inside it. It worked. Shouldn't this happen automatically?