Pages cache stays empty

Glad I could help :slight_smile:

In case it’s of use to you: here’s a hacky solution I came up with, since I needed caching restored despite wanting to check for user status in a snippet – this little plugin is a temporary workaround until the discussed changes might appear in core.

Kirby::plugin('my/plugin', [
  'siteMethods' => [
    /* returns true if logged-in user and their role is in $roles (can be string or array) */
    'userIs' => function ($roles) {
      // the cookie check can be removed once core issue #3976 is resolved
      if (!empty(Cookie::get(option('session.cookieName')))) {
        if (kirby()->user() && in_array(kirby()->user()->role()->id(), (array)$roles)) {
          return true;
        }
      }
      return false;
    },
  ],
]);

This way, I can check for site()->userIs('admin') instead of kirby()->user() && kirby()->user()->isAdmin() while not triggering that cache exclusion rule for anybody who never logged in.

The trick of hiding the real session check (which triggers the “no caching” rule) behind a preposed check for overall existence of a Kirby session cookie of course only works on sites that use Kirby’s sessions exclusively for login …and any plugin still using the “normal” way of checking for a session would foil this effort as well.

3 Likes