Like the title, I have a landing page written by reactJS, it's small and I want to export to pure static html. I've researched and not found any solution.
Run npm run build or npx react-scripts build to create an optimized production build for your React App and then run the command npx gulp to bundle all the JS and CSS files in the static build folder into the single main html file.
If you're hosting your build with a static hosting provider you can use react-snapshot or react-snap to generate HTML pages for each route, or relative link, in your application. These pages will then seamlessly become active, or “hydrated”, when the JavaScript bundle has loaded.
A note before we begin: because the rendering all happens on the client side (unless you're using server-side rendering), React is technically “static” as far as your web server is concerned. The files that you host on your web server don't change in response to requests like PHP would.
I used webpack to generate static html and javascript files.
here is a good guide on how to start
The below code will give you the static file in the dist folder.
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
}
};
Then you can use below line code to send static files to browser, if you have express server:
app.use('/dist', express.static(path.join(__dirname, 'dist')));
// Here put all your API route.
router.get('*', (req, res) => {
const route = path.join(__dirname, '..', 'dist', 'index.html');
res.sendFile(route);
});
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