Jake B.
Well-known member
- Affected version
- 2.2.4
We need to overwrite the cache ID for requests made by a certain user agent to prevent the page header / navigation from being rendered. I'm currently able to accomplish this by doing the following:
However, this won't work if someone else has already set a cache ID generator in another add-on, or if XF ever decides to set it themselves in the future as it throws an exception. I'm catching this Exception currently and allowing it to fall back to whatever was set previously, but that makes it so my changes no longer work. Would be much cleaner if I was able to extend the
PHP:
/** @var PageCache|null $pageCache */
$pageCache = $app->container('pageCache');
if ($pageCache) {
try {
$pageCache->setCacheIdGenerator(function(Request $request) {
$options = \XF::options();
$styleId = intval($request->getCookie('style_id', 0));
if (!$styleId) {
$styleId = $options->defaultStyleId;
}
$languageId = intval($request->getCookie('language_id', 0));
if (!$languageId) {
$languageId = $options->defaultLanguageId;
}
$uri = $request->getFullRequestUri();
$uri = preg_replace('#(\?|&)_debug=[^&]*#', '', $uri);
return 'audapp_page_' . sha1($uri) . '_' . strlen($uri) . "_s{$styleId}_l{$languageId}_v" . PageCache::CACHE_VERSION;
});
} catch (\LogicException $e) {
// We don't want to overwhelm the logs in this case, so we'll allow it to fall back
}
}
However, this won't work if someone else has already set a cache ID generator in another add-on, or if XF ever decides to set it themselves in the future as it throws an exception. I'm catching this Exception currently and allowing it to fall back to whatever was set previously, but that makes it so my changes no longer work. Would be much cleaner if I was able to extend the
\XF\PageCache::generateCacheId
method and add my prefix without the potential of running into this issue