Skip to main content
  1. Extra/

React Hugo Boilerplate Code

·367 words·2 mins· loading · ·

On This Page

Table of Contents
Share with :

Here’s a clean and minimal boilerplate structure and guide for integrating React components inside a Hugo project using Vite + TypeScript. This setup gives you the performance of Hugo with the interactivity of React.


🧱 Project Structure
#

my-hugo-site/
β”œβ”€β”€ assets/
β”œβ”€β”€ content/
β”œβ”€β”€ layouts/
β”‚   └── partials/
β”‚       └── react-widget.html       <-- Embed point
β”œβ”€β”€ static/
β”‚   └── js/
β”‚       └── (bundled React code here)
β”œβ”€β”€ react-app/                      <-- Vite + React project
β”‚   β”œβ”€β”€ index.html (optional)
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   └── main.tsx
β”‚   β”‚   └── components/
β”‚   β”‚       └── Widget.tsx
β”‚   └── vite.config.ts
β”œβ”€β”€ config.toml

πŸš€ Step-by-Step Guide
#

βœ… 1. Create React App Inside Hugo
#

Navigate to your Hugo root and run:

npm create vite@latest react-app -- --template react-ts
cd react-app
npm install

βœ… 2. Edit vite.config.ts
#

Modify the vite.config.ts to output the bundled file to Hugo’s static/js/:

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';

export default defineConfig({
  plugins: [react()],
  build: {
    outDir: '../static/js',
    emptyOutDir: true,
    rollupOptions: {
      input: './src/main.tsx',
      output: {
        entryFileNames: 'bundle.js',
      },
    },
  },
});

This ensures Hugo can serve the React app at /js/bundle.js.


βœ… 3. Create a Simple React Widget
#

react-app/src/components/Widget.tsx

export function Widget() {
  return (
    <div style={\{ padding: "1rem", background: "#eef" }\}>
      Hello from <strong>React!</strong>
    </div>
  );
}

react-app/src/main.tsx

import React from 'react';
import ReactDOM from 'react-dom/client';
import { Widget } from './components/Widget';

const root = document.getElementById('react-root');
if (root) {
  ReactDOM.createRoot(root).render(<Widget />);
}

βœ… 4. Add a Hugo Partial to Load React
#

layouts/partials/react-widget.html

<div id="react-root"></div>
<script src="/js/bundle.js" defer></script>

βœ… 5. Add Partial to Your Layout
#

In your layouts/_default/baseof.html or any specific layout, include:

{\{ partial "react-widget.html" . }\}

βœ… 6. Build React App
#

From inside the react-app directory:

npm run build

This creates static/js/bundle.js for Hugo to use.


βœ… 7. Run Hugo
#

In the Hugo root:

hugo server

You should now see your React component rendering inside a Hugo page!


βœ… Bonus Tips
#

  • Use npm run dev in react-app with proxy to Hugo for dev HMR.
  • Keep React small (widgets, forms, dashboards) β€” let Hugo handle content.

Would you like me to generate this project structure as a GitHub repo or zip file?

Dr. Hari Thapliyaal's avatar

Dr. Hari Thapliyaal

Dr. Hari Thapliyal is a seasoned professional and prolific blogger with a multifaceted background that spans the realms of Data Science, Project Management, and Advait-Vedanta Philosophy. Holding a Doctorate in AI/NLP from SSBM (Geneva, Switzerland), Hari has earned Master's degrees in Computers, Business Management, Data Science, and Economics, reflecting his dedication to continuous learning and a diverse skill set. With over three decades of experience in management and leadership, Hari has proven expertise in training, consulting, and coaching within the technology sector. His extensive 16+ years in all phases of software product development are complemented by a decade-long focus on course design, training, coaching, and consulting in Project Management. In the dynamic field of Data Science, Hari stands out with more than three years of hands-on experience in software development, training course development, training, and mentoring professionals. His areas of specialization include Data Science, AI, Computer Vision, NLP, complex machine learning algorithms, statistical modeling, pattern identification, and extraction of valuable insights. Hari's professional journey showcases his diverse experience in planning and executing multiple types of projects. He excels in driving stakeholders to identify and resolve business problems, consistently delivering excellent results. Beyond the professional sphere, Hari finds solace in long meditation, often seeking secluded places or immersing himself in the embrace of nature.

Comments:

Share with :