Clear cache for pages displaying datas coming from other pages (via a shortcode)

Hello there,

I use custom shortcodes ( made with this method Custom Shortcodes > Simple Way ) to display informations coming from other pages.

My problem is that the pages with the shortcode do not update when the pages with the informations are edited. I’m wondering how to automate a cache clear.

To be a litte more explicit:
Say that I have some “source” pages with frontmatters like

---
title: Happy birthday to John
date: 2020-02-17
news:
    thumbnail: gift.png
    targets:
        - page1
        - page2
---

and that a “target” page contains the shortcode [news target=page1]
When this page is displayed, this shortcode is replaced by a list of page.title, page.date, page.header.news.thumbnail for each source page where header.news.targets contains the value page1.
This is great, it works fine and it saves me the burden to frequently edit several target pages.
But when I modify a source page, the target pages are not updated until I manually clear the cache. Does anyone have some advices to improve my system ?

I never wrote a plugin. I think a simple solution may be to catch an appropriate grav event (onPageProcessed maybe ?), check if page.header.news is defined, and clear the whole cache ( perhaps grav[‘cache’]->clearCache( xxx ) ? ). What do you think ?
Or perhaps it would be better to catch the event, loop over all the pages, and clear the cache only for the ones that contains a matching shortcode ?

Thank you.

Maybe this approach is much simpler - in all related pages set these settings in the Page’s Advanced tab:

  • Enable “Never Cache Twig”
  • Disable “Caching”

Hit “Save” and clear the whole cache once, manually. Then test again.

1 Like

Thank you for the suggestion. It is simpler, indeed. But I’m concerned about performances: about 1/2 of my website pages contain such a shortcode, and it is running on a basic shared host.

This is a matter of testing. if you clear the whole cache very often, things will get very slow. So the better way is to keep certain pages uncached. In most cases it costs more time to rebuild the cache as to deliver uncached pages.

1 Like

I’d agree that not caching is the way to go. Even if half of your pages are concerned, you are probably not getting that many pagehits (and if you do, it’s maybe time to move onto a VPS anyway).

An alternative could be to update the target page via some AJAX instead ?

1 Like

You’re right :slight_smile:
I did what you said, and after some tests it doesn’t seem to be a performance problem at all.
I was worried because my server pack is “for static websites or personnal blogs”. But I’ve rarely got more than 100 pages hits a day (perhaps never if I do not count my admin days).
Thanks to both of you, I like Grav and its community a little more each day !

1 Like

We like to be liked a bit more :slight_smile: