Post

How I Optimized The Lab Bench with AI and PageSpeed Insights

How I Optimized The Lab Bench with AI and PageSpeed Insights

How I Optimized The Lab Bench with AI and PageSpeed Insights

There is a certain irony in running a blog about self-hosting and infrastructure while the blog itself makes a browser wait for fonts from Google and JavaScript from a CDN.

Naturally, I had to fix that.

I ran The Lab Bench through PageSpeed Insights and received the usual mixture of useful advice, alarming colours, and recommendations that sound as if the website is personally disappointing the browser.

The main problems were on mobile:

  • Slow third-party requests
  • Render-blocking CSS and JavaScript
  • Late-loading fonts
  • Unused CSS
  • Layout shifts
  • Too much JavaScript work during the first render

The audit also found an invalid ARIA role and an incorrect sitemap URL. So this was no longer just a performance exercise. It had become one of those “while I am here” jobs that grows legs and walks away with the weekend.

I used AI to help interpret the findings and compare possible fixes. The actual changes were then implemented and tested in the production Docker build.

First: stop asking the browser to visit half the internet

The biggest improvement was moving Chirpy’s static assets closer to home.

Chirpy now uses self-hosted assets:

1
2
3
assets:
  self_host:
    enabled: true

The standard libraries, Font Awesome, icon fonts, and text fonts are served from the same site instead of being loaded from JSDelivr and Google Fonts.

This avoids extra DNS lookups, connection setup, and downloads from separate origins. That matters particularly on mobile, where every additional request has a chance to take longer than expected.

It also makes the build more predictable. Depending on a CDN is convenient until the production image contains HTML referring to files that were never copied into the image. At that point the website is technically deployed, but the browser has been left holding the empty plate.

The static asset repository is pinned to a known commit. The Docker build also contains a fallback for clean checkouts where the Git submodule has not been initialized, because apparently even a blog needs a contingency plan for missing fonts.

Not every stylesheet needs to arrive before breakfast

Several stylesheets were blocking the first render even though they were not required for the first screen.

I added a shared _includes/non-blocking-stylesheet.html partial. Optional stylesheets now use a preload that becomes a normal stylesheet once loading has finished:

1
2
3
4
5
<link rel="preload" href="..." as="style" fetchpriority="low"
      onload="this.onload=null;this.rel='stylesheet'">
<noscript>
  <link rel="stylesheet" href="...">
</noscript>

This is used for:

  • The local font stylesheet
  • Font Awesome
  • Lazy-loading CSS
  • Table-of-contents styles
  • GLightbox

The noscript fallback means the site does not collapse completely just because JavaScript is having a day off.

For the homepage, I split the theme CSS into two parts:

  1. Critical CSS for the visible layout.
  2. A deferred stylesheet containing everything else.

The critical rules cover the article, sidebar, footer, scroll-to-top control, and basic page geometry. The remaining styles are written to:

1
jekyll-theme-chirpy-deferred.css

and loaded after the initial render.

Other layouts still load the complete theme stylesheet normally. There was no reason to redesign the entire site just because the homepage wanted to be faster.

Removing CSS nobody is using

The theme stylesheet contained quite a bit of CSS that was not needed by the generated pages.

The new purgecss.js step runs after Jekyll has generated the final HTML. This is important because the actual selectors are spread across layouts, includes, Markdown output, and JavaScript. Looking only at the source templates would be a bit like inspecting a sausage recipe and claiming to understand the sausage.

PurgeCSS scans the generated HTML and JavaScript and removes unused selectors. Runtime classes for Bootstrap and the site’s interactive components are preserved, including:

  • Collapse
  • Dropdowns
  • Modals
  • Offcanvas panels
  • Toasts
  • Tooltips
  • Carousels
  • Mermaid
  • Syntax highlighting

The .d-none class is explicitly preserved as well. It may look unused in the generated page, but it is needed to keep hidden elements hidden while deferred CSS is arriving.

The PageSpeed audit estimated that around 15.5 KiB of the homepage’s 19.1 KiB of theme CSS was unused. The exact savings depend on the generated content, but the homepage no longer sends the entire theme stylesheet synchronously just in case someone opens a modal they cannot see.

Layout shifts: the page should stay where I put it

A fast page that moves around while loading is not particularly pleasant. It is like trying to read a menu while somebody keeps moving the table.

The remote GitHub avatar was one source of movement. Its dimensions were not always reserved before the image decoded, so the sidebar could change shape as the image arrived.

The image now has explicit dimensions and stable critical CSS:

1
2
3
4
5
6
7
8
9
10
#sidebar #avatar {
  aspect-ratio: 1 / 1;
}

#sidebar #avatar img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

The main container, footer, scroll-to-top control, and icons also receive stable geometry before the deferred CSS loads:

1
2
3
4
5
6
7
8
9
10
11
12
13
*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  line-height: 1.5;
}

#main-wrapper > .container {
  flex: 0 0 100%;
}

Initially, I concentrated on the first article and considered the job mostly done. The audit then pointed out that the footer could still move. Naturally, the footer had opinions too.

Icons are small, but not invisible

Font Awesome loads asynchronously. Before the icon font is ready, fallback glyphs can have different dimensions from the final icons.

That caused small changes in the mode toggle and sidebar navigation. The critical CSS now reserves space:

1
2
3
4
5
6
7
8
9
10
11
.fa-fw {
  display: inline-block;
  width: 1.25em;
}

#mode-toggle {
  width: 1.25em;
  height: 1.25em;
  line-height: 1.25em;
  text-align: center;
}

Nothing dramatic. Just enough geometry so the controls do not have to rearrange themselves once the font turns up.

Fonts should not rearrange the article

The generated font stylesheets originally used font-display: block or font-display: swap.

That can make text appear late or change its dimensions after the page has already been laid out. For a blog, readable fallback text immediately is more useful than waiting for the perfect font while staring at an empty space.

The production build now uses:

1
font-display: optional;

The browser can display the system sans-serif fallback immediately. If the local font is ready quickly, it may be used. If not, the browser avoids replacing already-rendered text and shifting the layout.

This is not a typography redesign. The fonts have not been sent to a farm upstate. They are still available; they are simply no longer allowed to disrupt the first render.

The build checks that the generated font stylesheets do not contain font-display: block or font-display: swap, so this particular problem should not quietly return later.

JavaScript can wait its turn

The main Chirpy JavaScript is now deferred:

1
<script src="/assets/js/dist/theme.min.js" defer></script>

The homepage-specific bundle waits even longer. It is loaded after the load event and during an idle period:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
window.addEventListener('load', () => {
  const loadBundle = () => {
    const script = document.createElement('script');
    script.src = '...';
    script.async = true;
    document.head.appendChild(script);
  };

  if ('requestIdleCallback' in window) {
    requestIdleCallback(loadBundle, { timeout: 1500 });
  } else {
    setTimeout(loadBundle, 0);
  }
});

The homepage should first display the article before it starts preparing functionality that is not required for the first paint. Even JavaScript deserves a queue. Preferably a well-managed one, unlike the queue at the post office.

Search focus was also changed from:

1
input.focus();

to:

1
input.focus({ preventScroll: true });

This keeps keyboard focus working without making the browser scroll the page unexpectedly when search is activated.

Removing a few unnecessary transitions

PageSpeed identified color and border transitions on the tags, sidebar trigger, search trigger, and mode toggle as non-composited animations.

The targeted fix is:

1
2
3
4
5
6
.post-tag,
#mode-toggle,
#sidebar-trigger,
#search-trigger {
  transition: none !important;
}

This is not a global ban on animation. The controls still work. They simply no longer spend time repainting colour and border changes during the initial page shell.

Sometimes the fastest animation is the one that does not happen. Very efficient.

Fixing the things that were not performance problems

The audit also found an accessibility issue. The PWA update notification used:

1
<aside role="alert">

That is not a valid role assignment for the element. It now uses a div while keeping:

1
2
3
role="alert"
aria-live="assertive"
aria-atomic="true"

The announcement behavior remains unchanged, but the markup is valid.

The sitemap was corrected as well:

1
url: "https://blog.stix.foo"

The production build now verifies that sitemap.xml exists and that robots.txt contains:

1
Sitemap: https://blog.stix.foo/sitemap.xml

This is primarily an SEO fix, but once the audit has found a problem, leaving it there feels a bit like putting a “please ignore” sticker on the dashboard.

Verification

The complete production build passed successfully.

Current mobile PageSpeed result: performance 100
Current mobile result; performance 100.
Current desktop PageSpeed result: performance 100
Current desktop result; performance 100.
Earlier mobile PageSpeed result: performance 78
Earlier mobile result; performance 78.
Earlier desktop PageSpeed result: performance 98
Earlier desktop result; performance 98.

The generated site passed:

  • PurgeCSS
  • The project’s custom tests
  • HTML-Proofer
  • Shell and JavaScript syntax checks
  • Whitespace checks
  • The production Nginx smoke test

HTML-Proofer checked 77 internal links and hashes, and the local asset URLs returned HTTP 200.

A browser layout-shift observer recorded:

1
CLS: 0

on both desktop and mobile viewport sizes after the final footer and icon fixes.

The PageSpeed score still needs to be checked again after deployment. Remote audits depend on the server, cache state, network conditions, and test device, so a local build cannot promise a permanent green number.

What it can prove is that the site is generating the output I intended:

  • Fewer third-party requests
  • Less blocking CSS
  • Less unused CSS
  • Less JavaScript during the first render
  • More stable image and icon geometry
  • Fewer disruptive font changes
  • Better accessibility and SEO validation

AI helped me work through the report, but it did not magically make the website fast. The useful part was still the boring part: understanding what the browser was waiting for, changing one thing at a time, and testing the actual production build.

A fast website is not created by adding one mysterious optimization at the end.

It is created by making the browser wait less, download less, and move fewer things around.

And, ideally, by not making it travel to three different countries just to display a blog post.

This post is licensed under CC BY 4.0 by the author.