vigetlabs / craft-viget-base

http://code.viget.com/craft-viget-base/
7 stars 3 forks source link

Blitz caching the Edit Entry button #69

Open maxfenton opened 1 year ago

maxfenton commented 1 year ago

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

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:

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

👏 This seems good to go in https://github.com/vigetlabs/craft-viget-base/commit/605fb745e5b95f37e359f70ca1b1067ce4c48f62