This is a total experiment on my side; I'm pretty sure there's some better way to achieve this. I'm 100% open to suggestions, please open an issue to discuss or better, send a PR :wink:
My problem is that I'd like to use some React components with some legacy HTML/CSS, I could use Gatsby or something like that, but porting the HTML/CSS is no way an option... sometimes time is a scarce resource.
I have Googled a lot about it, but no out of the boxâ„¢ solution, so I started playing to see where I could arrive, the worst option would be no SSR, a lot of learning and fun.
After starting to look for some inspiration, I got into this issue that has the idea of some building blocks to achieve the SSR on Jekyll.
So I started trying to get things working together, my first working version was something fsckin' ugly, so after got it green, was time to refactor, and at this moment it's, at least, readable.
First you need some React components to use, so create your own on webpack/components
folder, there's a Counter
example (attention the name of the file, is the name to be used to mount the component).
Later, we need to render the component on a layout or page, as Jekyll uses liquid, I created the ReactTag, that is a Liquid::Tag
where everything happens, it's responsible for:
bundle.js
file so we can execute on the server sideReactRailsUJS.serverRender
function using execjs
that returns the HTML for the rendered component.After that, when the page finished the loading process, there's a div with component content, and it's mounted by ReactRailsUJS
, check entry.js to check how it's configured.
So simple to use:
{% react Counter|{ name: "fernandes" } %}
where Counter
is the component name (the file name, not the export/class name), and after the |
(pipe), the props are in the Ruby Hash format.
Install dependencies your way, mine is:
Run commands, run webpack to generate bundle.js before jekyll
Access your jekyll site
yarn run webpack.js # to generate your bundle.js file
jekyll build # generate `_site` folder
After that you just need to push _site
folder to gh-pages branch.
How to do this? I just create an orphaned branch with the folder content, commit and push, you can check the gh-pages branch
Wanna see it working? Check the demo
bundle.js
path? Jekyll config?Open an issue, send PR, give any idea, suggestion, you're welcome! :smile: