Magento 2

Magento 2 Performance Drain: Unmasking and Fixing the RequireJS Duplicate Script Loading Bug

Code snippet illustrating the proposed fix for Magento's RequireJS mixins.js bug
Code snippet illustrating the proposed fix for Magento's RequireJS mixins.js bug

Critical Performance Alert: Unpacking the Duplicate RequireJS Script Loading Bug in Magento 2.4.x

As e-commerce experts focused on Magento migrations and platform optimization, we at Shopping Mover constantly monitor the pulse of the Magento community. A recent GitHub issue (Issue #40527) has brought to light a significant performance and stability concern affecting Magento Open Source and Adobe Commerce versions 2.4.7, 2.4.8, and the 2.4-develop branch. This critical bug involves the duplicate loading of JavaScript modules via RequireJS, leading to slower page loads and potential front-end errors.

The Core Problem: Unintended Double Script Loading

The issue stems from an unexpected behavior within Magento's RequireJS implementation, specifically in the lib/web/mage/requirejs/mixins.js file. The defContext.configure wrapper, designed to synchronize RequireJS contexts, inadvertently forwards the complete configuration object—including deps (dependencies) and callback—to the unbundled $ RequireJS context. This is problematic because the $ context is primarily intended for path resolution, not for loading modules.

Consequently, every module listed in a deps array within requirejs-config.js is loaded twice: once by the default _ context and again by the $ context. This results in duplicate