From 24f48c57b5bf0d7f200609d43a3ba5cbb9192e96 Mon Sep 17 00:00:00 2001 From: Mehul <5mehulhelp5@gmail.com> Date: Wed, 1 Oct 2025 13:07:41 +0530 Subject: [PATCH 1/9] alpine js error fix --- view/frontend/templates/hyva/bfcache/handler.phtml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/view/frontend/templates/hyva/bfcache/handler.phtml b/view/frontend/templates/hyva/bfcache/handler.phtml index 53f7d6e..70fa3ef 100644 --- a/view/frontend/templates/hyva/bfcache/handler.phtml +++ b/view/frontend/templates/hyva/bfcache/handler.phtml @@ -122,7 +122,7 @@ $autoCloseMenuMobile = $bfcacheConfig->autoCloseMenuMobile() ? 'true' : 'false'; if (!cartDrawer || typeof Alpine === 'undefined') { return; } - const cartDrawerData = Alpine.\$data(cartDrawer); + const cartDrawerData = Alpine.$data(cartDrawer); if (cartDrawerData && typeof cartDrawerData.open !== 'undefined') { cartDrawerData.open = false; document.body.style.overflow = ''; @@ -140,7 +140,7 @@ $autoCloseMenuMobile = $bfcacheConfig->autoCloseMenuMobile() ? 'true' : 'false'; handleMenuClosure() { const mobileMenu = document.querySelector("[x-data^='initMenuMobile']"); if (mobileMenu && typeof Alpine !== 'undefined') { - const mobileMenuData = Alpine.\$data(mobileMenu); + const mobileMenuData = Alpine.$data(mobileMenu); if (mobileMenuData && typeof mobileMenuData.open !== 'undefined') { mobileMenuData.open = false; } @@ -148,7 +148,7 @@ $autoCloseMenuMobile = $bfcacheConfig->autoCloseMenuMobile() ? 'true' : 'false'; const desktopMenu = document.querySelector("[x-data^='initMenuDesktop']"); if (desktopMenu && typeof Alpine !== 'undefined') { - const desktopMenuData = Alpine.\$data(desktopMenu); + const desktopMenuData = Alpine.$data(desktopMenu); if (desktopMenuData && typeof desktopMenuData.hoverPanelActiveId !== 'undefined') { desktopMenuData.hoverPanelActiveId = 0; } From ea5b19dd5ca1d913bb85bee96188c062cb8df793 Mon Sep 17 00:00:00 2001 From: Mehul <5mehulhelp5@gmail.com> Date: Wed, 1 Oct 2025 13:40:11 +0530 Subject: [PATCH 2/9] error fix php variable are not defined properly in js also when we click back it's not updating cart and other data so all that has been fixed --- .../templates/hyva/bfcache/handler.phtml | 79 +++++-------------- 1 file changed, 19 insertions(+), 60 deletions(-) diff --git a/view/frontend/templates/hyva/bfcache/handler.phtml b/view/frontend/templates/hyva/bfcache/handler.phtml index 70fa3ef..bf21730 100644 --- a/view/frontend/templates/hyva/bfcache/handler.phtml +++ b/view/frontend/templates/hyva/bfcache/handler.phtml @@ -4,50 +4,35 @@ use Hyva\Theme\Model\ViewModelRegistry; use Hyva\Theme\ViewModel\HyvaCsp; use Magento\Framework\Escaper; use Magento\Framework\View\Element\Template; -use MageOS\ThemeOptimization\ViewModel\BfCache; +use Magexperts\ThemeOptimization\ViewModel\BfCache; /** @var Template $block */ /** @var Escaper $escaper */ /** @var ViewModelRegistry $viewModels */ /** @var HyvaCsp $hyvaCsp */ - /** @var BfCache $bfcacheConfig */ $bfcacheConfig = $viewModels->require(BfCache::class); - -$isCustomerLoggedIn = $bfcacheConfig->isCustomerLoggedIn() ? 'true' : 'false'; -$enableUserInteractionRefresh = $bfcacheConfig->isReloadMiniCartOnInteraction() ? 'true' : 'false'; -$autoCloseMenuMobile = $bfcacheConfig->autoCloseMenuMobile() ? 'true' : 'false'; ?> From e46ba3f751e09c8da9b9c3ab201b44c4692ffba3 Mon Sep 17 00:00:00 2001 From: Mehul <5mehulhelp5@gmail.com> Date: Wed, 1 Oct 2025 13:48:09 +0530 Subject: [PATCH 3/9] Update handler.phtml by mistake Themeoptimization viewmodel file has changed --- view/frontend/templates/hyva/bfcache/handler.phtml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/view/frontend/templates/hyva/bfcache/handler.phtml b/view/frontend/templates/hyva/bfcache/handler.phtml index bf21730..e81bddd 100644 --- a/view/frontend/templates/hyva/bfcache/handler.phtml +++ b/view/frontend/templates/hyva/bfcache/handler.phtml @@ -4,7 +4,7 @@ use Hyva\Theme\Model\ViewModelRegistry; use Hyva\Theme\ViewModel\HyvaCsp; use Magento\Framework\Escaper; use Magento\Framework\View\Element\Template; -use Magexperts\ThemeOptimization\ViewModel\BfCache; +use MageOS\ThemeOptimization\ViewModel\BfCache; /** @var Template $block */ /** @var Escaper $escaper */ From a8480d97305f636729a80bfd79b8d4f90b22595e Mon Sep 17 00:00:00 2001 From: Mehul <5mehulhelp5@gmail.com> Date: Mon, 6 Oct 2025 00:34:23 +0530 Subject: [PATCH 4/9] Update handler.phtml this is well optimized code for for hyva. i have tested on my side and it working fine --- .../templates/hyva/bfcache/handler.phtml | 211 ++++++++++-------- 1 file changed, 117 insertions(+), 94 deletions(-) diff --git a/view/frontend/templates/hyva/bfcache/handler.phtml b/view/frontend/templates/hyva/bfcache/handler.phtml index e81bddd..d06d317 100644 --- a/view/frontend/templates/hyva/bfcache/handler.phtml +++ b/view/frontend/templates/hyva/bfcache/handler.phtml @@ -15,118 +15,141 @@ $bfcacheConfig = $viewModels->require(BfCache::class); ?> -registerInlineScript() ?> + +registerInlineScript() ?> \ No newline at end of file From b84e7d42a7d870aa42b3a5efc9be38e942a83a4b Mon Sep 17 00:00:00 2001 From: Mehul <5mehulhelp5@gmail.com> Date: Mon, 6 Oct 2025 00:49:02 +0530 Subject: [PATCH 5/9] Update handler.phtml simple but effective --- .../templates/hyva/bfcache/handler.phtml | 42 +++++++++++++------ 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/view/frontend/templates/hyva/bfcache/handler.phtml b/view/frontend/templates/hyva/bfcache/handler.phtml index d06d317..5c647cf 100644 --- a/view/frontend/templates/hyva/bfcache/handler.phtml +++ b/view/frontend/templates/hyva/bfcache/handler.phtml @@ -12,6 +12,9 @@ use MageOS\ThemeOptimization\ViewModel\BfCache; /** @var HyvaCsp $hyvaCsp */ /** @var BfCache $bfcacheConfig */ $bfcacheConfig = $viewModels->require(BfCache::class); +$isCustomerLoggedIn = $bfcacheConfig->isCustomerLoggedIn() ? 'true' : 'false'; +$enableUserInteractionRefresh = $bfcacheConfig->isReloadMiniCartOnInteraction() ? 'true' : 'false'; +$autoCloseMenuMobile = $bfcacheConfig->autoCloseMenuMobile() ? 'true' : 'false'; ?> -registerInlineScript() ?> \ No newline at end of file +registerInlineScript() ?> From 53db283008aed3613ef19287645e170708f18f70 Mon Sep 17 00:00:00 2001 From: Ryan Hoerr Date: Sun, 5 Oct 2025 21:18:32 -0400 Subject: [PATCH 7/9] Update handler.phtml to match changes to hyva/bfcache/handler.phtml --- view/frontend/templates/bfcache/handler.phtml | 146 +++++++++--------- 1 file changed, 74 insertions(+), 72 deletions(-) diff --git a/view/frontend/templates/bfcache/handler.phtml b/view/frontend/templates/bfcache/handler.phtml index c81186a..679e5a9 100644 --- a/view/frontend/templates/bfcache/handler.phtml +++ b/view/frontend/templates/bfcache/handler.phtml @@ -18,31 +18,43 @@ $autoCloseMenuMobile = $bfcacheConfig->autoCloseMenuMobile() ? 'true' : 'false'; $script = << { class BFCacheHandler { - /** - * Initialize BFCache handler with configuration options - */ constructor() { this.options = { isCustomerLoggedIn: {$isCustomerLoggedIn}, enableUserInteractionRefreshMiniCart: {$enableUserInteractionRefresh}, autoCloseMenuMobile: {$autoCloseMenuMobile} }; - + this.userInteractionEvents = ['touchstart', 'mouseover', 'wheel', 'scroll', 'keydown']; + this._reloadTriggered = false; + this.dom = {}; + + // Centralized selectors for reuse + this.selectors = { + minicartCloseButton: '#btn-minicart-close', + htmlElement: 'html' + }; } - + /** - * Initialize all BFCache functionalities + * Safe DOM getter with caching (re-queries only if element removed) */ + getElement(key) { + const cached = this.dom[key]; + if (cached && document.contains(cached)) return cached; + const el = document.querySelector(this.selectors[key]); + if (el) this.dom[key] = el; + return el; + } + init() { this.refreshMiniCart(); this.reloadCustomerLoginPage(); this.actionAutoCloseMenu(this.options.autoCloseMenuMobile); } - + /** - * Refresh minicart based on configuration - * Either immediately or on first user interaction + * Refresh minicart on user interaction or immediately */ refreshMiniCart() { if (this.options.enableUserInteractionRefreshMiniCart) { @@ -51,56 +63,46 @@ $script = << { - this.userInteractionEvents.forEach(eventType => { - window.removeEventListener(eventType, refreshMiniCart); - }); + this.userInteractionEvents.forEach(eventType => + window.removeEventListener(eventType, refreshMiniCart) + ); this.actionRefreshMiniCart(); }; - - this.userInteractionEvents.forEach(eventType => { - window.addEventListener(eventType, refreshMiniCart, { - once: true, - passive: true - }); - }); + const opts = { once: true, passive: true }; + this.userInteractionEvents.forEach(eventType => + window.addEventListener(eventType, refreshMiniCart, opts) + ); } - + /** - * Check customer login state consistency and reload if needed - * Compares backend state with frontend localStorage state + * Compare frontend & backend login state and reload if mismatched */ reloadCustomerLoginPage() { const backendLoggedInState = this.options.isCustomerLoggedIn; - - const getCustomerDataFromStorage = () => { - try { - const cacheStorage = localStorage.getItem('mage-cache-storage'); - const customerData = cacheStorage ? JSON.parse(cacheStorage).customer : null; - return customerData; - } catch (error) { - console.warn('BFCache: Failed to parse customer data from localStorage', error); - return null; + + let customerData = null; + try { + const cacheStorage = localStorage.getItem('mage-cache-storage'); + if (cacheStorage) { + customerData = JSON.parse(cacheStorage).customer || null; } - }; - - const customerData = getCustomerDataFromStorage(); + } catch { + // Ignore parse errors silently + } + const frontendLoggedInState = Boolean(customerData?.firstname); - - if (frontendLoggedInState !== backendLoggedInState) { + + if (frontendLoggedInState !== backendLoggedInState && !this._reloadTriggered) { + this._reloadTriggered = true; window.location.reload(); } } - + /** - * Update minicart data from customer data sections - * Reloads cart section to ensure accurate item count and totals + * Dispatch Magento event to refresh customer sections (minicart, etc.) */ actionRefreshMiniCart() { require([ @@ -109,51 +111,51 @@ $script = << { customerData.reload(['messages'], true); - - const minicartCloseButton = $('#btn-minicart-close'); + + const minicartCloseButton = document.querySelector(this.selectors.minicartCloseButton); if (minicartCloseButton.length) { minicartCloseButton.trigger('click'); } - + if (autoCloseMenuMobile) { - const htmlElement = $('html'); - - const navigationClasses = ['nav-open', 'nav-before-open']; - navigationClasses.forEach(function(className) { - if (htmlElement.hasClass(className)) { - htmlElement.removeClass(className); - } - }); + this.handleMenuClosure(); + } + }).bind(this)); + } + + /** + * Handle closing of mobile menu + */ + handleMenuClosure() { + const htmlElement = document.querySelector(this.selectors.htmlElement); + + const navigationClasses = ['nav-open', 'nav-before-open']; + navigationClasses.forEach(className => { + if (htmlElement.hasClass(className)) { + htmlElement.removeClass(className); } }); } - }; - + } + /** - * Handle pageshow event for BFCache scenarios - * Detects back/forward navigation and page restoration from BFCache + * Page restoration handler — persistent global instance */ - const handlePageShow = (event) => { + window.addEventListener('pageshow', (event) => { if (event.persisted) { - new BFCacheHandler().init(); + (window._bfcacheHandler ??= new BFCacheHandler()).init(); } - }; - - // Register event listener with proper cleanup capability - window.addEventListener('pageshow', handlePageShow, { passive: true }); + }, { passive: true }); })(); -JS; + JS; ?> renderTag('script', ['type' => 'text/javascript'], $script, false) ?> From 6cdffbfdbde9251d038cb9efbe8a184abd5eecc0 Mon Sep 17 00:00:00 2001 From: Ryan Hoerr Date: Mon, 6 Oct 2025 21:13:32 -0400 Subject: [PATCH 8/9] Added Hyva CSP warning --- view/frontend/templates/hyva/bfcache/handler.phtml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/view/frontend/templates/hyva/bfcache/handler.phtml b/view/frontend/templates/hyva/bfcache/handler.phtml index ab999a3..fc1c444 100644 --- a/view/frontend/templates/hyva/bfcache/handler.phtml +++ b/view/frontend/templates/hyva/bfcache/handler.phtml @@ -174,5 +174,5 @@ $autoCloseMenuMobile = $bfcacheConfig->autoCloseMenuMobile() ? 'true' : 'false'; }, { passive: true }); })(); - registerInlineScript() ?> +registerInlineScript() has to be called directly after the closing script tag! There must be no other HTML in between. */ ?> From fa1c5bb2c242d95dd7b78351ef39e4fefd8d3093 Mon Sep 17 00:00:00 2001 From: Ryan Hoerr Date: Mon, 6 Oct 2025 21:32:44 -0400 Subject: [PATCH 9/9] JS fixes --- view/frontend/templates/bfcache/handler.phtml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/view/frontend/templates/bfcache/handler.phtml b/view/frontend/templates/bfcache/handler.phtml index 679e5a9..b48b663 100644 --- a/view/frontend/templates/bfcache/handler.phtml +++ b/view/frontend/templates/bfcache/handler.phtml @@ -122,8 +122,8 @@ $script = << { - if (htmlElement.hasClass(className)) { - htmlElement.removeClass(className); + if (htmlElement.classList.contains(className)) { + htmlElement.classList.remove(className); } }); }