joplin-notes/joplin notion extension/Web Development/React/static html generator.md
2026-11-04 00:33:23 +05:30

4.6 KiB

static html generator

Converting a React or any other JavaScript framework code to static HTML typically involves pre-rendering or server-side rendering (SSR). This can be particularly useful for SEO purposes or for situations where a static file is needed. Here are steps to convert React code to static HTML:

Method 1: Using ReactDOMServer for Static HTML Export

  1. Install Necessary Libraries: You will need to have react, react-dom, and react-dom/server installed. If you are not working within an existing React app, create one using Create React App.

    npx create-react-app my-static-site
    cd my-static-site
    npm install react react-dom react-dom/server
    
  2. Create a Renderer Script: Make a new file, render.js, at the root of your project.

    const fs = require('fs');
    const path = require('path');
    const React = require('react');
    const ReactDOMServer = require('react-dom/server');
    const App = require('./src/App').default;
    
    const html = ReactDOMServer.renderToStaticMarkup(
        React.createElement(App)
    );
    
    const fullHtml = `
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>My Static Site</title>
    </head>
    <body>
        <div id="root">${html}</div>
    </body>
    </html>`;
    
    fs.writeFileSync(
        path.resolve(__dirname, 'build', 'index.html'),
        fullHtml,
        'utf8'
    );
    
  3. Modify Your App Component: Ensure that your React components are defined in a way that makes them easily rendered to static HTML. For instance, if you have any runtime-specific logic, you may need to adjust it.

  4. Run the Renderer Script: Run the script to generate the static HTML file.

    node render.js
    
  5. View the Output: Check the build directory for your index.html. This file is now pure HTML and can be deployed as a static site.

Method 2: Using Next.js for Static Site Generation

Using Next.js, a React framework, it is easier to pre-render static HTML:

  1. Create a Next.js App:

    npx create-next-app my-static-site
    cd my-static-site
    
  2. Set Up Static Generation: By default, pages in Next.js will be pre-rendered as static HTML. Define your pages in the pages directory.

  3. Create Pages: Example of a simple index page in pages/index.js:

    const Home = () => {
        return (
            <div>
                <h1>Welcome to my Static Site</h1>
                <p>This is a static HTML page generated by Next.js.</p>
            </div>
        );
    };
    
    export default Home;
    
  4. Build the Site: Build your site for production:

    npm run build
    npm run export
    
  5. View the Output: The HTML files will be in the out directory, which can be deployed statically.

Method 3: Using Gatsby for Static Site Generation

  1. Create a Gatsby Site:

    npx gatsby new my-static-site
    cd my-static-site
    
  2. Create Pages: Define your pages in the src/pages directory. An example index.js file:

    import React from "react";
    
    const Home = () => (
      <div>
        <h1>Welcome to my Static Site</h1>
        <p>This is a static HTML page generated by Gatsby.</p>
      </div>
    );
    
    export default Home;
    
  3. Build the Site: Build your Gatsby site:

    npm run build
    
  4. View the Output: The generated HTML files are in the public directory.

Conclusion

You can choose an appropriate method based on your existing setup and requirements. Using frameworks like Next.js or Gatsby, which are designed for static site generation, generally makes the process simpler and more efficient.