Hi! I am trying to integrate React into Hugo and running into some issues.
Goal:
- To use React components in Hugo partials and markdown files. We want to replace almost all site parts with react components.
The React library we use is a custom library of components that we wrapped into shortcodes and partials. This seems to be working, but there are a few issues we are facing.
For better clarity, here is how we connected Hugo and React:
React > Hugo connection
To enable passing props from Hugo shortcodes to React component files we use Hugo pipes:
- To create a resource from template - we inject the shortcode params as strings into the JS files
const param = {{ .Get "param" }}
; // for shortcodes
const param = {{ .Param}}
; // for partials
-
Then using Webpack we convert all files into the browser-friendly js files
-
Finally we serve the project using Webpack server (from the “public” folder)
Package.json building script - rimraf public && hugo --debug && webpack && webpack serve
Problem 1: JS files duplication in partials leads to lots of extra building
During the build, Hugo recreates every partial for each page. So if we have a Babel pipeline in it, it can process only one file at a time, so it creates the same file for each page that uses this partial. As the result even with one very small React component in partial - the build time is increasing significantly.
Is there a way to tell Hugo that this file should only be processed once and be shared with the rest of the pages?
Problem 2: There is no hot-reloading
We are only able to see changes after rebuilding the whole website. Is there a way to achieve the same goal but to save hot-reloading?
Problem 3: Shortcodes are not working in partials.
Is there a way to enable this functionality to avoid duplication of shortcodes as partials?
If you have any questions or suggestions - please feel free to ask, correct me or advise solutions! Thank you!