Fix Field cache for Anonymous users, only if Internal Page Cache is enabled

Created on 4 September 2023, over 1 year ago
Updated 22 September 2023, about 1 year ago

Problem/Motivation

There are several problems with serving page cache for anonymous users.

The office_hours module displays the Office hours af an entity, including a open/closed status indicator.
Depending on the time fo day, the indicator may change.
However, when the Internal Page Cache module is enabled, the page is cached forever for anonymous users.
This module is especially for caching pages for anonymous users, when no external caching (Varnish etc.)is enabled.

Steps to reproduce

- in OfficeHoursCacheHelper~getCacheMaxAge(), remove the exception for Anonymous users.
- Enable page_cache module (default Drupal setting)
- Prepare 2 browsers , 1 with authenticated user, the other with anonymous user
- Set office hours field formatter to display current status open/closed.
- Edit an entity, make sure that current slot ends in 1-2 minutes. Save.
- Display the entity in both browsers. the status says 'closed'
- refresh the pages until end time kicks in. Authenticated user is refreshed. Anonymous user is not.
- Disable page_cache module (default Drupal setting)
- repeat. all works fine.
- in OfficeHoursCacheHelper~getCacheMaxAge(), add the exception for Anonymous users.
if (\Drupal::currentUser()->isAnonymous()) {return 0;}
- repeat. all works fine, but cache is now always disabled, even when page_cache is disabled.

Proposed resolution

Add a check for moduleExists('page_cache')

🐛 Bug report
Status

Fixed

Version

1.11

Component

Code - formatter

Created by

🇳🇱Netherlands johnv

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024