We extended the product listing to contain the specific paramter "property-groups" which influences the output.
All works fine in dev
mode, but when switching to prod
mode, changing that parameter does not have any effect any more.
It seems this is cached, but why is that, even the URL parameters change?
i.e.
/widgets/cms/navigation/67e6a05deae747e984a622c7e94e6881?no-aggregations=1&order=name-asc&p=1&property-groups=385b9ebbfc8e450bac9c4d63052ddf7f&slots=7b2547b253a145468fa35eb684b26a62
returns the same like
/widgets/cms/navigation/67e6a05deae747e984a622c7e94e6881?no-aggregations=1&order=name-asc&p=1&property-groups=&slots=7b2547b253a145468fa35eb684b26a62
If we flush the full cache, the second call works and returns the right result.
We of course do not want to fully disable the caching, so we probably have to tell Shopware 6 somehow, to use the new parameter in the cache key.
But we could not find, where the caching happens by debbuging through \Shopware\Storefront\Controller\CmsController::category
.
Problem seems to be this:
\Shopware\Core\Content\Product\SalesChannel\Listing\CachedProductListingRoute::generateKey
private function generateKey(string $categoryId, Request $request, SalesChannelContext $context, Criteria $criteria): string
{
$parts = [
self::buildName($categoryId),
$this->generator->getCriteriaHash($criteria),
$this->generator->getSalesChannelContextHash($context),
];
$event = new ProductListingRouteCacheKeyEvent($parts, $categoryId, $request, $context, $criteria);
$this->dispatcher->dispatch($event);
return md5(JsonFieldSerializer::encodeJson($event->getParts()));
}
Here a cache key is generated which takes account the standard parameters, but not the new one added by us.
You have to listen for the ProductListingRouteCacheKeyEvent
and add your parameter to the parts.
public static function getSubscribedEvents(): array
{
return [
// ...
ProductListingRouteCacheKeyEvent::class => 'addPropertyGroupsCachePart'
];
}
public function addPropertyGroupsCachePart(ProductListingRouteCacheKeyEvent $event)
{
$event->addPart((string)$event->getRequest()->get('poperty-groups'));
}
This way Shopware generates a unique cache key that is also based on that parameter we introduced and the cache collision disappears.