Wikimedia Developer Support

Cache problem when displaying content if cookie is detected


I have a cache problem. A have placed a button, if I click this button I set a cookie and reload the page, both with javascript. What should happen is: check with $_COOKIE if cookie is set, if yes then parse a tag like <something>. The problem is, it’s not working because of the cache. If I delete the cache manually it works fine. Cookie is detected, everything works as intended. It works if I add ?action=purge to the reload-URL. But there’s a promt I do not want to have.

I hope everything is understandable. Thank you.

Assuming the contents of the tag <something> can be cached (it doesn’t depend on the user viewing the page, the moment of time the page is viewed or another variable not dependent on the content of the page itself) you could modify the cache hash with the PageRenderingHash hook when the cookie is set.

Otherwise, if you want your page to not be cached at all, you may look at how an extension like MagicNoCache does that.

1 Like

Thank you for your answer.
I already tried PageRenderingHash hook but it only works for logged in users.
MagicNoCache does ist by

$parser->getOutput()->updateCacheExpiry( 0 );
$wgOut->enableClientCache( false );

Sadly it does nothing. I tried something similiar but then it does empty the page cache for every user (logged in or not).

I don’t get it why it’s so hard to do.

on the hook “onParserBeforeStrip” I just check if cookie is set, if yes, show content X otherwise content Y. Not possible for me to do this. Trying this for days! A little bit frustrating.