Chasing a 100 on PageSpeed
Eight fixes that made my portfolio load faster: fonts, CSS, one photo and a few small things. What worked, what didn't, and the numbers.
I ran PageSpeed Insights on this site and did not like what I saw. The home page took over four seconds on a phone to show its biggest piece of text. That was a static site with no framework, so the problem was mine.
This post is the list of things I changed, in the order I changed them, with the numbers from each step.
In short
- Fonts were the biggest cost. I moved them onto my own domain and cut them down to the letters each page uses.
- Most of the CSS was never used. Each page now gets a stylesheet with only the rules it can use: 9 KB instead of 24 KB.
- Small warnings add up. A too-large photo, a slow animation and a script that read the layout too early each cost a little.
- Mobile Lighthouse on the home page went from about 86 to 90 after the font work alone, and LCP from 4.3 s to 3.6 s.
What PageSpeed measures
PageSpeed gives a score from 0 to 100. Most of it comes from a few timings:
words in this post
- LCP
- Largest Contentful Paint. When the biggest text or image on the screen shows up. On my home page that is the greeting with my name.
- TBT
- Total Blocking Time. How long the browser is too busy to react to a tap.
- CLS
- Cumulative Layout Shift. How much the page jumps around while it loads.
- Render-blocking
- a file the browser must download before it can draw anything, like a stylesheet in the head.
The mobile test is the hard one. It pretends to be a slow phone on a slow network, so every extra kilobyte shows.
The plan
Serve the fonts myself
Cut the display font down to my name
Fix the colour contrast
Stop Bangla pages from loading English fonts
Make the site work offline
Clear the render, reflow and animation warnings
Send a smaller photo to normal screens
Give each page its own stylesheet
The Execution
Fonts come from my own domain
The site used Google Fonts. That means a second domain, a second connection, and a CSS file from Google before the browser even knows which font files it needs.
I downloaded the fonts into the site with a small Python script and served them from the same domain. Three more things came with it:
- Variable fonts. One file covers every weight, so I don't need a file for 400, another for 500 and another for 700.
- Preload. The body and heading fonts are preloaded, so the browser starts fetching them right away.
- The logo became an SVG. The header logo is my name in an italic font. Every page was downloading the whole italic font just for that. Now it is an SVG path, drawn once from the font by a script.
English pages also show a few Bangla words, like my name in Bangla. Those now use a 6 KB cut of Hind Siliguri instead of the full Bangla font.
Fonts on an English pagelower is better
- Before630 KBfrom Google Fonts
- After, heaviest page475 KB
- After, lightest page270 KB
Five letters, 150 KB
After step 1, the home page was still slow. The greeting at the top says hello with my name in a big italic font. That greeting is the LCP element, so the page is not "loaded" until that font arrives.
The whole italic font was 150 KB, and the page used it for five letters.
So the font script now makes a second, tiny font called "Display Name". It has only the letters of my short name, with every weight and style axis kept. The home page preloads it.
Italic font for the greetinglower is better
- Whole font150 KB
- Name-only cut8 KB
- Mobile Lighthouse, home page
- 86 → 90
- LCP, home page
- 4.3 s → 3.6 s
mobile
That was the biggest single jump in the whole project.
A colour that was almost dark enough
This one was not about speed. PageSpeed also checks accessibility, and it flagged the small grey text on my project cards.
Grey text fails contrast
- symptom
PageSpeed said the type line on the project cards was hard to read.
- cause
The grey (#7d766b) on the card background had a contrast of 4.42 to 1. Text at 13 px needs at least 4.5 to 1. It also failed on the page background, but the dotted pattern there hid it from the audit.
- fix
A slightly darker grey: #706a60 in light mode (4.56 to 5.27 on every background) and #928a7d in dark mode (4.64, was 4.46).
- lesson
"Almost passes" is a fail. Check a colour on every background it sits on, not only the one the tool happens to test.
Bangla pages were loading English fonts
The site has Bangla pages too. I expected them to load only Bangla fonts. They didn't.
The reason was the space character. A Bangla sentence still uses a normal space and normal punctuation, like commas. In the font files from Google, the space lives in the Latin file, not the Bengali one. So a Bangla heading downloaded a whole English font just to draw its spaces.
The fix was to build a twin of each font, like "Fraunces BN". The twin has the Bengali letters plus the space and punctuation copied in from the Latin file. On Bangla pages, the twin comes first in the font list, so the browser finds everything it needs in one file.
My name in the greeting got the same treatment: one small cut for the English spelling and one for the Bangla spelling. Each of these is set with no fallback font. If I added a fallback, the browser would download it "just in case" while the small cut loads, and the saving would be gone. The build stops with an error if my name has a letter that the cut doesn't have, so it can't break quietly.
- LCP on Bangla pages
- 5.1 s → 4.7 s
mobile
Offline reading
This step doesn't raise the score, but it is close to it. A service worker now saves pages, styles, scripts and fonts. A second visit loads from the phone, not the network, and the site works with no connection at all.
One thing I had to be careful about: after a deploy, nobody should get an old CSS file from the cache. Every stylesheet and script link now ends with a version made from its content, like site.css?v=3fa9c1. New content, new link, no stale file.
Three small warnings
PageSpeed had three more warnings. Each was small. Together they mattered.
A second render-blocking stylesheet. The font rules lived in their own file, fonts.css. The browser had to download it before it could even start on the fonts. Now those rules are written straight into the head of each page. I also started minifying the main stylesheet at build time, which saves about 7 KB gzipped.
A script that read the layout too early. My header hides when you scroll down. To do that, the script needs the header's height, and it was reading it while the page was still loading. Asking for a height at that moment makes the browser stop and lay out the page early, just to answer. That is called a forced reflow.
site.js
var state = 'full', fullH = top.offsetHeight;
root.style.setProperty('--top-h', fullH + 'px');var state = 'full', fullH = 0;
// read the height after layout, and again when it changes
new ResizeObserver(measure).observe(top);A ResizeObserver runs after the browser has done its layout anyway, so reading the height there is free.
An animation the GPU couldn't run. There is a hand-drawn underline under my name that draws itself from left to right. It animated stroke-dashoffset, which the browser has to repaint on the main thread, frame by frame.
Now it is a "wipe": the box around the line slides in from the left, and the line inside slides the other way by the same amount. The line looks still, and only the visible part grows. Both moves are transform, which the GPU handles off the main thread.
The underline
.name path {
stroke-dasharray: 420;
stroke-dashoffset: 420;
animation: draw 0.9s forwards;
}.name .ul { overflow: hidden; animation: wipe-box 0.9s both; }
.name svg { animation: wipe-line 0.9s both; }
@keyframes wipe-box { from { transform: translateX(-100%); } }
@keyframes wipe-line { from { transform: translateX(100%); } }To make sure none of this changed how the site looks, I took full-page screenshots of twelve pages, in both themes, at phone and desktop widths, before and after. They matched pixel for pixel, apart from the underline in the middle of its animation.
A photo that was too big
On desktop, PageSpeed said my profile photo was 400 px wide but shown at about 290 px. The browser was downloading pixels it threw away.
The build now makes a 300 px WebP copy next to the original. The image tag gets a srcset, so the browser picks the right one:
<img src="/me.webp"
srcset="/me-300.webp 300w, /me.webp 400w"
sizes="(max-width: 640px) min(232px, calc(80.5vw - 32px)),
(max-width: 960px) 244px, 290px"
width="252" height="290" fetchpriority="high" alt="...">
Normal screens get the small copy. Phones and high-density screens still get the full one, so the photo never looks soft.
Profile photolower is better
- Original19 KB
- 300 px copy8 KB
Each page gets its own stylesheet
The last warning was "reduce unused CSS". The site has one stylesheet for everything: the home page, posts, project pages, photo galleries. Any single page uses a small part of it.
Tools like PurgeCSS solve this for the whole site. I wanted it per page. So after each build, a small C# step goes through every HTML file and does this:
- Collect every class and id used in the page's HTML.
- Collect every word in the page's scripts, because a script can add a class later.
- Go through the stylesheet and drop a rule only if none of its selectors could match.
- Save what is left as
/css/p/<hash>.cssand link it from the page.
The file is named by a hash of its content. So pages that need the same rules, like the English and Bangla home pages, share one file, and the browser caches it once.
I kept the rules safe on purpose. Rules stay in the same order, so the cascade doesn't change. Anything tricky, like :not(), :is(), :has() or @keyframes, is kept whole. A few extra bytes are better than a broken page.
CSS on the home page, gzippedlower is better
- One stylesheet for the site24 KB
- Cut for this page9 KB
What I tried first
If the cut stylesheet is only 9 KB, I can put it straight into the page head. No extra request, nothing render-blocking, faster page.
I wrote each page's CSS into a style tag in its head and ran mobile Lighthouse again.
The render-blocking warning went away, but Total Blocking Time went up to about 500 ms. The browser now had to parse all that CSS as part of its first big task. A separate file, which the browser can download and cache, was faster.
So the stylesheet stays a separate file. The one thing I did put into the head is the small block of font rules from step 6. That block is short, and the browser needs it early to find the fonts.
What I learned
- Fonts first. On a text-heavy site, fonts were most of the weight and most of the LCP. Cut them to what each page uses.
- Look at your LCP element. For me it was five italic letters. Once I knew that, the fix was obvious.
- Read the warning, then read the cause. "Bangla page loads Latin font" sounded like a bug in my CSS. It was the space character.
- Measure every change. Inlining the CSS looked like a clear win and made things worse. I only knew because I ran the test again.
- Check nothing broke. Screenshots of every page before and after let me change a lot without fear.
None of these fixes are clever on their own. The score moved because I did all of them, one at a time, and checked each one.