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 influencesconversion rates, repeat business, andsearch engine rankings.
When a theme is submitted to theShopify 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 adevelopment store.
Learn more about performance testing for themes in the Shopify Theme Store.
Optimizing for performance
Anchor link to section titled "Optimizing for performance"Consider the following best practices for optimizing the performance of your theme.
Optimize your JavaScript
Anchor link to section titled "Optimize your JavaScript"Optimize the JavaScript in your theme using the following principles.
Reduce JavaScript usage
Anchor link to section titled "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 blog5 things you can do with CSS instead of JavaScriptby Juan Martín García.
Your minified JavaScript bundle size should be 16 KB or less. Shopifyautomatically minifiesJavaScript when it's requested by the storefront.
Reduce your dependency on external frameworks and libraries
Anchor link to section titled "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 supportasync
/await
). If you use abrowserslist, then you can target browsers with a > 1% marketshare.
Avoid parser-blocking scripts
Anchor 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 likeFirst Contentful PaintandLargest Contentful Paint. Usedefer
orasync
attributes on your script tags to avoid this.
Preload key resources, defer or avoid loading others
Anchor 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 resources
Anchor 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:
- The
preload_tag
filter - The
preload
keyword argument on thestylesheet_tag
orimage_tag
filters
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
Anchor 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 aloading: 'lazy'
attribute to your image tag using theimage_tag
filter:
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
Anchor link to section titled "Load non-critical resources on interaction"为一个组件或页面可能包含代码resource that isn't always used. You can load these resources using animport on interactionpattern to avoid loading, parsing, and executing unnecessary code.
Consider using a system font
Anchor link to section titled "Consider using a system font"Using asystem fontavoids the client needing to download another resource before the online store's text can be rendered.
Host assets on Shopify servers
锚的链接部分题为“主机资产商店ify 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/assetsfolder, either manually by using theGitHub Integration, or by using theAsset
REST Admin API resource. You can create links to these assets usingURL filters.Learn more about the Shopify CDN.
Use responsive images
Anchor 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 theimage_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 thesizes
keyword argument.
Optimize Liquid code
Anchor 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 theShopify Theme Inspector for Chrome来identify the lines of code that are slowing down pages in your online store. Read a walkthrough of analyzing your Liquid using this toolon the Shopify Engineering blog.
Use Theme Check to identify performance issues
Anchor link to section titled "Use Theme Check to identify performance issues"You can useTheme Check来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
Anchor link to section titled "Testing for performance"Shopify offers merchants aspeed reportthat helps them to understand the performance of their store. This report is a weighted averages of theLighthouse performance scoresof 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
Anchor 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 theShopify Theme Store. You can run a similar test against your theme to understand how it performs.
- Create adevelopment store.
- Importthetest product csv来the store. The store should have no other collections, products, or variants.
- In your development store, beside欧宝体育官网入口首页, click the eye icon to preview your store.
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 toDevelopment 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
_bt
value to the end of each URL.For example, the url
https://{shop}.myshopify.com/products/sunglasses
becomeshttps://{shop}.myshopify.com/products/sunglasses?_bt=value-you-copied
.VisitGoogle 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 2020
Anchor 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 theThemespage of your development store, choose the theme that you want to test.
- ClickActions>Preview. A preview of the theme opens.
- At the bottom right of the page, in the preview bar, clickShare Preview. A modal will open with your preview url.
- In the modal, clickCopy link.
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 fromhttps://{shop}.myshopify.com/products/sunglasses
来https://12345678.shopifypreview.com/products/sunglasses
.
Use Lighthouse CI to catch performance issues early
Anchor 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 usingthe 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
Anchor 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 theStorespage, then clickingView store performance.Learn more about the Store Performance page.