diff --git a/docs/docs/how-to/performance/improving-site-performance.md b/docs/docs/how-to/performance/improving-site-performance.md new file mode 100644 index 0000000000000..7674bd66cdfed --- /dev/null +++ b/docs/docs/how-to/performance/improving-site-performance.md @@ -0,0 +1,213 @@ +--- +title: Improving Site Performance +--- + +To paraphrase Tolstoy, all fast websites are alike, but all slow websites are slow in different ways. + +This document is a brief guide to improve your Gatsby site's performance. + +## Part 1: Creating a process for performance improvements + +Because websites are hard to make fast and easy to make slow, performance improvement isn't just something that takes place at one point in time. Instead, performance is a system that you put in place to maintain and extend performance gains over time. + +### Step 1: Choose a testing tool + +In order to get a baseline, you need to decide what you consider the "source of truth". + +Lighthouse / PageSpeed Insights and Webpagetest are the most common website performance testing tools in the Gatsby community. Lighthouse tends to be seen as more "canonical". Webpagetest tends to be seen as more precise. + +These tools measure what are known as "Core Web Vitals", which measure both time to page load and time to page interactivity. [Google's official page](https://web.dev/vitals/) has more detail on what these metrics are. + +### Step 2: Set up performance monitoringĀ  + +Site performance monitoring becomes increasingly important the more long-lasting your project is. You can put a lot of effort into performance work -- but then ship a performance regression that wipes out all your hard work! + +There are two options for this: + +- Gatsby Cloud has Lighthouse performance reports built into its CI/CD. Every time someone opens a pull request or merges into master, a Lighthouse report will be run and the performance score displayed. + +- Use a third-party service that offers performance monitoring. Different services offer different options -- schedule runs on a daily basis or CI/CD integration through Github (or your source control service). + +For additional precision, run Lighthouse multiple times and take the median result. + +### Step 3: Quantify the impact of each change you makeĀ  + +While you're doing performance-improvement work, it's important to understand the impact of each change or set of changes. You may find that one change gives you a 2-point Lighthouse improvement and another gives you a 20-point improvement. + +There's an easy way to measure this: + +1. Write a pull request with each change or set of changes, using a service like Gatsby Cloud or Netlify that generates deploy previews per-PR. +2. Run Lighthouse tests against the deploy preview for that branch and the deploy preview for master. Do not compare the live site to a deploy preview since the CDN setup may differ. +3. Calculate the difference in Lighthouse scores. +4. If you don't see a significant difference from a change, consider batching similar changes together until the difference is noticeable and thinking about the changes as a group. + +Performance work can be surprisingly nonlinear in impact. + +## Part 2: Implement Improvements. + +When you run a test in your testing tool of choice, it will give you a number of recommendations. While this can feel like a laundry list of issues, it can be helpful to understand the five core categories that these issues are bucketed into. + +- Blocking calls & third-party scripts. +- Javascript bundle size. +- Stylesheets and font files. +- Images and other media. +- Resource requests & CDN caching configuration. + +Every site is different, and the recommendations will give some guidance as to where the highest effort to impact ratio is on your site. + +### Address third-party-script impact + +Various types of calls made in your HTML, like calls to external font files, will block page load or page interactivity in different ways. In addition, third-party scripts can execute "eagerly", often delaying page load while they do so. + +#### Step 1: Remove unneeded and high-cost, low-value scripts + +For each script, it can be helpful to understand the business purpose, relative importance, and who's using the data. This will allow you to identify and remove any unused scripts. + +There may also be relatively unimportant scripts that have a high performance costs; these are also good candidates for removal. + +#### Step 2: lazy load or inline scripts + +One of the lowest-hanging fruits is to set your scripts to load lazily rather than "eagerly" (the default). Any `