6

I'd like to populate my Docusaurus project with documentation using the "docs" plugin and a custom (JavaScript) plugin to connect it to a headless CMS. Currently, I'm using the loadContent Lifecycle API event to call my Headless CMS API and then using fs.writeFileSync to create physical markdown files in '/docs' and overwriting the ./sidebars.js file so the 'docs' plugin that comes with the classic preset works.

./my-plugin/index.js:

module.exports = function (context, options) {
return {
  name: 'my-docusaurus-plugin',
  async loadContent() {
    //calls to Headless CMS API for documentation content
    let response =  await fetchArticles('documentation');
    // Adds the markdown files for 'docs' plugin using fs.writeFileSync   
    await buildArticles(response)

    //fetch homepage and navigation sections from CMS API
    let homepage = await fetchPages('homepage');
    let sidebarSection = await fetchPages('page');

    //overwrite ./sidebars.js with API navigation data using fs.writeFileSync
    await buildSidebar(homepage, sidebarSection);
  }
};

};

This works in that I get content from my CMS and the Documentation renders, but it seems more like a workaround than an elegant solution for connecting a headless CMS with Docusaurus. Am I missing some best practices or is there a better approach using other lifecycle events?

  • I am in a similar situation. It seems that editing a file triggers a reload, which calls loadContent and then contentLoaded that edit files. So I am stuck in an infinite loop. Did you find any resource on that subject? – Gabin Feb 22 '21 at 20:57
  • Unfortunately, I had to take another approach due to time constraints on the project. I ended up using [extendCli] (https://v2.docusaurus.io/docs/lifecycle-apis#extendclicli) with a custom npm build script in order to pull content from my headless CMS and create markdown files: [cli extension](https://github.com/kentico-michaelb/kontent-docusaurus-boilerplate/blob/main/my-website/cms-scripts/index.js) [package.json containing "sync-kontent" script](https://github.com/kentico-michaelb/kontent-docusaurus-boilerplate/blob/main/my-website/package.json) – Michael Berry Feb 23 '21 at 19:01

0 Answers0