Skip to content

Commit 7edd33b

Browse files
committed
Remove active category in the cache key
- Update js logic;
1 parent d7b1dd4 commit 7edd33b

File tree

1 file changed

+52
-23
lines changed

1 file changed

+52
-23
lines changed

lib/web/mage/menu.js

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ define([
122122
var currentUrl = window.location.href.split('?')[0];
123123

124124
if (!this._setActiveMenuForCategory(currentUrl)) {
125-
this._setActiveMenuForProduct(currentUrl);
125+
this._setActiveMenuForProduct();
126126
}
127127
},
128128

@@ -135,6 +135,9 @@ define([
135135
* @private
136136
*/
137137
_setActiveMenuForCategory: function (url) {
138+
// Clear active state from all categories
139+
this._clearActiveState();
140+
138141
var activeCategoryLink = this.element.find('a[href="' + url + '"]'),
139142
classes,
140143
classNav;
@@ -156,6 +159,19 @@ define([
156159
return true;
157160
},
158161

162+
/**
163+
* Clears the active state from all menu items within the navigation element.
164+
* It removes 'active' and 'has-active' classes from all list items (li elements),
165+
* which are used to indicate the currently selected or parent of a selected item.
166+
* This method is typically used to reset the menu's state before applying new active states.
167+
*
168+
* @return void
169+
* @private
170+
*/
171+
_clearActiveState: function () {
172+
this.element.find('li').removeClass('active has-active');
173+
},
174+
159175
/**
160176
* Sets 'has-active' CSS class to all parent categories which have part of provided class in childClassName
161177
*
@@ -182,33 +198,46 @@ define([
182198
},
183199

184200
/**
185-
* Tries to retrieve category URL from current URL and mark this category as active
186-
* @see _setActiveMenuForCategory(url)
187-
*
188-
* @example
189-
* currentUrl - http://magento.com/category1/category12/product.html,
190-
* category URLs has extensions .phtml - http://magento.com/category1.phtml
191-
* method sets active category which has URL http://magento.com/category1/category12.phtml
201+
* Activates the category in the menu corresponding to the current product page.
202+
* It resolves the category URL based on the referrer (if it's from the same domain),
203+
* and then sets this category as active in the menu.
204+
* If no suitable referrer URL is found, no category is set as active.
192205
*
193-
* @param {String} currentUrl - current page URL without parameters
194206
* @return void
195207
* @private
196208
*/
197-
_setActiveMenuForProduct: function (currentUrl) {
198-
var categoryUrlExtension,
199-
lastUrlSection,
200-
possibleCategoryUrl,
201-
//retrieve first category URL to know what extension is used for category URLs
202-
firstCategoryUrl = this.element.find('> li a').attr('href');
203-
204-
if (firstCategoryUrl) {
205-
lastUrlSection = firstCategoryUrl.substr(firstCategoryUrl.lastIndexOf('/'));
206-
categoryUrlExtension = lastUrlSection.lastIndexOf('.') !== -1 ?
207-
lastUrlSection.substr(lastUrlSection.lastIndexOf('.')) : '';
208-
209-
possibleCategoryUrl = currentUrl.substr(0, currentUrl.lastIndexOf('/')) + categoryUrlExtension;
210-
this._setActiveMenuForCategory(possibleCategoryUrl);
209+
_setActiveMenuForProduct: function () {
210+
var categoryUrl = this._resolveCategoryUrl();
211+
212+
if (categoryUrl) {
213+
this._setActiveMenuForCategory(categoryUrl);
214+
}
215+
},
216+
217+
/**
218+
* Resolves the category URL based on the referrer URL.
219+
* Checks if the referrer URL belongs to the same domain and is a likely category page.
220+
* If so, returns the referrer URL after stripping any query parameters.
221+
* Returns null if no suitable referrer is found or if it cannot be determined to be a category page.
222+
*
223+
* @return {String|null} The URL of the category, or null if it cannot be determined.
224+
* @private
225+
*/
226+
_resolveCategoryUrl: function () {
227+
var categoryUrl = document.referrer;
228+
229+
// Ensure the referrer is from the same domain
230+
if (categoryUrl && new URL(categoryUrl).hostname === window.location.hostname) {
231+
// Remove any query parameters from the referrer URL
232+
var queryParamIndex = categoryUrl.indexOf('?');
233+
if (queryParamIndex > 0) {
234+
categoryUrl = categoryUrl.substring(0, queryParamIndex);
235+
}
236+
return categoryUrl;
211237
}
238+
239+
// Fallback or default behavior if no suitable referrer is found
240+
return null;
212241
},
213242

214243
/**

0 commit comments

Comments
 (0)