Running code once when private data is loaded
When custom initialization code needs to run only once after private section data loads (e.g., in Hyvä), use the following pattern:
<script>
const initMyCode = (event) => {
// event.detail.data contains the entire sectionData
const sectionData = event.detail.data;
const isLoggedIn = sectionData.customer && !!sectionData.customer.firstname;
// other code...
const cartSummaryCount = sectionData.cart ? sectionData.cart.summary_count : 0;
};
window.addEventListener('private-content-loaded', initMyCode, { once: true });
</script>
Note
The { once: true } option in window.addEventListener ensures the callback runs only once. This is a native browser API feature that automatically unregisters the event listener after its first execution.