PROBLEM
I had an interesting one. I’ve blogged about Drupal 8 caching before and how annoying it is. I’ve solved the issue of caching custom blocks and caching on a specific view.
But what about paragraphs?
We needed to make content in a paragraph dynamic and change on every page load. We want a random item to display from an array of items. I noticed that this happened when the user was logged in but not anonymously. I don’t know if I have caching turned off for authenticated users or what.
After some digging, I couldn’t find a good way to turn caching off with config and I wasn’t about to turn caching off for every page. I also couldn’t find a way to do it in the preprocess of the paragraph.
SOLUTION
There’s kinda like a kill switch, I guess. I think we should probably use this sparingly as there’s a reason why Drupal caches so much (it is a HUGE footprint!).
I put this code inside my preprocess function of my paragraph in my .theme
file:
\Drupal::service("page_cache_kill_switch")->trigger();
It took me a bit to find it as googling around doesn’t yield the results I’d expect.
And note, this is how I solved it for the mean time… This way might not be the best but it works. Until I am told that this is very dangerous or whatever and we find a better way, this is what I did.