Disable cache for specific template

Hi!

I have a site that includes pages that are protected by password. the problem is that the cache renders those templates after someone logs into them (As they are cached it seems to skip the controller and everything).

I know I can disable cache by url, but the way the site is built allows those pages to have different urls and locations. so the question is, can cache bbe disabled for specific templates?

Thanks!

If you filter your site inex by template, you should get a collection of pages, from which you can create an array of uris to exclude from the cache.

I was trying to do that, but accessing the site() in the config is impossible as I am using multi lang kirby - and I am hitting this:

Is there a workaround?

I don’t know any.

But does the list of excluded URLs have to be dynamic?

If nothing changes we will force it not to be. But its preferable if it was dynamic

ps. Thanks for the help!

Have you tried setting the excluded URL’s dynamically via a URL in a plugin?

e.g. this:

function excludedfromcache() {
    $excludedPages = kirby()->site()->index()->filterBy('template', 'page');
    $exclArr = [];

    foreach($excludedPages as $p) {
        array_push($exclArr, $p->uri());
    }

    return $exclArr;
}

c::set('cache.ignore', excludedfromcache());

Edit: Nevermind. It suppresses the errors, but in my quick test, pages which should be excluded get cached anyway. I can only assume setting cache.ignore in a plugin is too late.

yea, I’ve tried different things, plugins, controllers… I think it has to be before the cache true for it to work

Another thing I just. tested is to write the URIs to file and then to. read this file.

c::set('cache', true);
$excluded = json_decode(f::read(kirby()->roots()->index().'/urls.txt'));

c::set('cache.ignore', $excluded);

Looks like it could work.

This list could then be updated via hooks.

Clever & out-of-the-box.
Be sure to have logic to invalidate and rebuild this cache when needed though.

interesting ideas - I will check this out.