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.
Optimizing for performanceAnchor link to section titled "Optimizing for performance"
Consider the following best practices for optimizing the performance of your theme.
Avoid namespace collisionsAnchor link to section titled "Avoid namespace collisions"
function scope. Values defined in a
function scope are available only within the scope of that function, so there's no risk of collision with other variables that are defined on the global scope.
Reduce your dependency on external frameworks and librariesAnchor link to section titled "Reduce your dependency on external frameworks and libraries"
await). If you use a browserslist, then you can target browsers with a > 1% marketshare.
Avoid parser-blocking scriptsAnchor link to section titled "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
async attributes on your script tags to avoid this.
Preload key resources, defer or avoid loading othersAnchor link to section titled "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 resourcesAnchor link to section titled "Use resource hints to preload key resources"
You can add up to two resource hints to your code per template by using one of the following:
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 imagesAnchor link to section titled "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 pass a
loading: 'lazy' attribute to your image tag using the
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 interactionAnchor link to section titled "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 fontAnchor link to section titled "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 serversAnchor link to section titled "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 imagesAnchor link to section titled "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 using the
image_tag filter. This filter returns a srcset for the image using a smart default set of widths. You can adjust the srcset sizes that the filter returns using the
sizes keyword argument.
Optimize Liquid codeAnchor link to section titled "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 issuesAnchor link to section titled "Use Theme Check to identify performance issues"
Testing for performanceAnchor link to section titled "Testing for performance"
Shopify offers a Web Performance Dashboard & Reports that helps you to understand the performance of your store. This tool helps you can understand how your store performs across industry standards for loading speed, interactivity, and visual stability, better known as Core Web Vitals. The Web Performance Dashboard & Reports is calculated using Real User Monitoring (RUM) data and aims to provide you with a reliable performance signal and actionable insights on how to improve.
Since real user data is needed here, you may still choose to use Lighthouse performance scores on shops with no / low traffic. 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 dataAnchor link to section titled "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.
- Create a development store.
- Import the test product csv to the store. The store should have no other collections, products, or variants.
- In your development store, beside Online Store, click the eye icon to preview your store.
From the preview URL, copy the value of the
If your preview URL doesn't have a
_btparameter, 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.
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).
Append your theme's
_btvalue to the end of each URL.
For example, the url
Visit Google Lighthouse, and follow the steps to run a report for each of your pages. Note the mobile score for each page.
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 2020Anchor link to section titled "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:
- On the Themes page of your development store, choose the theme that you want to test.
- Click Actions > Preview. A preview of the theme opens.
- At the bottom right of the page, in the preview bar, click Share Preview. A modal will open with your preview url.
- In the modal, click Copy link.
For each page that you want to test, replace the base URL with the preview URL.
For example, to test the
sunglassesproduct, you might change the URL from
Use Lighthouse CI to catch performance issues earlyAnchor link to section titled "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' performanceAnchor link to section titled "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.