Dynamic content in Magento 2 does not automatically mean you must disable the Full Page Cache for the entire page. That is what Private Contentis designed to help avoid.
A block with cacheable=”false” can make the whole page non-cacheable. With Private Content, the page itself remains cacheable, while only certain user-specific sections are loaded separately and rendered dynamically.
Typical examples include the shopping cart, customer name and wishlist.
The principle is simple: Cacheable content comes from the full page cache; personal content is loaded independently.
cacheable=”false” should not be used indiscriminately just because one area of the page is dynamic. Private Content provides a cleaner solution in many cases without sacrificing full page caching for the entire page.
A simple example is a custom customer section. The PHP class provides the personalized data:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
namespace Vendor\Module\CustomerData; use Magento\Customer\CustomerData\SectionSourceInterface; class Example implements SectionSourceInterface { public function getSectionData(): array { return [ 'message' => 'Dieser Inhalt ist individuell' ]; } } |
The section is then declared in etc/frontend/di.xml registriert:
|
1 2 3 4 5 6 7 |
<type name="Magento\Customer\CustomerData\SectionPoolInterface"> <arguments> <argument name="sectionSourceMap" xsi:type="array"> <item name="example" xsi:type="string">Vendor\Module\CustomerData\Example</item> </argument> </arguments> </type> |
In Hyvä, customer sections are available through the event private-content-loaded :
|
1 2 3 4 5 6 7 8 |
<div x-data="{ message: '' }" @private-content-loaded.window=" message = $event.detail.data.example?.message || '' " > <span x-text="message"></span> </div> |
The page itself stays in the full page cache. Only the customer section data is supplied separately in the browser and can vary by visitor.