Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

General

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

Solved by Harald Schneider View solution

Started by lilive 6 years ago · 6 replies · 469 views
6 years ago

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

YAML
---
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.

last edited 02/18/20 by lilive
6 years ago Solution

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
6 years ago

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.

6 years ago

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
6 years ago

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
6 years ago

You're right 🙂
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

Suggested topics

Topic Participants Replies Views Activity
General · by Jerry Hunt, 4 days ago
2 82 12 hours ago
General · by pamtbaau, 17 hours ago
1 55 16 hours ago
General · by Andy Miller, 1 day ago
0 45 1 day ago
General · by Marcel, 12 months ago
6 348 5 days ago
General · by Duc , 5 days ago
3 43 5 days ago