In short and in plain language
A shop uses many small pieces of code. They are called plugins. Some are only needed on one page. Still, the browser often loads them on every page.
Since Shopware 6.6 this can change. A plugin can load later. It only comes when the page really needs it.
Some plugins must be there at once. They stay as they are. One example is the help for people who have the page read out to them.
We measure before and after the change. We count how much code each page loads. Then you see what the change brings.
We check your shop and change your own plugins. We plan the work for the time after the holidays.
A Shopware shop grows with every extension: a size advisor on the product page, a voucher field in the cart, a callback form on the contact page. Each of these features brings its own JavaScript, and as long as it is imported the classic way at the top of main.js, the browser loads that code on every page, including pages that do not use it. Since Shopware 6.6 (Shopware UPGRADE-6.6) there is a built-in way out: storefront plugins can be registered with a dynamic import. They then stay out of the main bundle and live in a separate file that the PluginManager only fetches when their selector is present on the current page (Shopware docs). This article shows how to sort the own and third-party plugins of your shop, which ones should stay synchronous, how to convert them including overrides, and how to measure the amount of JavaScript per page type before and after the change.
Key takeaways
- Since Shopware 6.6 (Shopware UPGRADE-6.6), a storefront plugin can be registered with
() => import(). It then stays out of the main bundle and only loads when its selector is on the page. - The median mobile page loads 558 KB of JavaScript, and 44 percent of the JavaScript bytes delivered remain unused during page load (Web Almanac 2024). Code that a page type does not run is part of that share.
- Plugins that have to work immediately on every page stay synchronous. Shopware keeps AlertAria synchronous, for example, so that screen reader announcements do not depend on lazy loading (Shopware source code).
- If a core plugin is registered asynchronously, your own override has to use a dynamic import as well. Each JavaScript plugin can only be overridden once (Shopware docs).
- Measure per page type before and after the change: JavaScript bytes transferred, number of files and unused code. Lighthouse flags every file with more than 20 KiB of unused code (Chrome for Developers).
Why the main bundle weighs on every page
The amount of JavaScript on the web has been growing for years. In 2024, the median mobile page loaded 558 KB of JavaScript (Web Almanac 2024) and made 22 JavaScript requests for it (Web Almanac 2024). A large part of it is not needed during load at all: at the median on mobile, 206 KB or 44 percent of the JavaScript bytes delivered remain unused while the page is built (Web Almanac 2024). These are values across the entire HTTP Archive data set, not values for Shopware shops. But they show where the lever is: code that a page does not run is still transferred, decompressed, parsed and compiled.
The cost is not limited to the network. If a script is render-blocking, the browser has to download, parse, compile and evaluate it before it can proceed with the rest of the rendering work, and even asynchronously loaded code competes with other resources for bandwidth while it downloads (Chrome for Developers). Anything that runs on the main thread for longer than 50 milliseconds in one go counts as a long task (web.dev). The median mobile page has 14 such long tasks (Web Almanac 2024) and spends 2.37 seconds in them (Web Almanac 2024). In Lighthouse this shows up in Total Blocking Time, which carries 30 percent of the performance score in Lighthouse 10 (Chrome for Developers). How to attribute such blocking to individual scripts is covered in our article on Long Animation Frames.
In the Shopware storefront the problem takes its own shape. Since 6.6, Shopware no longer merges the JavaScript of extensions into a shared all.js, each app and each plugin gets its own file served by its own <script> tag (Shopware UPGRADE-6.6). The entry point of every extension remains its main.js in src/Resources/app/storefront/src (Shopware docs). Everything imported at the top of that file ends up in the main bundle of that extension. The docs say so explicitly: a plugin imported the normal way is included in the JavaScript on all pages (Shopware docs). Binding a plugin to a selector prevents it from running on pages without a matching element, but not the download.
What async means in the storefront
An asynchronous plugin differs from a synchronous one in a single line. Instead of importing the plugin file at the top of main.js, you pass PluginManager.register() an arrow function with a dynamic import, and the import path stays the same (Shopware docs). From this, the PluginManager recognizes on its own that the plugin is async. By default it is then not included in the main bundled JavaScript storefront.js and is only downloaded when its selector is found on the current page (Shopware docs).
import NavbarExtension from './navbar-extension/navbar-extension.plugin';
const PluginManager = window.PluginManager;
// synchronous: ends up in the main bundle and loads on every page
PluginManager.register('NavbarExtension', NavbarExtension, '[data-navbar-extension]');
// asynchronous: separate file, loads only when the selector is on the page
PluginManager.register(
'SizeAdvisor',
() => import('./size-advisor/size-advisor.plugin'),
'[data-size-advisor]',
);Shopware uses the mechanism itself. With 6.6, the vendor announced that the majority of its own storefront plugins would be changed to async (Shopware UPGRADE-6.6). In the main.js of Shopware 6.7.16.0, listing and filters, sliders, cart widgets, forms and the cookie dialog are registered with dynamic imports (Shopware source code). Of the plugins, the file imports only SetBrowserClass, SpeculationRules and AlertAria synchronously, and for AlertAria the reason is written right into the code: the plugin stays synchronous to prevent an unreliable load time for the screen reader (Shopware source code).
The PluginManager shows how the decision is made in the browser. On the DOMContentLoaded event, the storefront calls PluginManager.initializePlugins() (Shopware source code). The method looks for the selector of every async plugin, fetches all required files together and only then initializes the plugins (Shopware source code). If the selector is missing at registration, the whole document counts as the target, and the plugin is fetched on every page. Async without a selector therefore saves nothing, it only costs an additional request.
Context: separate entry points
main.js of every extension is such an entry point with its own file, and within that entry point the build splits the code as soon as a plugin is registered with a dynamic import (Shopware UPGRADE-6.6). Someone still has to decide on the split deliberately, while the storefront build creates the additional file for each plugin on its own.Inventory: which plugin is needed where
Every conversion starts with a list. It begins in the compiled theme: since 6.6, public/theme/<theme-hash>/js/ contains a separate subdirectory for each app and plugin, named after the technical name of the extension, holding the main bundle and the files created by dynamic imports (Shopware UPGRADE-6.6). The file sizes there show at a glance which extension sends how much code to every page. Next comes a look at the main.js of each of your own plugins: which plugins are registered, synchronously or asynchronously, and which selector are they bound to?
The second column of the list answers the question on which page types the selector appears at all. Searching the Twig templates for the data attribute shows where the plugin is bound, and a look at the source of the delivered page confirms it. For a shop, these page types are usually enough:
- Home page and shopping experiences with sliders, videos and teaser areas.
- Category and search results with listing, filters and sorting.
- Product detail page with gallery, variant switch, reviews and add-on modules such as a size advisor.
- Cart and off-canvas cart, which loads its content via Ajax.
- Checkout with addresses, shipping and payment methods.
- Customer account and forms such as contact, callback or inquiry.
This matrix yields a recommendation for every plugin. The table sums up the typical cases we come across in storefronts:
| Plugin type | Needed on | Registration | Watch out for |
|---|---|---|---|
| Header, menu, search | every page | synchronous, or async without savings | async saves no bytes here, it only delays the start |
| Live announcements for screen readers | every page | synchronous | Shopware deliberately keeps AlertAria synchronous |
| Size advisor, configurator, 3D view | product detail page | async with selector | set the data attribute only in the product page template |
| Voucher, delivery date, add-on options | cart and checkout | async with selector | off-canvas content arrives later, wait for initialization |
| Callback, appointment request, upload | individual forms | async with selector | selector in the form, not in the layout |
| Plugin without selector | every page | add a selector | without a selector, even an async plugin loads everywhere |
Third-party plugins from the Store belong on the same list, even if you cannot change their code. Their main.js sits in the vendor's package, and the vendor's build decides the registration. Here the list serves as a basis for discussion: if you can show that a module for the product page loads on every page, you have a concrete request for the vendor. Shopware recommended anyway that vendors release a new version compatible with 6.6.0 onwards (Shopware UPGRADE-6.6).
Converting to dynamic imports
The conversion itself is small, the care goes into everything around it. For each plugin that is only needed on some page types, you replace the static import at the top of main.js with the arrow function in register(). The plugin class stays as it is, but it has to be a default export (export default): when loading, the PluginManager reads exactly this export and skips the plugin with a console warning if it is missing (Shopware source code). Take the PluginManager from the window object instead of importing it. Shopware advises against the import because it can lead to unintended side effects (Shopware UPGRADE-6.6).
const PluginManager = window.PluginManager;
// product detail page
PluginManager.register('SizeAdvisor', () => import('./size-advisor/size-advisor.plugin'), '[data-size-advisor]');
// cart and off-canvas cart
PluginManager.register('CartVoucher', () => import('./cart-voucher/cart-voucher.plugin'), '[data-cart-voucher]');
// contact form
PluginManager.register('CallbackForm', () => import('./callback-form/callback-form.plugin'), '[data-callback-form]');When building with composer run build:js:storefront, the dynamic import creates an additional file for each of these plugins in the dist folder of the extension (Shopware UPGRADE-6.6). Exactly these files have to go into the release, and Shopware explicitly asks to include all additional JS files generated by dynamic imports (Shopware UPGRADE-6.6). If one is missing, the import fails in the browser: import() then returns a rejected promise, for example on an HTTP error such as 404 (MDN), and the PluginManager skips the plugin with a warning (Shopware source code). A feature then silently disappears from the page without any error page.
The same discipline applies on the template side. The data attribute belongs only in the page type where the plugin works, for example in a block of the product detail page that you extend with sw_extends (Shopware docs). If data-size-advisor accidentally sits in the footer, the PluginManager finds the selector on every page and fetches the file everywhere. Options are passed as before through an attribute following the pattern data-size-advisor-options (Shopware docs), and the async registration does not change that.
Content that enters the page after load needs special attention, such as the off-canvas cart or a form loaded via Ajax. The PluginManager detects new selectors when PluginManager.initializePlugins() runs again. Since 6.6 this method is async and returns a promise because it downloads all async plugins before initializing them (Shopware UPGRADE-6.6). Code that accesses a plugin instance right afterwards, for example via getPluginInstanceFromElement(), has to wait for that promise (Shopware UPGRADE-6.6). Custom code that used to continue synchronously with the instance is a typical place where a conversion breaks silently.
// content was inserted into the off-canvas cart via Ajax
window.PluginManager.initializePlugins().then(() => {
const instance = window.PluginManager.getPluginInstanceFromElement(element, 'CartVoucher');
// access only once the lazy-loaded plugin is initialized
instance?.refresh();
});Check before going live
Bringing overrides and extensions along
Many shops adapt core plugins instead of writing new ones: a modified cookie dialog, an extended listing, a custom quantity selector. Such changes go through PluginManager.override(). If the core plugin is registered asynchronously, the override has to use a dynamic import as well (Shopware docs). Since Shopware set out to change the majority of its storefront plugins to async (Shopware UPGRADE-6.6), this affects many overrides that date from before 6.6.
const PluginManager = window.PluginManager;
// Listing is registered async in the core, so the override is async too
PluginManager.override(
'Listing',
() => import('./listing-extension/my-listing-extension.plugin'),
'[data-listing]',
);Two limits belong in the inventory. Each JavaScript plugin can only be overridden once, and if two extensions try to override the same plugin, only the last one loaded takes effect (Shopware docs). In addition, anyone who fetches a third-party plugin class via PluginManager.getPlugin(...).get('class') to inherit from it does not get the class for an async plugin before it has loaded: the PluginManager treats anything without a findable prototype as async and only holds the import function until then (Shopware source code). Importing the parent class directly in the override file, as the docs show for core plugins, is the safer route (Shopware docs). Because that file is itself lazy-loaded, the imported parent class usually stays out of the main bundle as well.
Sometimes an override is not needed at all. Shopware points out that an event can sometimes be used instead of overriding a plugin (Shopware docs). An event listener does not depend on whether the core plugin arrives synchronously or asynchronously, and it does not run into the limit of one override per plugin. For extensions that only need to react to a result, for example after adding a product to the cart, it is the more robust route.
Measuring JavaScript per page type
Whether the conversion pays off is decided by measurement, not by the number of converted plugins. Measure per page type, because the effect differs by page type: after the conversion, a product page still loads the size advisor, the home page no longer does. For a first recording, the DevTools network panel filtered to JavaScript with the cache disabled is enough. The Coverage panel also shows, per file, how many bytes are used during load and interaction and how many are not (Chrome for Developers).
Lighthouse offers two audits that respond directly. The unused JavaScript audit flags every file with more than 20 KiB of unused code (Chrome for Developers). The execution time audit warns when JavaScript runs for longer than 2 seconds (Chrome for Developers) and fails at more than 3.5 seconds (Chrome for Developers). Both are lab values from a single run. Field data shows what visitors experience: Interaction to Next Paint counts as good at 200 milliseconds or less (web.dev), measured at the 75th percentile of page loads (web.dev).
To keep the values before and after the conversion comparable, we define the measurement protocol up front:
- Define the page types: home page, a typical category, a product detail page with all add-on modules, cart, checkout and a form page.
- Record the conditions: device or throttling, browser version, logged in or as a guest, cache cleared.
- Note per page type: JavaScript bytes transferred, number of JavaScript files, unused bytes according to Coverage, Total Blocking Time and long tasks.
- Record several runs and use the median so that no single outlier carries the decision.
- After the conversion, measure the same page types under the same conditions and report the difference per page type.
- Cross-check field data as soon as enough real visits are available after go-live.
No promise about the gain can be made in advance. How much disappears depends on how much page-specific code sits synchronously in the main bundle today. How to read the values directly in the browser is covered in our article on live metrics in DevTools, and a JavaScript budget sets the frame for a lasting upper limit. What lies behind the metrics is explained on our page about Core Web Vitals.
December: measure now, convert later
Limits and side effects
Asynchronous also means later. A lazy-loaded plugin only starts once its file has arrived. At page start, initializePlugins() first fetches all required async files and only then initializes the plugins, including the synchronously registered ones (Shopware source code). For controls in the visible area that have to respond immediately, this can open a short window in which a click does nothing yet. Test the buy button and its feedback specifically after the conversion, and our article on the buy button and INP shows what matters there.
Every lazy-loaded file is also a request of its own. With 22 JavaScript requests on the median mobile page (Web Almanac 2024), it does not pay to split out every small plugin that is needed on most page types anyway. Make async the plugins whose selector is missing on many pages, and keep in the main bundle what appears almost everywhere. The technique is still rare on the web: dynamic imports were found on 3.70 percent of mobile pages in 2024 (Web Almanac 2024).
Technically, the dynamic import is no longer an obstacle, import() has been available across browsers since January 2020 (MDN). If you are reviewing the browser target of your build anyway, our article on browser targets with Baseline provides the matching frame. Scripts that are not storefront plugins at all, such as embedded services of other providers, are not covered by this mechanism. Separate rules apply to them, which we describe in trimming third-party scripts.
How we support the conversion
In our Shopware performance optimization, the conversion starts with the inventory: we list the bundles of all extensions in the compiled theme, map every registered plugin to its page types and measure the amount of JavaScript per page type. From this we derive a plan for which of your own plugins become async, which stay synchronous and which overrides have to be brought along. For extensions from the Store, we prepare the findings so that you can present them to the vendor. After the conversion we measure the same page types again and report the change per page type.
If you first want to know whether JavaScript is the biggest item in your shop at all, start with a technical analysis. It also shows whether other measures of front-end optimization bring more, and it complements the built-in tools we described in Shopware performance with HTTP cache and OpenSearch. For a first assessment, reach us through the contact form.
Sources
main.js and plugin.manager.js); HTTP Archive Web Almanac 2024 (JavaScript chapter); Chrome for Developers (Lighthouse: Remove unused JavaScript, Reduce JavaScript execution time, Lighthouse performance scoring; DevTools: Coverage); web.dev (Optimize long tasks, Interaction to Next Paint); MDN Web Docs (import()).register() receives an arrow function with a dynamic import instead of the class (Shopware docs). The import path stays the same, and the plugin class does not have to be rewritten, it only needs a default export.PluginManager.initializePlugins(), for example after the off-canvas cart has inserted its content. Since 6.6, the method is async and returns a promise (Shopware UPGRADE-6.6). Code that accesses the new plugin instance directly waits for that promise.dist folder of the package.Related Articles
Snowfall in Your Shop: Holiday Decor Without Slowdowns
Snowfall scripts, glitter borders and GIF banners slow down weak smartphones. How to keep holiday decorations in your shop smooth and your INP stable.
Browser Targets with Baseline: Drop Polyfills Nobody Needs
How much polyfill weight does your shop ship for old browsers? Find your build target, set a Baseline target, measure legacy code and check visitor data.
Images in the browser: what WordPress 7.1 changes on upload
WordPress 7.1 resizes and encodes images in the browser during upload. What that changes for files, formats, GIFs and server load, and how to measure it.