Performance best practices for Shopify themes

Performance is an important factor for merchants when they choose a theme for their online store. When you build or customize a theme, you should build with performance in mind. Optimizing your theme for performance is key to the success of the merchants that you support and to the experiences of their customers. Performance directly influences conversion rates, repeat business, and search engine rankings.

When a theme is submitted to the Shopify Theme Store, the theme's tested on a benchmark shop to determine its performance score. To be accepted into the Shopify Theme Store, a theme must have a minimum average lighthouse performance score of 60 across the home page, product page, and collection page. You can run a similar test on your theme using a development store.

Learn more about performance testing for themes in the Shopify Theme Store.

Optimizing for performance

Consider the following best practices for optimizing the performance of your theme.

Optimize your JavaScript

Optimize the JavaScript in your theme using the following principles.

Reduce JavaScript usage

Consider building your theme using primarily HTML and CSS. JavaScript shouldn't be required for the basic functionality of your theme, such as finding or purchasing products. Instead, you should only use JavaScript as a progressive enhancement, and only where there is no HTML or CSS solution available.

CSS parses and renders much faster than JavaScript, so wherever possible, you should use CSS features for building interactivity. You can find more information on the internet by searching the phrase “using CSS instead of JavaScript”. One example is the blog 5 things you can do with CSS instead of JavaScript by Juan Martín García.

Your minified JavaScript bundle size should be 16 KB or less. Shopify automatically minifies JavaScript when it's requested by the storefront.

Reduce your dependency on external frameworks and libraries

If you need to use JavaScript, consider avoiding introducing third-party frameworks, libraries, and dependencies. Instead, use native browser features and modern DOM APIs whenever possible. Including JavaScript libraries in your package can lead to large bundle sizes, slow load times, and a poor experience for customers. Frameworks such as React, Angular, and Vue, and large utility libraries such as jQuery have significant performance costs. Avoid introducing polyfill libraries for very old browsers (anything that doesn't support async/await). If you use a browserslist, then you can target browsers with a > 1% marketshare.

Avoid parser-blocking scripts

Parser-blocking scripts block the construction and rendering of the DOM until the script is loaded, parsed, and executed. They also create congestion on the network and significantly delay page rendering. This impacts metrics like First Contentful Paint and Largest Contentful Paint. Use defer or async attributes on your script tags to avoid this.

Preload key resources, defer or avoid loading others

Preloading resources allows the browser to download resources before they are discovered. Choosing to load some resources later and using system resources helps you to reduce the size of the initial package of resources that needs to be downloaded before a customer can meaningfully interact with the page.

Use resource hints to preload key resources

You can add up to two resource hints to your code per template by using preload keyword arguments on Liquid filters. Currently, only the stylesheet_tag filter accepts a preload keyword argument.

When Shopify renders a page with preload instructions, it will send a preload resource hint as a Link header on subsequent requests.

You should use resource hints sparingly. For example, consider preloading only render-blocking stylesheets that are needed for initial functionality of the page, such as above-the-fold content.

Lazy load below-the-fold images

Load images only when they're needed on a page, and consider using placeholders until customers scroll down the page. This can also help with perceived performance as the page looks like it’s loading quicker than it actually is. Rather than using a library, you should set img or srcset loading attributes to lazy:

Anything that appears above the fold shouldn't be lazy-loaded. Above-the-fold content is the content a viewer sees on page load before they scroll down the page. Above-the-fold resources should be considered critical assets, and should be loaded normally.

Load non-critical resources on interaction

Your page might contain code for a component or resource that isn't always used. You can load these resources using an import on interaction pattern to avoid loading, parsing, and executing unnecessary code.

Consider using a system font

Using a system font avoids the client needing to download another resource before the online store's text can be rendered.

Host assets on Shopify servers

Deliver as much as you can from the Shopify content delivery network (CDN). Using the same host for your assets avoids unnecessary HTTP connections and allows the server to prioritize delivery of blocking resources using HTTP/2 prioritization.

In a Shopify context, you can do this by adding your assets to the theme's /assets folder, either manually by using the GitHub Integration, or by using the Asset REST Admin API resource. You can create links to these assets using URL filters. Learn more about the Shopify CDN.

Use responsive images

Viewing large images on a small device can be frustrating and can slow down page load speed. Using responsive images automatically resizes them to fit the device screen that customers are using.

Specifying an image size ensures that you download the smallest possible image without degrading quality. The storefront requests the image size that’s going to be displayed, and then cuts down the file size downloaded from the CDN. This reduces reliance on browser-side scaling.

You can add responsive images to your theme by combining with HTML attributes like srcset and sizes on img and picture tags. Inside of your tag, you can reference images using the img_url asset URL filter with defined size parameters. You can also lazy-load images in this context using loading="lazy".

Example:

Optimize Liquid code

You can edit almost all of the Liquid that is used to render your store. There are efficient and inefficient ways of writing Liquid code. Doing complex operations repeatedly can increase your Liquid render time, which impacts your overall store speed.

For example, if you want to order the products in a collection by price, you should do that before you loop through the products in your collection, and not as part of the loop code. This is because the order of the products does not change for each product, and calculating the order of the products adds processing time to the request.

Run the Shopify Theme Inspector for Chrome to identify the lines of code that are slowing down pages in your online store. Read a walkthrough of analyzing your Liquid using this tool on the Shopify Engineering blog.

Use Theme Check to identify performance issues

You can use Theme Check to identify possible performance issues in your theme code, including large CSS and JS bundles, references to remote assets, and parser-blocking JavaScript. Learn more about Theme Check performance checks.

Testing for performance

Shopify offers merchants a speed report that helps them to understand the performance of their store. This report is a weighted averages of the Lighthouse performance scores of certain pages. You can run Lighthouse audits manually or using CI, or you can review the speed scores of stores that you manage.

Run a Lighthouse audit using Shopify data

Use the following process to emulate the tests that Shopify runs to determine an online store's speed score. Shopify runs a similar test against themes before they are accepted into the Shopify Theme Store. You can run a similar test against your theme to understand how it performs.

  1. Create a development store.
  2. Import the test product csv to the store. The store should have no other collections, products, or variants.
  3. In your development store, beside Online Store, click the eye icon to preview your store.
  4. From the preview URL, copy the value of the _bt parameter.

    If your preview URL doesn't have a _bt parameter, then your development store might have been created before August 2020. To learn how to find a preview URL for these stores, refer to Development stores created before August 2020.

  5. Get the URLs for the pages that you want to audit. You should test the home page (h), any product page (p), and any collection page (c).

  6. Append your theme's _bt value to the end of each URL.

    For example, the url https://my-dev-shop.myshopify.com/products/sunglasses becomes https://my-dev-shop.myshopify.com/products/sunglasses?_bt=value-you-copied.

  7. Visit Google PageSpeed Insights, and then analyze each page by entering the URL and clicking Analyze. Note the score for each page.

  8. Apply this formula to your results: [(p * 31) + (c * 33) + (h * 13)] / 77. The result is your theme's speed score.

Development stores created before August 2020

If your development store was created before August 2020, then follow these steps to get preview links to test:

  1. On the Themes page of your development store, choose the theme that you want to test.
  2. Click Actions > Preview. A preview of the theme opens.
  3. At the bottom right of the page, in the preview bar, click Share Preview. A modal will open with your preview url.
  4. In the modal, click Copy link.
  5. For each page that you want to test, replace the base URL with the preview URL.

    For example, to test the sunglasses product, you might change the URL from https://my-dev-store.myshopify.com/products/sunglasses to https://12345678.shopifypreview.com/products/sunglasses.

Use Lighthouse CI to catch performance issues early

If you use a continuous integration (CI) process for your themes during development, then you can add a CI check to make sure that changes to your theme code don't have a significant negative impact to your performance score. You can do so using the Shopify Lighthouse CI GitHub action, a Shopify-developed GitHub action that uploads your theme code to a benchmark shop and then measures and calculates your speed score.

Review managed stores' performance

You can review the performance of the stores that you manage on the Store Performance page in your Shopify Dashboard. You can access this page by logging into your Partner Dashboard, navigating to the Stores page, then clicking View store performance. Learn more about the Store Performance page.

Next steps