Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to export react app to pure static html

Tags:

html

reactjs

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.

like image 854
Hải Bùi Avatar asked Nov 25 '19 04:11

Hải Bùi


People also ask

Which command is used to convert React project to static HTML CSS and JS file?

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.

Does React compile to HTML?

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.

Can a React app be static?

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.


2 Answers

I used webpack to generate static html and javascript files.

here is a good guide on how to start

like image 159
Tiisetso Tjabane Avatar answered Sep 17 '22 08:09

Tiisetso Tjabane


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);
});
like image 26
Mukesh Purohit Avatar answered Sep 19 '22 08:09

Mukesh Purohit