Skip to content

Commit e52100d

Browse files
committed
Remove active category in the cache key
- Remove ViewModel;
1 parent 98d4e21 commit e52100d

File tree

5 files changed

+24
-197
lines changed

5 files changed

+24
-197
lines changed

app/code/Magento/Catalog/ViewModel/SeoConfigTopMenu.php

Lines changed: 0 additions & 83 deletions
This file was deleted.

app/code/Magento/Catalog/view/frontend/layout/default.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,5 @@
6666
<block class="Magento\Framework\View\Element\Js\Components" name="head.components" as="components" template="Magento_Catalog::js/components.phtml"/>
6767
</referenceContainer>
6868
<block class="Magento\Framework\View\Element\Template" name="head.additional" as="head.additional" template="Magento_Theme::html/container.phtml"/>
69-
<referenceContainer name="page.top">
70-
<referenceBlock name="catalog.topnav" template="Magento_Catalog::html/topmenu.phtml">
71-
<arguments>
72-
<argument name="viewModel" xsi:type="object">Magento\Catalog\ViewModel\SeoConfigTopMenu</argument>
73-
</arguments>
74-
</referenceBlock>
75-
</referenceContainer>
7669
</body>
7770
</page>

app/code/Magento/Catalog/view/frontend/templates/html/topmenu.phtml

Lines changed: 0 additions & 35 deletions
This file was deleted.

dev/tests/integration/testsuite/Magento/Catalog/Block/Html/TopmenuTest.php

Lines changed: 0 additions & 42 deletions
This file was deleted.

lib/web/mage/menu.js

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ define([
1616
*/
1717
$.widget('mage.menu', $.ui.menu, {
1818
options: {
19-
useCategoryPathInUrl: false,
2019
categoryLayoutClass: 'catalog-product-view',
2120
responsive: false,
2221
expanded: false,
@@ -200,61 +199,56 @@ define([
200199
/**
201200
* Determines if the current page is a product page.
202201
* It checks the catalog product view related class in the body tag of the document.
203-
* The result is cached after the first check to optimize performance for subsequent calls.
204202
*
205203
* @return {Boolean} True if the current page is a product page, false otherwise.
206204
* @private
207205
*/
208206
_isProductPage: function () {
209-
// Cache the result if called multiple times
210-
if (this._cachedIsProductPage === undefined) {
211-
this._cachedIsProductPage = document.body.classList.contains(this.options.categoryLayoutClass);
212-
}
213-
return this._cachedIsProductPage;
207+
return document.body.classList.contains(this.options.categoryLayoutClass);
214208
},
215209

216210
/**
217-
* Sets the active state in the menu for a product page.
218-
* It first tries to determine the category URL based on the referrer or the current URL.
219-
* If 'useCategoryPathInUrl' is enabled, it uses the current URL and appends the dynamically determined category URL suffix.
220-
* Otherwise, it uses the referrer URL or the current URL with the dynamically determined category URL suffix.
221-
* If a valid category URL is found, it sets the corresponding category as active in the menu.
222-
* If no valid category URL is found, it clears the active state.
211+
* Sets the active state in the menu for a product page. Determines the category URL from either
212+
* the referrer URL or the current URL, using the URL extension to identify the category.
213+
* Sets the corresponding category as active in the menu if a valid category URL is found.
214+
* Clears the active state if no valid category URL is found or if it's not a product page.
223215
*
224-
* @param {String} currentUrl - current page URL without parameters
225-
* @return {Boolean}
216+
* @param {String} currentUrl - The current page URL without parameters.
217+
* @return {Boolean} - True if a valid category is set, false otherwise.
226218
* @private
227219
*/
228220
_setActiveMenuForProduct: function (currentUrl) {
229221
var firstCategoryUrl = this.element.find('> li a').attr('href');
230222

231-
// If no category URL is found in the menu, return false.
223+
// Return false if no category URL is found in the menu.
232224
if (!firstCategoryUrl) {
225+
this._clearActiveState();
233226
return false;
234227
}
235228

236229
var categoryUrlExtension = this._getUrlExtension(firstCategoryUrl);
237230
var categoryUrl;
231+
var isProductPage = this._isProductPage();
238232

239-
if (this.options.useCategoryPathInUrl) {
240-
// Derive the category URL from the current URL and append the dynamically determined URL extension
241-
categoryUrl = currentUrl.substring(0, currentUrl.lastIndexOf('/')) + categoryUrlExtension;
242-
} else {
243-
var referrer = new URL(document.referrer);
244-
var isProductPage = this._isProductPage();
233+
if (isProductPage) {
234+
var currentHostname = window.location.hostname;
245235

246-
if (referrer.hostname === window.location.hostname && referrer.pathname.endsWith(categoryUrlExtension) && isProductPage) {
247-
categoryUrl = referrer.href.split('?')[0];
248-
} else if (isProductPage) {
249-
categoryUrl = currentUrl.substring(0, currentUrl.lastIndexOf('/')) + categoryUrlExtension;
236+
// Check if the referrer's hostname matches the current hostname
237+
// and if the referrer's pathname ends with the category URL extension
238+
if (document.referrer.includes(currentHostname) && document.referrer.endsWith(categoryUrlExtension)) {
239+
categoryUrl = document.referrer.split('?')[0];
250240
} else {
251-
this._clearActiveState();
252-
return false;
241+
// Fallback to using the current URL
242+
categoryUrl = currentUrl.substring(0, currentUrl.lastIndexOf('/')) + categoryUrlExtension;
253243
}
244+
245+
this._setActiveMenuForCategory(categoryUrl);
246+
return true;
254247
}
255248

256-
this._setActiveMenuForCategory(categoryUrl);
257-
return true;
249+
// Clear active state if not a product page
250+
this._clearActiveState();
251+
return false;
258252
},
259253

260254
/**

0 commit comments

Comments
 (0)