Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blitz caching the Edit Entry button #69

Open
maxfenton opened this issue Oct 26, 2023 · 3 comments
Open

Blitz caching the Edit Entry button #69

maxfenton opened this issue Oct 26, 2023 · 3 comments
Assignees

Comments

@maxfenton
Copy link
Contributor

maxfenton commented Oct 26, 2023

The red "Edit Entry" sometimes ends up in Blitz or CDN cached versions of the site.

We should explore a few different options to prevent this from happening when content editors view the front-end of the site.

There are a few caching layers

  • Blitz's caching (stored as files on the server or in Yii's Cache).
  • CDN caching (Cloudflare or Cloudfront sitting in front of the site). These are separate and unaware of Blitz or Craft internals. All that the Blitz integration does is invalidate these caches when content changes.
    • It's often possible to prevent these services from caching by passing the appropraite no-cache/store headers

Change to Viget.com module code (no-cache headers for admins)

Details:
Across a few sites that use this module with the Blitz plugin (e.g. ADC-AMH, Viget) sometimes the Edit Entry button gets cached in the Blitz static cache for all users.

It was a misunderstanding that Blitz automatically does not cache or serve cached assets to signed-in users, but in looking at the source code the logic is as follows:

Blitz is only checking:

  • is the site turned off but the user can see it anyway? ($user !== null) && (!Craft::$app->getIsLive() && !$user->can('accessSiteWhenSystemIsOff'))
  • does the user have the debug toolbar open ($user !== null) && ($user->getPreference('enableDebugToolbarForSite'))
  • does the page say not to cache it (admin area etc) using (!empty($request->getParam('no-cache')))
  • page has a token ($request->getToken() !== null && !$this->getIsGeneratorRequest())

During development, Viget developers (cough me) have the debug toolbar visible so the caching issue was not immediately evident or consistent. Seeing that this Blitz logic checks for the no-cache header, add a no-cache header to all pages that show the Edit Entry button

Looking at the Craft 4 code for Response there may be a public function setNoCacheHeaders() or else the module might need to do something like

$request = Craft::$app->getRequest();
$request->setHeader('Pragma', 'no-cache', true);

Ref:

@maxfenton
Copy link
Contributor Author

Joshua correctly pointed out that I was mis-reading (!empty($request->getParam('no-cache'))) as checking a Header when it is checking a GET param. setNoCacheHeaders() will not work.

@maxfenton
Copy link
Contributor Author

maxfenton commented Oct 26, 2023

Can we do a check in the module to see if Blitz exists and if it does, only output this if the Debug Toolbar is also visible?

isPluginEnabled()
https://docs.craftcms.com/api/v4/craft-services-plugins.html#method-ispluginenabled

$user = Craft::$app->getUser()->getIdentity();
if ($user !== null) && ($user->getPreference('enableDebugToolbarForSite')) { ... output the button }

@maxfenton
Copy link
Contributor Author

👏 This seems good to go in 605fb74

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants