@@ -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