今日已更新 340 条资讯 | 累计 26752 条内容
关于我们

Magento 2 Customer Data Sections & localStorage Performance Optimization

Magevanta 2026年08月01日 17:16 0 次阅读 来源:Dev.to

Magento 2 Customer Data Sections & localStorage Performance Optimization Every Magento 2 storefront uses customer data sections — the mechanism behind the mini-cart, customer name display, wishlist counters, and checkout summaries. It looks seamless to shoppers, but under the hood it can silently murder your page load performance. If you've ever wondered why your pages fire an extra AJAX request immediately after the initial load, or why your localStorage balloons to several megabytes, this post is for you. What Are Customer Data Sections? Magento 2 splits page rendering into two phases: server-side (Astro/Varnish/FPC) and client-side (JavaScript). Because full-page cache serves the same HTML to every visitor, personalized data — cart contents, logged-in customer name, wishlist count — cannot be rendered server-side for cached pages. Enter sections.xml and the Customer Data JS API : <!-- Vendor_Module/etc/frontend/sections.xml --> <?xml version="1.0"?> <config xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation= "urn:magento:module:Magento_Customer:etc/sections.xsd" > <action name= "checkout/cart/add" > <section name= "cart" /> <section name= "checkout-data" /> </action> </config> This file tells Magento: "When the checkout/cart/add action runs, invalidate the cart and checkout-data sections." On the next page load, JavaScript detects these invalidated sections and fetches fresh data via customer/section/load/ AJAX. The Data Flow Page loads from FPC/Varnish (no personalization) JS initializes Magento_Customer/js/customer-data localStorage is checked for cached section data (by sectionLoadUrl + storeId key) Invalidated sections trigger POST /customer/section/load/ with section names Response updates localStorage, ko.observables, and UI (mini-cart, messages, etc.) This sounds efficient, but it has three major performance traps . Performance Trap #1: The "Sections Hell" AJAX Request Out of the box, Magento 2's customer-data module calls

本文内容来源于互联网,版权归原作者所有
查看原文