27

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.

akhtarvahid
  • 9,445
  • 2
  • 26
  • 29
Hải Bùi
  • 2,492
  • 2
  • 9
  • 17
  • you need to build for production then only you can get static file – akhtarvahid Nov 25 '19 at 04:10
  • 1
    Basically you need to build it to get all the static files, if you used create-react-app, you can find more about deployment here: https://create-react-app.dev/docs/deployment/ – Loi Nguyen Huynh Nov 25 '19 at 04:15
  • 1
    Possible duplicate of [is there a way to build a react app in a single html file?](https://stackoverflow.com/questions/51949719/is-there-a-way-to-build-a-react-app-in-a-single-html-file) – Grinish Nepal Nov 25 '19 at 04:18
  • @HuỳnhLợiNguyễn No I mean a static html file like you can run it by just open index.html file. Not run any command – Hải Bùi Nov 25 '19 at 05:03
  • I think you should try `npm run build`. From what I understand you want, this should do it. Works if you used create-react-app to build your project – PiNaKa30 Nov 25 '19 at 05:08
  • @PiNaKa30 I've tried, but build folder still react app, you need run command "serve -s build" to start it – Hải Bùi Nov 25 '19 at 05:10
  • 1
    Then create-react-app website suggests 2 packages: [Pre-rendering into Static HTML](https://create-react-app.dev/docs/pre-rendering-into-static-html-files/) – PiNaKa30 Nov 25 '19 at 05:15
  • Ok @PiNaKa30 I'll try it, thank you – Hải Bùi Nov 25 '19 at 05:30
  • right click Save as... – Lawrence Cherone Nov 25 '19 at 05:45

5 Answers5

14

I used webpack to generate static html and javascript files.

here is a good guide on how to start

Tiisetso Tjabane
  • 2,088
  • 2
  • 19
  • 24
2

You need to pre render them with tools like https://www.npmjs.com/package/react-snapshot or https://github.com/stereobooster/react-snap

that actually make a snapshot of your current build but as plain html/css/js that can be run from any browser or even offline. There are also more advanced frameworks for this like Gatsby but this 2 should be enough for simpler problems.

Blissful
  • 198
  • 8
  • 3
    helpful links, thanks.. I'd say remove the NPM repo `react-snapshot` link though (_edit: ..or change to it's GH link?_), since it's [`README.md` on GitHub](https://github.com/geelen/react-snapshot) says that it's deprecated, recommending `react-snap` in it's place anyway. (: – Andre Greeff Oct 30 '21 at 18:25
1

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);
});
1

Here is my attempt. From the docs we can see that package.json entry of homepage makes THE difference, including deployment on GitHub pages which I have written about.

  1. Standard CRA app
  2. Edit package.json with "homepage": "."
  3. Run npm/yarn build
  4. On Linux xdg-open build/index.html

Your app should be running. And if the question is asking about embedding it in your custom HTML file then either use an iframe or link what is in the index.html file with the referenced element for React to render there.

Layik
  • 43
  • 6
-1

Convert react app to complete static HTML isn't possible since 'dynamic rendering' is on which whole react is working. Although if you want equivalent html, you can get that from browser by inspecting but that's not 100% reliable solution.

Even if you have a simple react app, for ex. Simple hello world, you can get equivalent HTML and css at runtime by inspecting but if someone have used something like router or States etc ,then it's not possible to convert it to STATIC HTML and retain all its working easily.

The point you mentioned that you want it to run simply by running the index.html , that is possible but that's also not static html.

Run npm build or yarn build , this will generate a build directory , in that you have static folder, index.html, all your css , js files , all minified , ready for deployment. Now you can simply run index.html, all css , js is already linked in that HTML file , this build is just a normal version of your react app which will run in browser directly , by just running index.html, no node environment needed!

So, your second part is solved that you have index.html, you can directly run that in browse.

If you want single HTML file, you can manually move all CSS and js to style and script tags respectively in index.html after build.