My Situation:
I'm hosting an app created with create-react-app
on Surge. In order to have Surge let react-router
handle the routes, I have to add a 200.html to the root, and in order to handle 404s, I have to add a 404.html to the root. The problem is, with npm run build
, only index.html
and favicon.ico
are added to the root of the build directory.
Possible Solution:
I can copy other files by making the build
script in package.json react-scripts build && cp 200.html 404.html build
.
Question:
Is there a better way to have react-scripts
add extra files to the root of the build, rather than relying on bash scripting in package.json?
The answer I settled on was to use bash scripts in my package.json as I originally suspected, but I decided to do the file operations in a deploy script instead of the build script, and rename index.html to 200.html instead of copying extra files, as suggested by the create-react-app docs;
"build": "react-scripts build",
"deploy": "npm run build && mv build/index.html build/200.html && surge build mydomain.com"
I think doing the file operation in the deploy script, instead of the build script, is the right solution because that change is unique to the deployment environment, meaning, if I was deploying somewhere other than surge, I might not want to rename the index file.
Still open to other solutions if anyone has a better suggestion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With