7

At the top of every page of page of my organization's site resides the current hours of operation and current "now open"/"now closed" status, using Views block and Office Hours module).

At this time of night on a Wednesday when logged in, I correctly see that the hours are reflecting tomorrow's hours and that the organization is now closed:

enter image description here

However when logged out, I see that the current day's hours (now over) and "NOW OPEN". It's six hours after closing time and the 5 minute page cache time in performance settings does not seem to be respected for anonymous page views:

enter image description here

What can I do to force an anonymous cache clear (overkill?) or invalidation of a block for anonymous users, perhaps on a schedule? I'm aware that the built-in Page Cache module may be caching full rendered pages for anonymous users while Dynamic Page Cache module makes it usable for logged in users. Shouldn't Page Cache respect the page cache maximum age set in the Performance settings page? Most people viewing the current hours at the top of every page are anonymous users.

For now I'll manually clear the cache, making the status update properly (not ideal).

7
  • 1
    This sounds like a great place to use Drupal 8's lazy builder: tech.dichtlog.nl/php/2015/08/03/lazy-builder-callback.html qed42.com/blog/lazy-builders-drupal8 This allows the rest of the page to be cached, but your lazy builder service would return whether business is now open or now closed.
    – oknate
    Oct 27 '16 at 5:00
  • Thank you for the quick insight everyone. I'll test answers as soon as possible today. Oct 27 '16 at 12:53
  • Nice idea, but actually doesn't work. lazy builder/bigpipe is for anonymous users only.
    – Berdir
    Oct 27 '16 at 19:43
  • @Berdir Most visitors to the website are not logged in. However, it seems to be the case that lazy builder+BigPipe enabled is not a working solution yet (unless I'm missing something) for anonymous users. I'm using the Icecream example from this post. It works very well when logged in, but that doesn't solve my issue. I'll keep playing around and then try a JavaScript solution next... Note: I have Performance settings: page cache set to one minute (anonymous users only seeing chocolate icecream for an hour+) Nov 9 '16 at 20:19
  • Update: It seems that if I have "Page cache maximum age" set to "<no caching>" (followed by cache being cleared), pages in are still being long-term-cached for anonymous users. Maybe there is a different issue here... Nov 9 '16 at 20:25
5

At the top of the issue queue of Office Hours is this issue:

Fix javascript in D8-version of office_hours module

The problem is, you need this issue resolved, because for anonymous users javascript has to work.

If you want to build your own module, follow @oknate advice and use a lazy builder in a custom block plugin and install BigPipe to get the javascript part for anonymous logged-in users.

Updated question:

For anonymous users you can instead of disabling the Internal Page Cache set an expires header to the response as suggested by @Berdir's comment. See https://drupal.stackexchange.com/a/196557/47547

1
  • 3
    Javascript is by far the easiest solution. Anonymous page cache doesn't support lazy builders and big pipe doens't either (there is an issue to support it, but that's not for cached pages, just for the first request for an anonymous user). Dynamic max-age would be better than lazy builder actually, since it is not dynamic, just timed. But anonymous page cache also doesn't respect max-age, unfortunately. The only thing that it respects is the expires header on the response object, so you could try to set that on the response object, with a response event.
    – Berdir
    Oct 27 '16 at 19:46
1

Try setting "cache max-age" in your block definition. As per short description provided on Cache API Page for max-age:

For time-sensitive caching, i.e. time dependencies

For example of how to use/set cache in block check Drupal 8 Block Cache.

Hope this helps.

1

A quick and dirty solution would be to use the Cleaner module to force the cache to clear when cron runs. You could schedule cron runs to occur at open/close times. It's not an ideal solution because Cleaner clears the entire cache, resulting in slower performance. But it's a quick fix until you have time to develop the lazy builder/bigpipe solution mentioned above.

1
  • While I wouldn't call this a final solution, I feel it deserves an upvote as it has taken the stress out of current hours for this small site while I test out the bigpipe+lazy builder. Thanks! Oct 27 '16 at 22:52
1

Have you tried implementing hook_block_view_BASE_BLOCK_ID_alter?

Here is the code to exclude from cache.

function hook_block_view_BASE_BLOCK_ID_alter(array &$build, \Drupal\Core\Block\BlockPluginInterface $block) {
  $build['#cache']['max-age'] = 0;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.