Adobe Commerce Front-End Dev Expert study materials
Adobe Commerce Front-End Dev Expert study resources

Full Page Cache Serves Stale Cart Block After Login

Adobe Commerce Front-End Dev Expert · Put your knowledge to work in context.

All case studies

Full Page Cache Serves Stale Cart Block After Login

A store uses Magento's built-in full page cache with customer_data and cart sections. A custom header block displays the cart item count via a Knockout template bound to customerData.get('cart'). After a customer logs in on a category page, the header count updates correctly. But after logout, the header still shows the previous customer's item count until a hard refresh. The block is defined in default.xml with cacheable="false". QA also sees the issue only on category and CMS pages, not on the cart page. The site runs Varnish in front of Magento.

1. Explain why the stale count appears and why the cart page is unaffected.

Read the worked answer

cacheable="false" on a block prevents that block's HTML from being cached in the FPC hole-punch, but the Knockout template reads customerData.get('cart'), which is populated by the customer_data section served as a private section. On logout, the customer_data section must be invalidated and the private content refreshed. If the header block is cacheable=false but its Knockout binding is rendered inside a cached page shell, the shell is served from Varnish; the private section request should update it via customerData.reload. The cart page is unaffected because it is excluded from FPC (it's in the no-cache list) and re-renders server-side. The stale count means the customer_data section wasn't invalidated on logout, or the private section request is being served from Varnish because the X-Magento-Vary cookie/header isn't varying correctly. Fix: ensure logout triggers customerData.invalidate(['cart','customer']) and that Varnish passes X-Magento-Vary and bypasses cache for /customer/section/load/.

2. What configuration and code changes make the header correct on all pages?

Read the worked answer

In default.xml, keep the header block cacheable="false" only if it truly must be; better, make it cacheable and let the private section drive the count. Add customerData.invalidate(['cart','customer']) in the logout success handler. In Varnish VCL, ensure /customer/section/load/ is not cached and that X-Magento-Vary is honored. Verify in pub/static that the Knockout template uses customerData.get('cart') with a subscribe that calls customerData.reload(['cart'], true) after login/logout events. Test: log in, add item, log out, navigate to a category page - count should be zero without a hard refresh.