Html/markdown caching issue

I’m trying to achieve this : having my grav website fetch some markdown resources via s3
and then iterate the h2 tags in that file in order to create at runtime a TOC (using twig like this : {{page.content()|markdown}} and then creating TOC using javascript ).

the problem is that these html’s will never be cached and always compiled at runtime for each request.
can this be achieved? I think I need to create and save that html file / markdown that have the rendered TOC.

Thanks for any help


I’ll try and be clearer about what I’m trying to do.

I have markdown files that I want to keep as clean as possible. I don’t want them to have the TOC inside.

my twig renders the markdown using this:


I also have a javascript code that listens to onready event, parses the dom and creates a TOC.

any advice on how can this be simplified/altered for me to enjoy grav’s caching?

any help would be appreciated.



You said:

the problem is that these html’s will never be cached and always compiled at runtime for each request

Grav is not a static site generator and does not cache generated HTML. HTML is generated at each request. You can see this in action by adding {{ "now"|date("F jS \\a\\t g:i:s") }} to a Twig template. The time will change at each request.

While not caching HTML, Grav does however cache as much as possible:

  • The header if a page is processed and cached together with the markdown. You can find these files in ‘/cache/doctrine’.
  • Twig files are processed into PHP and cached in ‘/cache/twig/’
  • Yaml files like config files, language files, blueprints etc. are being processed into PHP and cached in ‘/cache/compiled’
  • Grav’s caching is also available to plugins to store/retrieve their own data.

Other notes:

  • The use of filter ‘markdown’ in {{page.content()|markdown}} is not needed.
    According the docs about the markdown filter:

    Take an arbitrary string containing markdown and convert it to HTML using the markdown parser of Grav

    The value returned by page.content is already parsed into html.
    Try {{ var_dump(page.content) }} to see the return value.

  • In a plugin, you can make use of the caching infrastructure for the data fetched elsewhere. I can image you process (markdown?) the fetched resources as much as possible and store the resulting php associated array in cache.
    Then, on page request, the plugin can fetch the TOC from cache and then:

    • Pass the correct TOC to the Twig templates.
    • Or inject the TOC into the content of the page replacing a placeholder.

Hope this helps…

1 Like