<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>lighthouse Archives - gj</title>
	<atom:link href="https://blog.gaiterjones.com/tag/lighthouse/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.gaiterjones.com/tag/lighthouse/</link>
	<description>gaiterjones</description>
	<lastBuildDate>Fri, 15 Jan 2021 17:58:54 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.4.3</generator>
	<item>
		<title>Magento 2 Javascript Bundling with Magepack</title>
		<link>https://blog.gaiterjones.com/magento-2-javascript-bundling-with-magepack/</link>
					<comments>https://blog.gaiterjones.com/magento-2-javascript-bundling-with-magepack/#comments</comments>
		
		<dc:creator><![CDATA[PAJ]]></dc:creator>
		<pubDate>Sun, 03 May 2020 19:51:28 +0000</pubDate>
				<category><![CDATA[Javascript Bundling]]></category>
		<category><![CDATA[Magento2]]></category>
		<category><![CDATA[bundling]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[lighthouse]]></category>
		<category><![CDATA[magento2]]></category>
		<category><![CDATA[magepack]]></category>
		<category><![CDATA[pagespeed]]></category>
		<guid isPermaLink="false">https://blog.gaiterjones.com/?p=2171</guid>

					<description><![CDATA[Magento 2 PageSpeed (Lighthouse) performance audit results for mobile and desktop are notoriously bad. Imagine you have worked for months on a new Magento 2 eCommerce store, followed best practices...<a class="more-link" href="https://blog.gaiterjones.com/magento-2-javascript-bundling-with-magepack/" title="Continue reading">Continue reading</a>]]></description>
										<content:encoded><![CDATA[<p>Magento 2 PageSpeed (<a href="https://developers.google.com/web/tools/lighthouse">Lighthouse</a>) performance audit results for mobile and desktop are notoriously bad. Imagine you have worked for months on a new Magento 2 eCommerce store, followed best practices for setup and optimisation, the store seems to be running fine but the first time you run a Lighthouse report you see a performance score like this:<img fetchpriority="high" decoding="async" src="https://blog.gaiterjones.com/wp-content/uploads/2020/05/blog-pagespeed_1.jpg" alt="" width="250" height="250" class="aligncenter size-full wp-image-2172" />There are a lot of factors that can affect the Lighthouse performance results for any website but for Magento 2 a big performance killer is the sheer amount of external resources required to render a page whether it be a product page, cms page or category page. Some of these render blocking resources such as Javascript or CSS can cause significant delays in page loading and affect performance. You will see this type of performance problem identified in Lighthouse as &#8220;Eliminate render-blocking resources&#8221;.</p>
<p>Magento 2 uses the RequireJs <span>javascript module system to load Javascript source code <em>required</em> for each Magento 2 page. If you have a lot of custom features with modules implementing additional Magento 2 Javascript<em> mixins </em>the number of Javascript resources in addition to the core javascript code required by Magento will increase and adversely affect page loading performance. As an example, here is the network console log from a really simple product page from my development site, you can see that there are 194 requests for Javascript resources!</span></p>
<figure id="attachment_2196" aria-describedby="caption-attachment-2196" style="width: 473px" class="wp-caption aligncenter"><img decoding="async" src="https://blog.gaiterjones.com/wp-content/uploads/2020/05/blog-pagespeed_8.png" alt="Simple Magento 2 product page loads 194 Javascript files" width="473" height="361" class="wp-image-2196" /><figcaption id="caption-attachment-2196" class="wp-caption-text">Simple Magento 2 product page loads 194 Javascript files</figcaption></figure>
<p>There are various ways to try and reduce the performance impact of loading lots of Javascript including using http2 which is great at handling small file requests quickly or minifying the Javascript source to reduce it&#8217;s size but the most effective way of optimising Javascript loading is to use bundling.</p>
<p style="text-align: center;"><em>Javascript bundling is a technique that combines or bundles multiple files in order to reduce the number of HTTP requests that are required to load a page.<br />
</em></p>
<p>Magento 2 has a built in javascript bundler that is extremely ineffective! Users report it creating a <a href="https://github.com/magento/magento2/issues/4506">huge multi megabyte javascript</a> file that decreases performance instead of improving it. You will actually see the recommendation <em>not</em> to use the built in Magento 2 bundling referenced in Lighthouse reports &#8211; &#8220;Disable Magento&#8217;s built-in <a rel="noopener noreferrer" target="_blank" href="https://devdocs.magento.com/guides/v2.3/frontend-dev-guide/themes/js-bundling.html">JavaScript bundling and minification</a>, and consider using <a rel="noopener noreferrer" target="_blank" href="https://github.com/magento/baler/">baler</a> instead.&#8221;</p>
<p><a href="https://github.com/magento/baler/">Baler</a> mentioned here is an AMD (Asynchronous Module Definition) module bundler / preloader for Magento 2 stores. You will find a lot of Magento 2 js bundling guides that recommend using Baler but for the average developer (like me) or Magento 2 merchant the bundling process with Baler can be quite complex and daunting. There is however a new Magento 2 js bundler available that is much easier to use.</p>
<h1>MageSuite Magepack</h1>
<p>The <a href="https://github.com/magesuite/magepack">Magepack</a> from MageSuite is a &#8220;<em>Next generation Magento 2 advanced JavaScript bundler</em>&#8221; it&#8217;s pretty easy to implement and as of version 2.0 the results it achieves are very impressive.</p>
<ul>
<li>Up to 91 points mobile score in Google Lighthouse.</li>
<li>Up to 98% reduction in JavaScript file requests.</li>
<li>Up to 44% reduction in transferred JavaScript size.</li>
<li>Up to 75% reduction in total load time.</li>
<li>Works with Magento&#8217;s JavaScript minification and merging enabled.</li>
<li>Uses custom solution (inspired by Baler)</li>
</ul>
<p><span>I installed Magepack on my Magento 2 development site in May 2020 and achieved a 100 desktop performance score with PageSpeed  &#8211;</span></p>
<p><a href="https://developers.google.com/speed/pagespeed/insights/?url=http%3A%2F%2Fmagento2.gaiterjones.com%2Fen%2Faffirm-water-bottle.html&amp;tab=desktop"><img decoding="async" src="https://blog.gaiterjones.com/wp-content/uploads/2020/05/blog-pagespeed_2.jpg" alt="" width="600" height="209" class="aligncenter wp-image-2175 size-full" /></a>This is a simple product page, using the default Luma theme and I am also using Nginx as a container proxy running the PageSpeed module, so you probably won&#8217;t achieve this kind of result on a real world product page but you will see a <strong>huge</strong> improvement. Check the results yourself <a href="https://developers.google.com/speed/pagespeed/insights/?url=http%3A%2F%2Fmagento2.gaiterjones.com%2Fen%2Faffirm-water-bottle.html&amp;tab=desktop">here</a>.</p>
<p>Let&#8217;s look at how to setup and install MagePack for Magento 2.3.x / 2.4.x.</p>
<h2>Setup and install Magepack for Magento 2.3.x and 2.4.x</h2>
<p>MagePack consists of a NodeJS bundler app and a Magento 2 module. The bundler app runs on Node JS v10 or higher. I&#8217;m running MagePack in my Docker Magento 2 php container, it&#8217;s running Ubuntu server 20.04LTS and I&#8217;ve tested Magepack with Magento 2.3.3, 2.3.5 and 2.4.1. To install Node JS simply run</p>
<pre class="brush: bash; title: ; notranslate">
curl -sL https://deb.nodesource.com/setup_10.x | bash -
apt-get install -y nodejs
</pre>
<p>Ubuntu will probably need some more dependencies before MagePack will install</p>
<pre class="brush: bash; title: ; notranslate">apt-get install gconf-service libasound2 libatk1.0-0 libatk-bridge2.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget</pre>
<p>Finally to install the NodeJS MagePack app itself run<br />
<code>npm install -g magepack  --unsafe-perm=true --allow-root</code></p>
<figure id="attachment_2180" aria-describedby="caption-attachment-2180" style="width: 626px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" src="https://blog.gaiterjones.com/wp-content/uploads/2020/05/blog-magepack-screenshot.jpg" alt="Installing Magepack NodeJS app and dependencies" width="626" height="300" class="wp-image-2180" /><figcaption id="caption-attachment-2180" class="wp-caption-text">Installing Magepack NodeJS app and dependencies</figcaption></figure>
<p>You will see that Magepack pulls down Chromium &#8211; it needs a web browser to analyse your Magento 2 site, most of the dependencies installed earlier are required for Chromium.</p>
<p>With Magepack installed, we now need to install the <a href="https://github.com/magesuite/magepack-magento">Magepack Magento 2 module</a></p>
<p><code>composer require creativestyle/magesuite-magepack</code></p>
<p>Next, depending on the version of Magento 2 you are running you might need to install some patches.</p>
<ul>
<li>For Magento 2.3.3 and earlier 7 patches are required</li>
<li>For Magento 2.3.4 and 2.3.5 1 patch is required</li>
<li><strong>For Magento 2.4.1 no patches are required (tested November 2020)</strong></li>
</ul>
<p>The most painless way of patching Magento 2 is to use <a href="https://github.com/cweagans/composer-patches">Cweagans/Composer-Patches</a></p>
<p><code>composer require cweagans/composer-patches</code></p>
<p>You will find all the patches you need here : https://github.com/integer-net/magento2-requirejs-bundling</p>
<p>In your Magento 2 installation folder create a <em>patches</em> folder copy the patches into it and edit your Magento 2 composer.json file to include the following composer extra patches config.</p>
<p>If you see the error <code>Evaluation failed: ReferenceError: BASE_URL is not defined</code> running magepack generate with 2.3.5+ or 2.4.x check your CSP configuration, or try disabling the CSP module temporarily &#8211; errors reported by CSP may create a magepack error.</p>
<span class="collapseomatic collapse" id="id660c357226f13"  tabindex="0" title="2.3.3 composer extra patches config"    >2.3.3 composer extra patches config</span><div id="target-id660c357226f13" class="collapseomatic_content ">
[text]
    &quot;extra&quot;: {<br />
            &quot;magento-force&quot;: &quot;override&quot;,<br />
            &quot;composer-exit-on-patch-failure&quot;: true,<br />
            &quot;patches&quot;: {<br />
                &quot;magento/magento2-base&quot;: {<br />
                    &quot;[Performance] Fix missing shims and phtml files with mage-init directives (https://github.com/magento/magento2/commit/db43c11c6830465b764ede32abb7262258e5f574)&quot;: &quot;patches/composer/M233/github-pr-4721-base.diff&quot;,<br />
                    &quot;Refactor JavaScript mixins module https://github.com/magento/magento2/pull/25587&quot;: &quot;patches/composer/M233/github-pr-25587-base.diff&quot;<br />
                },<br />
                &quot;magento/module-braintree&quot;: {<br />
                    &quot;[Performance] Fix missing shims and phtml files with mage-init directives (https://github.com/magento/magento2/commit/db43c11c6830465b764ede32abb7262258e5f574)&quot;: &quot;patches/composer/M233/github-pr-4721-braintree.diff&quot;<br />
                },<br />
                &quot;magento/module-catalog&quot;: {<br />
                    &quot;[Performance] Fix missing shims and phtml files with mage-init directives (https://github.com/magento/magento2/commit/db43c11c6830465b764ede32abb7262258e5f574)&quot;: &quot;patches/composer/M233/github-pr-4721-catalog.diff&quot;<br />
                },<br />
                &quot;magento/module-customer&quot;: {<br />
                    &quot;[Performance] Fix missing shims and phtml files with mage-init directives (https://github.com/magento/magento2/commit/db43c11c6830465b764ede32abb7262258e5f574)&quot;: &quot;patches/composer/M233/github-pr-4721-customer.diff&quot;<br />
                },<br />
                &quot;magento/module-msrp&quot;: {<br />
                    &quot;[Performance] Fix missing shims and phtml files with mage-init directives (https://github.com/magento/magento2/commit/db43c11c6830465b764ede32abb7262258e5f574)&quot;: &quot;patches/composer/M233/github-pr-4721-msrp.diff&quot;<br />
                },<br />
                &quot;magento/module-paypal&quot;: {<br />
                    &quot;[Performance] Fix missing shims and phtml files with mage-init directives (https://github.com/magento/magento2/commit/db43c11c6830465b764ede32abb7262258e5f574)&quot;: &quot;patches/composer/M233/github-pr-4721-paypal.diff&quot;<br />
                },<br />
                &quot;magento/module-theme&quot;: {<br />
                    &quot;[Performance] Fix missing shims and phtml files with mage-init directives (https://github.com/magento/magento2/commit/db43c11c6830465b764ede32abb7262258e5f574)&quot;: &quot;patches/composer/M233/github-pr-4721-theme.diff&quot;,<br />
                    &quot;fix_baler_jquery_cookie&quot;: &quot;https://gist.github.com/tdgroot/f95c398c565d9bbb83e0a650cdf67617/raw/69ee2d001ff509d25d1875743e417d914e20fd85/fix_baler_jquery_cookie.patch&quot;<br />
                }<br />
            }<br />
        },<br />
[/text]
</div>
<span class="collapseomatic collapse" id="id660c357226f59"  tabindex="0" title="2.3.4, 2.3.5 composer extra patches config"    >2.3.4, 2.3.5 composer extra patches config</span><div id="target-id660c357226f59" class="collapseomatic_content ">
[text]
    &quot;extra&quot;: {<br />
            &quot;magento-force&quot;: &quot;override&quot;,<br />
            &quot;composer-exit-on-patch-failure&quot;: true,<br />
            &quot;patches&quot;: {<br />
                &quot;magento/magento2-base&quot;: {<br />
                    &quot;Refactor JavaScript mixins module https://github.com/magento/magento2/pull/25587&quot;: &quot;patches/composer/M234/github-pr-25587-base.diff&quot;<br />
                }<br />
            }<br />
        },<br />
[/text]
</div>
<p>Now run <code>composer update</code> Magento 2 will be patched and we are good to go.</p>
<h2>Let’s get ready to bundle</h2>
<p>Magepack needs to analyse pages from your Magento 2 store to determine the Javascript files your store is using and how they can be bundled. It saves this information in a configuration file called <em>magepack.config.js</em>. The magepack config file is generated by analysing three different type of pages from your Magento 2 store, a cms page i.e. the home page, a category page and a product page. This is done using the <code>magepack generate</code> command and supplying three store urls.</p>
<div>
<pre class="brush: bash; title: ; notranslate">
magepack generate --cms-url=&quot;http://magento2.gaiterjones.com/&quot; --category-url=&quot;http://magento2.gaiterjones.com/en/buy-x-get-y.html&quot; --product-url=&quot;http://magento2.gaiterjones.com/en/affirm-water-bottle.html&quot;
</pre>
<p>Run this command in the root folder of your Magento 2 installation to create the magepack.config.js file. It&#8217;s worth noting that you could run this generate command from any system, and just copy the generated config file to your Magento 2 server.</p>
<p>If you take a look at magepack.config.js you will see it contains references to all the javascript required to load Magento pages. Below is an example from a product page.</p>
<span class="collapseomatic collapse" id="id660c357226f86"  tabindex="0" title="example product section from magepack.config.js"    >example product section from magepack.config.js</span><div id="target-id660c357226f86" class="collapseomatic_content ">
[code language=&#8221;javascript&#8221;]
    name: &#8216;product&#8217;,<br />
    modules: {<br />
      &#8216;Magento_Catalog/js/price-utils&#8217;: &#8216;Magento_Catalog/js/price-utils&#8217;,<br />
      &#8216;Magento_Catalog/js/price-box&#8217;: &#8216;Magento_Catalog/js/price-box&#8217;,<br />
      &#8216;Magento_Wishlist/js/add-to-wishlist&#8217;: &#8216;Magento_Wishlist/js/add-to-wishlist&#8217;,<br />
      &#8216;Magento_Cookie/js/require-cookie&#8217;: &#8216;Magento_Cookie/js/require-cookie&#8217;,<br />
      &#8216;Magento_Swatches/js/configurable-customer-data&#8217;: &#8216;Magento_Swatches/js/configurable-customer-data&#8217;,<br />
      &#8216;Magento_Review/js/error-placement&#8217;: &#8216;Magento_Review/js/error-placement&#8217;,<br />
      &#8216;Magento_Review/js/process-reviews&#8217;: &#8216;Magento_Review/js/process-reviews&#8217;,<br />
      &#8216;Elgentos_LargeConfigProducts/js/swatch-renderer-mixin&#8217;: &#8216;Elgentos_LargeConfigProducts/js/swatch-renderer-mixin&#8217;,<br />
      &#8216;text!Magento_Theme/templates/breadcrumbs.html&#8217;: &#8216;Magento_Theme/templates/breadcrumbs.html&#8217;,<br />
      &#8216;magnifier/magnifier&#8217;: &#8216;magnifier/magnifier&#8217;,<br />
      &#8216;magnifier/magnify&#8217;: &#8216;magnifier/magnify&#8217;,<br />
      &#8216;Magento_Catalog/js/gallery&#8217;: &#8216;Magento_Catalog/js/gallery&#8217;,<br />
      &#8216;Magento_ProductVideo/js/load-player&#8217;: &#8216;Magento_ProductVideo/js/load-player&#8217;,<br />
      &#8216;Magento_ProductVideo/js/fotorama-add-video-events&#8217;: &#8216;Magento_ProductVideo/js/fotorama-add-video-events&#8217;,<br />
      &#8216;Magento_Theme/js/model/breadcrumb-list&#8217;: &#8216;Magento_Theme/js/model/breadcrumb-list&#8217;,<br />
      &#8216;Magento_Theme/js/view/breadcrumbs&#8217;: &#8216;Magento_Theme/js/view/breadcrumbs&#8217;,<br />
      &#8216;Magento_Theme/js/view/add-home-breadcrumb&#8217;: &#8216;Magento_Theme/js/view/add-home-breadcrumb&#8217;,<br />
      &#8216;Magento_Catalog/js/product/breadcrumbs&#8217;: &#8216;Magento_Catalog/js/product/breadcrumbs&#8217;,<br />
      &#8216;jquery/jquery.parsequery&#8217;: &#8216;jquery/jquery.parsequery&#8217;,<br />
      &#8216;Magento_ConfigurableProduct/js/options-updater&#8217;: &#8216;Magento_ConfigurableProduct/js/options-updater&#8217;,<br />
      &#8216;Magento_Review/js/validate-review&#8217;: &#8216;Magento_Review/js/validate-review&#8217;,<br />
      &#8216;Magento_Swatches/js/swatch-renderer&#8217;: &#8216;Magento_Swatches/js/swatch-renderer&#8217;,<br />
      &#8216;Magento_Catalog/product/view/validation&#8217;: &#8216;Magento_Catalog/product/view/validation&#8217;,<br />
      &#8216;Magento_Catalog/js/product/view/product-ids&#8217;: &#8216;Magento_Catalog/js/product/view/product-ids&#8217;,<br />
      &#8216;Magento_Catalog/js/product/view/product-ids-resolver&#8217;: &#8216;Magento_Catalog/js/product/view/product-ids-resolver&#8217;,<br />
      &#8216;Magento_Catalog/js/catalog-add-to-cart&#8217;: &#8216;Magento_Catalog/js/catalog-add-to-cart&#8217;,<br />
      &#8216;Magento_Catalog/js/validate-product&#8217;: &#8216;Magento_Catalog/js/validate-product&#8217;,<br />
      &#8216;Magento_Catalog/js/product/view/provider&#8217;: &#8216;Magento_Catalog/js/product/view/provider&#8217;,<br />
      &#8216;text!mage/gallery/gallery.html&#8217;: &#8216;mage/gallery/gallery.html&#8217;,<br />
      &#8216;text!Magento_InstantPurchase/template/confirmation.html&#8217;: &#8216;Magento_InstantPurchase/template/confirmation.html&#8217;,<br />
      &#8216;Magento_InstantPurchase/js/view/instant-purchase&#8217;: &#8216;Magento_InstantPurchase/js/view/instant-purchase&#8217;,<br />
      &#8216;Magento_Review/js/view/review&#8217;: &#8216;Magento_Review/js/view/review&#8217;,<br />
      &#8216;fotorama/fotorama&#8217;: &#8216;fotorama/fotorama&#8217;,<br />
      &#8216;mage/gallery/gallery&#8217;: &#8216;mage/gallery/gallery&#8217;,<br />
      &#8216;text!Magento_InstantPurchase/template/instant-purchase.html&#8217;: &#8216;Magento_InstantPurchase/template/instant-purchase.html&#8217;<br />
    }<br />
  },<br />
[/code]
</div>
<p>All that remains now is for us to create the bundle files and deploy them for all our store views and themes. This is simply done with the <code>magepack bundle</code> command which you can execute from the Magento installation root folder. If you are running in development mode, deploy frontend static files first.</p>
<p><code>magepack bundle</code></p>
<figure id="attachment_2194" aria-describedby="caption-attachment-2194" style="width: 600px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" src="https://blog.gaiterjones.com/wp-content/uploads/2020/05/blog-pagespeed_5.jpg" alt="Magepacl bundle command" width="600" height="231" class="wp-image-2194 size-full" /><figcaption id="caption-attachment-2194" class="wp-caption-text">Magepack bundle command</figcaption></figure>
<p>Finally enable Magepack Javascript bundling in admin :</p>
<p style="text-align: center;"><strong>Stores &#8211; Configuration &#8211; Advanced &#8211; Developer &#8211; Javascript Settings</strong></p>
<figure id="attachment_2193" aria-describedby="caption-attachment-2193" style="width: 600px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" src="https://blog.gaiterjones.com/wp-content/uploads/2020/05/blog-pagespeed_4.jpg" alt="Enable Magepack bundling in admin" width="600" height="201" class="wp-image-2193 size-full" /><figcaption id="caption-attachment-2193" class="wp-caption-text">Enable Magepack bundling in admin</figcaption></figure>
<p>If you are in production mode these options will be hidden in admin. To enable Magepack Javascript bundling from the command line use</p>
<p><code>bin/magento config:set dev/js/enable_magepack_js_bundling 1</code></p>
<p>Note that you should also enable the other Javascript optimisation options here including minfy javascript files and move js code to the bottom of the page &#8211; but <strong>don&#8217;t</strong> enable the default bundling!</p>
<p>MagePack Javascript bundling should now be enabled. To check it&#8217;s working go to a Magento 2 product page and look at the source code, do a search for &#8220;bundle&#8221; and you should see the magepack javascript bundles</p>
<p><img loading="lazy" decoding="async" src="https://blog.gaiterjones.com/wp-content/uploads/2020/05/blog-pagespeed_6.jpg" alt="" width="1003" height="158" class="aligncenter wp-image-2195" /></p>
<p>Now refresh the page and have a look at your network log</p>
<figure id="attachment_2197" aria-describedby="caption-attachment-2197" style="width: 447px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" src="https://blog.gaiterjones.com/wp-content/uploads/2020/05/blog-pagespeed_9.png" alt="After bundling there are only 7 js requests on the product page" width="447" height="269" class="wp-image-2197" /><figcaption id="caption-attachment-2197" class="wp-caption-text">After bundling there are only 7 js requests on the product page</figcaption></figure>
<p>Instead of loading 194 Javascript files, the product page now loads 7, Magepack has bundled all the Javascript into two main bundle files.</p>
<p>I guess it&#8217;s now time to look at the PageSpeed Lighthouse performance reports for your optimised Magento 2 pages. If you are using the Chrome browser simply run a Lighthouse report from the DevTools page. You can also use Googles PageSpeed insights tool at <a href="https://developers.google.com/speed/pagespeed/insights/">https://developers.google.com/speed/pagespeed/insights/</a></p>
<p>This is the improvement I saw in a live production Magento 2 site</p>
<figure id="attachment_2224" aria-describedby="caption-attachment-2224" style="width: 800px" class="wp-caption aligncenter"><img loading="lazy" decoding="async" src="https://blog.gaiterjones.com/wp-content/uploads/2020/05/blog-pagespeed_12.jpg" alt="Performance results before and after bundling" width="800" height="350" class="wp-image-2224 size-full" /><figcaption id="caption-attachment-2224" class="wp-caption-text">Performance results before and after bundling</figcaption></figure>
<p>If you don’t see a big improvement remember there are a lot of other factors taken into Lighthouse performance reports. Work through the report and try to find out where you can make further improvements.</p>
<h2>Deployment in production</h2>
<p>Whenever you flush your sites static files you will need to remember to run <code>magepack bundle</code> again. In production mode you should add this to your deployment process</p>
<pre class="brush: bash; title: ; notranslate">
bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magento setup:static-content:deploy en_GB --area adminhtml
bin/magento setup:static-content:deploy en_GB --area frontend --theme MY/Theme -f
magepack bundle
bin/magento cache:clean
</pre>
<p><strong>Top Tip</strong> &#8211; if for any reason you want to generate the magepack.config.js bundle config again remember to disable the Magepack module first!</p>
<h2>Testing and Troubleshooting</h2>
<p>You should test your store thoroughly to make sure there are no Javascript problems caused by the bundling process. Magepack cannot always 100% bundle all the Javascript required by some pages. Check your web browser console for errors. If you find some features of your store are not working, try and identify if the code was included in the magepack.config.js file. Try removing the code from the bundle and test again.</p>
<h2>Selective Bundling</h2>
<p>If you do not want to bundle at checkout, or any other specific pages take a look at the isEnabled method in the Block\BundlesLoader class. This method determines whether the magepack module is enabled and if bundling should be activated. Simply detect any page here i.e. checkout pages and return false to disable bundling at checkout.</p>
<pre class="brush: php; title: ; notranslate">

        \Magento\Framework\App\Request\Http $request,
        ....
        $this-&gt;request = $request;
        ....
public function isEnabled()
{
        if ($this-&gt;request-&gt;getFullActionName() == 'checkout_index_index') {

            // disable for checkout
            //
            return false;
        }

...


</pre>
<h2>Magepack for Magento Cloud</h2>
<p>You need to have magepack.config in repo, add this to magento.app.yaml:</p>
<pre class="brush: bash; title: ; notranslate">
echo &quot;\n================================== Install and configure Magepack - Start ===============================&quot;
unset NPM_CONFIG_PREFIX
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.35.1/install.sh | bash
export NVM_DIR=&quot;$HOME/.nvm&quot;
&#x5B; -s &quot;$NVM_DIR/nvm.sh&quot;  ] &amp;&amp; \. &quot;$NVM_DIR/nvm.sh&quot;
nvm install --lts=dubnium
npm install -g magepack
echo &quot;\n================================== create bundle ===============================&quot;
magepack bundle
echo &quot;\n================================== Install and configure Magepack - END =================================&quot;
</pre>
<p><em>Thanks to Solteq for this information.</em></p>
<p>Magepack is pretty new with updates being made regularly, be sure to check out the projects GitHub page for new issues.</p>
<h3>References</h3>
<p><a href="https://github.com/magesuite/magepack">https://github.com/magesuite/magepack</a><br />
<a href="https://github.com/magesuite/magepack-magento">https://github.com/magesuite/magepack-magento</a><br />
<a href="https://www.integer-net.com/magento-2-javascript-bundling-integer-net-2/">https://www.integer-net.com/magento-2-javascript-bundling-integer-net-2/</a><br />
<a href="https://gist.github.com/tdgroot/f95c398c565d9bbb83e0a650cdf67617">https://gist.github.com/tdgroot/f95c398c565d9bbb83e0a650cdf67617</a><br />
<a href="https://github.com/integer-net/magento2-requirejs-bundling">https://github.com/integer-net/magento2-requirejs-bundling</a><br />
<a href="https://developers.google.com/speed/pagespeed/module">https://developers.google.com/speed/pagespeed/module</a></p>
<p><a href="https://blog.gaiterjones.com/magento-google-pagespeed-jscsshtmlminify-optimisation/">PageSpeed optimisation in Magento 1</a></p>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.gaiterjones.com/magento-2-javascript-bundling-with-magepack/feed/</wfw:commentRss>
			<slash:comments>37</slash:comments>
		
		
			</item>
	</channel>
</rss>
