Avoiding caching ActivityPub content
-
When using LS Cache with ActivityPub, sometimes the JSON content from ActivityPub gets cached instead of the actual page. Both versions are served from the same URL, but the ActivityPub request comes with an
Accept: application/activity+jsonheader.Is there a way to prevent LS Cache from caching the JSON output, or to have a different cache for those requests?
-
@msaari
Try using this setting: https://docs.litespeedtech.com/lscache/lscwp/cache/#do-not-cache-uris
Another setting you can look into is: https://docs.litespeedtech.com/lscache/lscwp/cache/#cache-rest-api“Do not cache URIs” doesn’t help. The URI is the same. The only difference is the “Accept” header.
“Cache REST API” doesn’t help either; these are not REST API calls. These are plain front end calls, just with a different “Accept” header.
Ideally, LS Cache would respect the “Accept” header and would cache both the HTML and JSON content separately, or at least not serve HTML from the cache when the “Accept” header requests JSON.
For example, you can see how WP Super Cache includes the “Accept” header in the cache tag if it’s not “text/html”: https://github.com/Automattic/wp-super-cache/blob/004a3c3a21363578f25976dfdbe6a8dd40ae0f96/wp-cache-phase2.php#L34 This way the cache won’t return the wrong content type.
@msaari
Please send a report from LSC => Toolbox => Report => click on “Send to litespeed” then share the ID with meBYUAVDFL. I’m using the ActivityPub plugin.
For example, https://www.lautapeliopas.fi/artikkelit/kymmenen-vuotta-sitten-2015/ returns the HTML page and https://www.lautapeliopas.fi/?p=43695 with the “Accept: application/activity+json” header returns the ActivityPub JSON page (without the header, it returns the HTML page). This works, and right now when I test this, both pages show a “hit” from LS Cache.
However, occasionally the ActivityPub JSON gets cached so that visitors to https://www.lautapeliopas.fi/artikkelit/kymmenen-vuotta-sitten-2015/ see only the ActivityPub JSON. This happens rarely, but this specific page was released a couple of days ago and I already got two reports from users who got the JSON instead of the HTML.
RewriteCond %{HTTP:Accept} application
RewriteRule ^ - [E=Cache-Control:vary=%{ENV:LSCACHE_VARY_VALUE}+isjson]okay , what you are looking for is more like a cache vary
please try add this at top of your .htaccess
If lightspeed cache does support the vary header you can go to the ActivityPub advanced settings and enable that!
Nice! Those are some well-hidden advanced settings 😉 It would be nice to have some note that these advanced settings exist… If I enable that setting, are the .htaccess modifications necessary?
@msaari it is on purpose that there is no documentation about it ☺️
it is too easy to break things with these settings!
@pfefferle Maybe add a check for some of the most-used cache plugins and show a warning? “Hi, I see you’re using a cache plugin, check this setting to avoid problems with the cache”?
@msaari if you can verify that the setting fixes your issue, I will change the default behaviour of the
varyheader setting whenLiteSpeed Cacheis active.Unfortunately, this is something I can’t solidly verify – this is something that happens occasionally, and most of the time I don’t even hear of it. If I hear no complaints within a year, it’s probably fine…
hi @pfefferle , you may need to explicitily insert the rules I mentioned at top of htaccess, in order to tell cache engine to vary header , and it’s on the condition with
acceptheader , not by thevaryheader though.<IfModule LiteSeed>
RewriteCond %{HTTP:Accept} application
RewriteRule ^ - [E=Cache-Control:vary=%{ENV:LSCACHE_VARY_VALUE}+isjson]
</IfModule>better to enclose it with ifmodule , and also only insert it if LSCWP is enabled, you can check it by
if (defined('LSCWP_V'))-
This reply was modified 9 months, 3 weeks ago by
qtwrk.
@pfefferle I can now confirm that the Vary header setting alone does not solve the problem.
Thanks for the feedback @msaari ! With the next version, we provide an option to disable Content-Negotiation for user facing sites. This might be another option, to prevent JSON in the cache.
I will also work on an integration for the LiteSpeed Cache Plugin, like we currently did for Surge: https://github.com/Automattic/wordpress-activitypub/pull/1648
Maybe we can automatically add the .htaccess rules if both plugins are installed. I will drop an update here, maybe you can have a look then @qtwrk
-
This reply was modified 9 months, 3 weeks ago by
The topic ‘Avoiding caching ActivityPub content’ is closed to new replies.