﻿<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" version="2.0">
	<channel>
		<title>S M Hasib Hasnain, tech</title>
		<link>https://hasibhasnain.com/</link>
		<description>Building and testing backends with C# and .NET.</description>
		<copyright>2026</copyright>
		<managingEditor>S M Hasib Hasnain</managingEditor>
		<pubDate>Sat, 05 Dec 2026 00:00:57 GMT</pubDate>
		<lastBuildDate>Sat, 05 Dec 2026 00:00:57 GMT</lastBuildDate>
		<item>
			<title>A social card for every page</title>
			<link>https://hasibhasnain.com/tech/a-social-card-for-every-page</link>
			<description>How my site draws its own link preview images at build time with SkiaSharp: the layout, Bangla text, caching, and the bugs on the way.</description>
			<author>S M Hasib Hasnain</author>
			<guid>https://hasibhasnain.com/tech/a-social-card-for-every-page</guid>
			<pubDate>Fri, 04 Dec 2026 00:00:00 GMT</pubDate>
			<content:encoded>&lt;p&gt;When you paste a link into WhatsApp, Facebook or LinkedIn, you get a little preview: a picture, a title and a line of text. That picture is called a social card, or an og:image. For a long time, every page on my site shared the same one. A post about databases and a photo story from Sylhet looked exactly the same in a chat.&lt;/p&gt;
&lt;p&gt;So I made the site draw its own cards. Every post, project and page now gets a card made for it, when the site is built. Here is the card for my last post:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://hasibhasnain.com/og/tech/chasing-a-100-on-pagespeed.png" alt="The social card for my PageSpeed post: the title in large serif type on dotted notebook paper, a CSS logo sticker on the right, and the date at the bottom."&gt;&lt;/p&gt;
&lt;p&gt;This post is how that works.&lt;/p&gt;
&lt;div class="blk blk-tldr"&gt;&lt;p class="tldr-h"&gt;In short&lt;/p&gt;&lt;div class="blk-prose"&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;No browser, no service.&lt;/strong&gt; The cards are drawn in C# with SkiaSharp during the Statiq build. 29 cards on the site today.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Bangla needed real text shaping.&lt;/strong&gt; Drawing Bangla letter by letter breaks the joined letters. HarfBuzz fixes it.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The title picks its own size.&lt;/strong&gt; The code tries big sizes first and stops at the first one that fits in three lines.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cards are cached by a hash&lt;/strong&gt; of everything drawn on them, so a build only draws the cards that changed.&lt;/li&gt;
&lt;/ul&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h2 id="why-not-take-a-screenshot"&gt;Why not take a screenshot?&lt;/h2&gt;
&lt;p&gt;The common way to make these cards is to build a small HTML page for the card and take a screenshot of it with a headless browser. Vercel also has a service that does this.&lt;/p&gt;
&lt;p&gt;I didn't want either. A headless browser is a big thing to install just to draw a picture, and it would make every build slower. A service means my build depends on someone else's server. My site is already a C# program (Statiq is a .NET static site generator), so I wanted the cards to be drawn by that same program.&lt;/p&gt;
&lt;p&gt;SkiaSharp is the .NET wrapper around Skia, the graphics library that Chrome and Android use to draw things. It can open a blank image, draw text, shapes and pictures on it, and save it as a PNG. That's all a card needs.&lt;/p&gt;
&lt;h2 id="what-is-on-a-card"&gt;What is on a card&lt;/h2&gt;
&lt;p&gt;A card is 1200 by 630 pixels. That is the size Facebook, LinkedIn and X all expect. Every card has the same parts, so they look like they belong to one site:&lt;/p&gt;
&lt;ol class="blk blk-steps"&gt;&lt;li class=""&gt;&lt;p class="step-h"&gt;The paper&lt;/p&gt;&lt;div class="blk-prose"&gt;&lt;p&gt;Notebook paper, like the site: a dot grid and a double red margin line on the left.&lt;/p&gt;&lt;/div&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;p class="step-h"&gt;A small label&lt;/p&gt;&lt;div class="blk-prose"&gt;&lt;p&gt;Handwritten, in the section's colour. "tech · Tools and AI", for example.&lt;/p&gt;&lt;/div&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;p class="step-h"&gt;The title&lt;/p&gt;&lt;div class="blk-prose"&gt;&lt;p&gt;As big as it can be. More on that below.&lt;/p&gt;&lt;/div&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;p class="step-h"&gt;The description&lt;/p&gt;&lt;div class="blk-prose"&gt;&lt;p&gt;One or two lines, but only if the title left room.&lt;/p&gt;&lt;/div&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;p class="step-h"&gt;A sticker or a photo&lt;/p&gt;&lt;div class="blk-prose"&gt;&lt;p&gt;An icon on a tilted sticker with a piece of tape. Photo stories and the about page get a polaroid instead.&lt;/p&gt;&lt;/div&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;p class="step-h"&gt;The footer&lt;/p&gt;&lt;div class="blk-prose"&gt;&lt;p&gt;My logo, the domain, and a date or a post count.&lt;/p&gt;&lt;/div&gt;&lt;/li&gt;&lt;/ol&gt;
&lt;p&gt;The colours come from &lt;code&gt;data/theme.yml&lt;/code&gt;, the same file the site's CSS is made from. A card can't read CSS, so the renderer reads the theme file and pulls out the ink, paper and accent colours. If I change the theme, the cards change with it.&lt;/p&gt;
&lt;h2 id="making-the-title-fit"&gt;Making the title fit&lt;/h2&gt;
&lt;p&gt;Titles are different lengths. "Records, init and with" is short. Some titles are long enough to fill the card. A fixed font size would make the short ones look lost and the long ones spill off the edge.&lt;/p&gt;
&lt;p&gt;So the renderer tries a list of sizes, biggest first, and stops at the first one that fits in three lines:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-csharp" title="OgCard.cs"&gt;float[] sizes = bn ? new float[] { 60, 54, 48, 42, 38 }
                   : new float[] { 74, 66, 58, 52, 46, 42 };

foreach (float size in sizes)
{
    using var f = new SKFont(bn ? _bnBold : _title, size);
    var lines = Wrap(title, s =&amp;gt; Measure(s, f, bn), maxWidth).ToList();
    if (lines.Count &amp;lt;= 3)
        return (lines, size, size * (bn ? 1.42f : 1.1f));
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;Wrap&lt;/code&gt; is a plain word wrap. It adds words to a line until the next word would make the line too wide, then starts a new line. If even the smallest size needs more than three lines, the third line ends with "…".&lt;/p&gt;
&lt;p&gt;The description has a similar rule. It only shows when the title took two lines or fewer, and it is cut at two lines. When it is cut, the last word is dropped if the "…" wouldn't fit after it. A description that stops halfway through a word looks broken.&lt;/p&gt;
&lt;p&gt;Bangla gets its own sizes and more space between lines. Bangla letters have marks above and below the line, and with English line spacing they touch the line above.&lt;/p&gt;
&lt;h2 id="bangla-text-was-broken-at-first"&gt;Bangla text was broken at first&lt;/h2&gt;
&lt;p&gt;Half my site is in Bangla, so the cards had to work in Bangla too. This was the hardest part.&lt;/p&gt;
&lt;div class="blk blk-ticket sev--high"&gt;&lt;div class="tk-head"&gt;&lt;span class="tk-id"&gt;BUG&lt;/span&gt;&lt;span class="tk-sev"&gt;high&lt;/span&gt;&lt;span class="tk-status"&gt;fixed&lt;/span&gt;&lt;/div&gt;&lt;p class="tk-title"&gt;Bangla titles look wrong on the cards&lt;/p&gt;&lt;dl class="tk-body"&gt;&lt;div&gt;&lt;dt&gt;symptom&lt;/dt&gt;&lt;dd&gt;&lt;p&gt;Bangla titles were readable, but wrong. Joined letters like ক্ষ and ন্ত showed up as two separate letters with a hasanta (্) between them. Vowel signs like ি sat in the wrong place.&lt;/p&gt;&lt;/dd&gt;&lt;/div&gt;&lt;div&gt;&lt;dt&gt;cause&lt;/dt&gt;&lt;dd&gt;&lt;p&gt;&lt;code&gt;DrawText&lt;/code&gt; in SkiaSharp draws one glyph per character, left to right. English works fine that way. Bangla doesn't. The font has special glyphs for joined letters and rules for where vowel signs go, and something has to read those rules and pick the right glyphs. That step is called text shaping, and plain &lt;code&gt;DrawText&lt;/code&gt; doesn't do it.&lt;/p&gt;&lt;/dd&gt;&lt;/div&gt;&lt;div&gt;&lt;dt&gt;fix&lt;/dt&gt;&lt;dd&gt;&lt;p&gt;Run Bangla text through HarfBuzz first, using the &lt;code&gt;SkiaSharp.HarfBuzz&lt;/code&gt; package. HarfBuzz is the shaping engine that browsers use. It takes the text and the font and returns the right glyphs with their exact positions. Then I draw those glyphs.&lt;/p&gt;&lt;/dd&gt;&lt;/div&gt;&lt;div&gt;&lt;dt&gt;lesson&lt;/dt&gt;&lt;dd&gt;&lt;p&gt;If text in your language looks right in a browser and wrong in your own drawing code, the missing piece is probably shaping.&lt;/p&gt;&lt;/dd&gt;&lt;/div&gt;&lt;/dl&gt;&lt;/div&gt;
&lt;p&gt;The drawing part looks like this. HarfBuzz returns a list of glyphs and a position for each one, and Skia draws them as one block of text:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-csharp" title="OgCard.cs"&gt;SKShaper.Result shaped = Shape(shaper, text, font);

using var builder = new SKTextBlobBuilder();
var run = builder.AllocatePositionedRun(font, shaped.Codepoints.Length);
for (int i = 0; i &amp;lt; shaped.Codepoints.Length; i++)
{
    run.Glyphs[i] = (ushort)shaped.Codepoints[i];
    run.Positions[i] = shaped.Points[i];
}

using SKTextBlob? blob = builder.Build();
if (blob != null) c.DrawText(blob, x, y, paint);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The measuring has to be shaped too. If I measured Bangla the simple way and drew it the shaped way, the word wrap would get the widths wrong and lines would run off the card.&lt;/p&gt;
&lt;p&gt;Two smaller things came with it:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Handwriting.&lt;/strong&gt; English labels use Caveat, a handwriting font. Caveat has no Bangla letters, so Bangla labels use Hind Siliguri in bold.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Mixed text.&lt;/strong&gt; A card counts as Bangla if its language is Bangla, or if its title has any Bangla letter in it. That way an English page with a Bangla title still gets the right font.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here is the card for the Bangla tech page:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://hasibhasnain.com/og/bn/tech.png" alt="The social card for the Bangla tech section: the title টেক in bold Bangla type, a Bangla description below it, and a terminal icon on the sticker."&gt;&lt;/p&gt;
&lt;h2 id="drawing-only-what-changed"&gt;Drawing only what changed&lt;/h2&gt;
&lt;p&gt;Drawing a card takes a moment, and with all my drafts and sample posts turned on, the site has more than a hundred of them. I didn't want to draw all of them on every build when I only fixed a typo in one post.&lt;/p&gt;
&lt;p&gt;So every card is saved in a folder called &lt;code&gt;.og-cache&lt;/code&gt;, and the file name is a hash of everything drawn on that card:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-csharp" title="OgCard.cs"&gt;public string Hash()
{
    string photo = Photo is not null &amp;amp;&amp;amp; File.Exists(Photo)
        ? $"{Photo}|{File.GetLastWriteTimeUtc(Photo).Ticks}" : "";
    string key = string.Join("\u001f",
        OgRenderer.Version, SiteData.Profile.Brand, Seo.Host,
        SiteData.Theme.SocialSignature(),
        Title, Description, Lang, Variant, Label, Accent, Icon, Meta,
        photo, Caption ?? "");
    return Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(key)))[..16]
        .ToLowerInvariant();
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If the file for that hash is already there, the build copies it. If not, it draws a new one. Change a title and only that card is drawn again. Change the theme colours and every card is drawn again, because the theme is part of the hash. The GitHub Actions deploy keeps this folder between runs, so the server gets the same benefit.&lt;/p&gt;
&lt;p&gt;There is one thing the hash can't see: the drawing code itself. If I move the sticker 10 pixels to the left, no title or colour changed, so every hash stays the same and the build happily serves the old cards. That is what &lt;code&gt;OgRenderer.Version&lt;/code&gt; is for. It is a number at the top of the renderer, and it is part of every hash. When I change how cards are drawn, I bump it. It is at 8 now, which tells you how many times I've changed my mind about the layout.&lt;/p&gt;
&lt;p&gt;I forgot to bump it more than once, and each time I spent a few minutes confused about why my change wasn't showing up.&lt;/p&gt;
&lt;h2 id="the-apostrophe-bug"&gt;The apostrophe bug&lt;/h2&gt;
&lt;p&gt;The cards were the big part, but I also cleaned up the tags in the page head that point to them. That turned up a bug that had been there for a while.&lt;/p&gt;
&lt;div class="blk blk-ticket sev--medium"&gt;&lt;div class="tk-head"&gt;&lt;span class="tk-id"&gt;BUG&lt;/span&gt;&lt;span class="tk-sev"&gt;medium&lt;/span&gt;&lt;span class="tk-status"&gt;fixed&lt;/span&gt;&lt;/div&gt;&lt;p class="tk-title"&gt;Broken apostrophes in share previews&lt;/p&gt;&lt;dl class="tk-body"&gt;&lt;div&gt;&lt;dt&gt;symptom&lt;/dt&gt;&lt;dd&gt;&lt;p&gt;Some link previews showed the description with &lt;code&gt;&amp;amp;#x27;&lt;/code&gt; where an apostrophe should be. "doesn't" came out as "doesn't".&lt;/p&gt;&lt;/dd&gt;&lt;/div&gt;&lt;div&gt;&lt;dt&gt;cause&lt;/dt&gt;&lt;dd&gt;&lt;p&gt;The text was escaped twice. Razor turns &lt;code&gt;'&lt;/code&gt; into &lt;code&gt;&amp;amp;#x27;&lt;/code&gt; when it writes an attribute. A later step in the build escaped the head again, so the &lt;code&gt;&amp;amp;&lt;/code&gt; became &lt;code&gt;&amp;amp;amp;&lt;/code&gt;. Browsers undo one layer, so the page looked fine. Link preview bots showed what was left.&lt;/p&gt;&lt;/dd&gt;&lt;/div&gt;&lt;div&gt;&lt;dt&gt;fix&lt;/dt&gt;&lt;dd&gt;&lt;p&gt;A small &lt;code&gt;Seo.Attr()&lt;/code&gt; helper that escapes only the four characters that matter inside a double-quoted attribute (&lt;code&gt;&amp;amp;&lt;/code&gt;, &lt;code&gt;"&lt;/code&gt;, &lt;code&gt;&amp;lt;&lt;/code&gt; and &lt;code&gt;&amp;gt;&lt;/code&gt;), and hands Razor text that it won't escape again.&lt;/p&gt;&lt;/dd&gt;&lt;/div&gt;&lt;div&gt;&lt;dt&gt;lesson&lt;/dt&gt;&lt;dd&gt;&lt;p&gt;Check your link previews with a real preview tool, not just the page. The bug was invisible in the browser.&lt;/p&gt;&lt;/dd&gt;&lt;/div&gt;&lt;/dl&gt;&lt;/div&gt;
&lt;p&gt;While I was there, I added the tags that were missing: the image size, its type, alt text for the image, and the &lt;code&gt;twitter:&lt;/code&gt; tags so X shows a large card too.&lt;/p&gt;
&lt;h2 id="what-id-still-change"&gt;What I'd still change&lt;/h2&gt;
&lt;p&gt;The cache never deletes anything. Every time a title or a description changes, a new card is saved and the old one stays. The site has 29 cards right now, but my local &lt;code&gt;.og-cache&lt;/code&gt; folder has about 1,600 files in it, around 100 MB. It doesn't break anything, since the build only copies the cards it needs. But a small cleanup step that removes cards no page asked for would be easy to add, and I should add it.&lt;/p&gt;
&lt;h2 id="what-i-learned"&gt;What I learned&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;You don't need a browser to draw an image.&lt;/strong&gt; For a fixed layout like this, a graphics library is smaller, faster and easier to run in CI.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Text is the hard part, not shapes.&lt;/strong&gt; Fitting the title, cutting the description and shaping Bangla took far more code than the paper, the sticker and the tape.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Put everything you draw in the cache key&lt;/strong&gt;, and add a version number for the code itself, because the code is the one input a hash can't see.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Test the preview, not the page.&lt;/strong&gt; Link preview bots read your HTML differently from a browser.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now when I share a post, the preview tells you what the post is about before you open it. That was the whole point.&lt;/p&gt;
</content:encoded>
			<comments xmlns="http://purl.org/rss/1.0/modules/slash/">0</comments>
		</item>
		<item>
			<title>Chasing a 100 on PageSpeed</title>
			<link>https://hasibhasnain.com/tech/chasing-a-100-on-pagespeed</link>
			<description>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.</description>
			<author>S M Hasib Hasnain</author>
			<guid>https://hasibhasnain.com/tech/chasing-a-100-on-pagespeed</guid>
			<pubDate>Wed, 02 Dec 2026 00:00:00 GMT</pubDate>
			<content:encoded>&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;This post is the list of things I changed, in the order I changed them, with the numbers from each step.&lt;/p&gt;
&lt;div class="blk blk-tldr"&gt;&lt;p class="tldr-h"&gt;In short&lt;/p&gt;&lt;div class="blk-prose"&gt;&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Fonts were the biggest cost.&lt;/strong&gt; I moved them onto my own domain and cut them down to the letters each page uses.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Most of the CSS was never used.&lt;/strong&gt; Each page now gets a stylesheet with only the rules it can use: 9 KB instead of 24 KB.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Small warnings add up.&lt;/strong&gt; A too-large photo, a slow animation and a script that read the layout too early each cost a little.&lt;/li&gt;
&lt;li&gt;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.&lt;/li&gt;
&lt;/ul&gt;&lt;/div&gt;&lt;/div&gt;
&lt;h2 id="what-pagespeed-measures"&gt;What PageSpeed measures&lt;/h2&gt;
&lt;p&gt;PageSpeed gives a score from 0 to 100. Most of it comes from a few timings:&lt;/p&gt;
&lt;div class="blk blk-gloss"&gt;&lt;p class="gloss-h"&gt;&lt;span class="hand"&gt;words in this post&lt;/span&gt;&lt;/p&gt;&lt;dl&gt;&lt;div&gt;&lt;dt lang="en"&gt;LCP&lt;/dt&gt;&lt;dd&gt;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.&lt;/dd&gt;&lt;/div&gt;&lt;div&gt;&lt;dt lang="en"&gt;TBT&lt;/dt&gt;&lt;dd&gt;Total Blocking Time. How long the browser is too busy to react to a tap.&lt;/dd&gt;&lt;/div&gt;&lt;div&gt;&lt;dt lang="en"&gt;CLS&lt;/dt&gt;&lt;dd&gt;Cumulative Layout Shift. How much the page jumps around while it loads.&lt;/dd&gt;&lt;/div&gt;&lt;div&gt;&lt;dt lang="en"&gt;Render-blocking&lt;/dt&gt;&lt;dd&gt;a file the browser must download before it can draw anything, like a stylesheet in the head.&lt;/dd&gt;&lt;/div&gt;&lt;/dl&gt;&lt;/div&gt;
&lt;p&gt;The mobile test is the hard one. It pretends to be a slow phone on a slow network, so every extra kilobyte shows.&lt;/p&gt;
&lt;h2 id="the-plan"&gt;The plan&lt;/h2&gt;
&lt;ol class="blk blk-steps"&gt;&lt;li class="is-done"&gt;&lt;p class="step-h"&gt;Serve the fonts myself&lt;/p&gt;&lt;/li&gt;&lt;li class="is-done"&gt;&lt;p class="step-h"&gt;Cut the display font down to my name&lt;/p&gt;&lt;/li&gt;&lt;li class="is-done"&gt;&lt;p class="step-h"&gt;Fix the colour contrast&lt;/p&gt;&lt;/li&gt;&lt;li class="is-done"&gt;&lt;p class="step-h"&gt;Stop Bangla pages from loading English fonts&lt;/p&gt;&lt;/li&gt;&lt;li class="is-done"&gt;&lt;p class="step-h"&gt;Make the site work offline&lt;/p&gt;&lt;/li&gt;&lt;li class="is-done"&gt;&lt;p class="step-h"&gt;Clear the render, reflow and animation warnings&lt;/p&gt;&lt;/li&gt;&lt;li class="is-done"&gt;&lt;p class="step-h"&gt;Send a smaller photo to normal screens&lt;/p&gt;&lt;/li&gt;&lt;li class="is-done"&gt;&lt;p class="step-h"&gt;Give each page its own stylesheet&lt;/p&gt;&lt;/li&gt;&lt;/ol&gt;
&lt;h2 id="the-execution"&gt;The Execution&lt;/h2&gt;
&lt;h3 id="fonts-come-from-my-own-domain"&gt;Fonts come from my own domain&lt;/h3&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Variable fonts.&lt;/strong&gt; One file covers every weight, so I don't need a file for 400, another for 500 and another for 700.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Preload.&lt;/strong&gt; The body and heading fonts are preloaded, so the browser starts fetching them right away.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The logo became an SVG.&lt;/strong&gt; 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.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;div class="blk blk-bench"&gt;&lt;p class="bench-title"&gt;Fonts on an English page&lt;span&gt;lower is better&lt;/span&gt;&lt;/p&gt;&lt;ul class="bench-rows"&gt;&lt;li class=""&gt;&lt;span class="bench-label"&gt;Before&lt;/span&gt;&lt;span class="bench-track"&gt;&lt;i style="width:100%"&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class="bench-val"&gt;630 KB&lt;/span&gt;&lt;span class="bench-note"&gt;from Google Fonts&lt;/span&gt;&lt;/li&gt;&lt;li class=""&gt;&lt;span class="bench-label"&gt;After, heaviest page&lt;/span&gt;&lt;span class="bench-track"&gt;&lt;i style="width:75.4%"&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class="bench-val"&gt;475 KB&lt;/span&gt;&lt;span class="bench-note"&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="is-best"&gt;&lt;span class="bench-label"&gt;After, lightest page&lt;/span&gt;&lt;span class="bench-track"&gt;&lt;i style="width:42.86%"&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class="bench-val"&gt;270 KB&lt;/span&gt;&lt;span class="bench-note"&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;
&lt;h3 id="five-letters-150-kb"&gt;Five letters, 150 KB&lt;/h3&gt;
&lt;p&gt;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 &amp;quot;loaded&amp;quot; until that font arrives.&lt;/p&gt;
&lt;p&gt;The whole italic font was 150 KB, and the page used it for five letters.&lt;/p&gt;
&lt;p&gt;So the font script now makes a second, tiny font called &amp;quot;Display Name&amp;quot;. It has only the letters of my short name, with every weight and style axis kept. The home page preloads it.&lt;/p&gt;
&lt;div class="blk blk-bench"&gt;&lt;p class="bench-title"&gt;Italic font for the greeting&lt;span&gt;lower is better&lt;/span&gt;&lt;/p&gt;&lt;ul class="bench-rows"&gt;&lt;li class=""&gt;&lt;span class="bench-label"&gt;Whole font&lt;/span&gt;&lt;span class="bench-track"&gt;&lt;i style="width:100%"&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class="bench-val"&gt;150 KB&lt;/span&gt;&lt;span class="bench-note"&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="is-best"&gt;&lt;span class="bench-label"&gt;Name-only cut&lt;/span&gt;&lt;span class="bench-track"&gt;&lt;i style="width:5.33%"&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class="bench-val"&gt;8 KB&lt;/span&gt;&lt;span class="bench-note"&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;
&lt;dl class="blk blk-stats"&gt;&lt;div&gt;&lt;dt&gt;Mobile Lighthouse, home page&lt;/dt&gt;&lt;dd&gt;86 → 90&lt;/dd&gt;&lt;p class="stat-note"&gt;&lt;/p&gt;&lt;/div&gt;&lt;div&gt;&lt;dt&gt;LCP, home page&lt;/dt&gt;&lt;dd&gt;4.3 s → 3.6 s&lt;/dd&gt;&lt;p class="stat-note"&gt;mobile&lt;/p&gt;&lt;/div&gt;&lt;/dl&gt;
&lt;p&gt;That was the biggest single jump in the whole project.&lt;/p&gt;
&lt;h3 id="a-colour-that-was-almost-dark-enough"&gt;A colour that was almost dark enough&lt;/h3&gt;
&lt;p&gt;This one was not about speed. PageSpeed also checks accessibility, and it flagged the small grey text on my project cards.&lt;/p&gt;
&lt;div class="blk blk-ticket sev--low"&gt;&lt;div class="tk-head"&gt;&lt;span class="tk-id"&gt;BUG&lt;/span&gt;&lt;span class="tk-sev"&gt;low&lt;/span&gt;&lt;span class="tk-status"&gt;fixed&lt;/span&gt;&lt;/div&gt;&lt;p class="tk-title"&gt;Grey text fails contrast&lt;/p&gt;&lt;dl class="tk-body"&gt;&lt;div&gt;&lt;dt&gt;symptom&lt;/dt&gt;&lt;dd&gt;&lt;p&gt;PageSpeed said the type line on the project cards was hard to read.&lt;/p&gt;&lt;/dd&gt;&lt;/div&gt;&lt;div&gt;&lt;dt&gt;cause&lt;/dt&gt;&lt;dd&gt;&lt;p&gt;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.&lt;/p&gt;&lt;/dd&gt;&lt;/div&gt;&lt;div&gt;&lt;dt&gt;fix&lt;/dt&gt;&lt;dd&gt;&lt;p&gt;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).&lt;/p&gt;&lt;/dd&gt;&lt;/div&gt;&lt;div&gt;&lt;dt&gt;lesson&lt;/dt&gt;&lt;dd&gt;&lt;p&gt;&amp;quot;Almost passes&amp;quot; is a fail. Check a colour on every background it sits on, not only the one the tool happens to test.&lt;/p&gt;&lt;/dd&gt;&lt;/div&gt;&lt;/dl&gt;&lt;/div&gt;
&lt;h3 id="bangla-pages-were-loading-english-fonts"&gt;Bangla pages were loading English fonts&lt;/h3&gt;
&lt;p&gt;The site has Bangla pages too. I expected them to load only Bangla fonts. They didn't.&lt;/p&gt;
&lt;p&gt;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 &lt;strong&gt;Latin&lt;/strong&gt; file, not the Bengali one. So a Bangla heading downloaded a whole English font just to draw its spaces.&lt;/p&gt;
&lt;p&gt;The fix was to build a twin of each font, like &amp;quot;Fraunces BN&amp;quot;. 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.&lt;/p&gt;
&lt;p&gt;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 &amp;quot;just in case&amp;quot; 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.&lt;/p&gt;
&lt;dl class="blk blk-stats"&gt;&lt;div&gt;&lt;dt&gt;LCP on Bangla pages&lt;/dt&gt;&lt;dd&gt;5.1 s → 4.7 s&lt;/dd&gt;&lt;p class="stat-note"&gt;mobile&lt;/p&gt;&lt;/div&gt;&lt;/dl&gt;
&lt;h3 id="offline-reading"&gt;Offline reading&lt;/h3&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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 &lt;code&gt;site.css?v=3fa9c1&lt;/code&gt;. New content, new link, no stale file.&lt;/p&gt;
&lt;h3 id="three-small-warnings"&gt;Three small warnings&lt;/h3&gt;
&lt;p&gt;PageSpeed had three more warnings. Each was small. Together they mattered.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;A second render-blocking stylesheet.&lt;/strong&gt; The font rules lived in their own file, &lt;code&gt;fonts.css&lt;/code&gt;. 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.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;A script that read the layout too early.&lt;/strong&gt; 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.&lt;/p&gt;
&lt;div class="blk blk-diff"&gt;&lt;p class="diff-title"&gt;site.js&lt;/p&gt;&lt;div class="diff-split"&gt;&lt;div class="diff-old"&gt;&lt;span class="ep-label"&gt;before&lt;/span&gt;&lt;pre class="blk-code"&gt;&lt;code class="language-js"&gt;var state = 'full', fullH = top.offsetHeight;
root.style.setProperty('--top-h', fullH + 'px');&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="diff-new"&gt;&lt;span class="ep-label"&gt;after&lt;/span&gt;&lt;pre class="blk-code"&gt;&lt;code class="language-js"&gt;var state = 'full', fullH = 0;
// read the height after layout, and again when it changes
new ResizeObserver(measure).observe(top);&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;A &lt;code&gt;ResizeObserver&lt;/code&gt; runs after the browser has done its layout anyway, so reading the height there is free.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;An animation the GPU couldn't run.&lt;/strong&gt; There is a hand-drawn underline under my name that draws itself from left to right. It animated &lt;code&gt;stroke-dashoffset&lt;/code&gt;, which the browser has to repaint on the main thread, frame by frame.&lt;/p&gt;
&lt;p&gt;Now it is a &amp;quot;wipe&amp;quot;: 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 &lt;code&gt;transform&lt;/code&gt;, which the GPU handles off the main thread.&lt;/p&gt;
&lt;div class="blk blk-diff"&gt;&lt;p class="diff-title"&gt;The underline&lt;/p&gt;&lt;div class="diff-split"&gt;&lt;div class="diff-old"&gt;&lt;span class="ep-label"&gt;before&lt;/span&gt;&lt;pre class="blk-code"&gt;&lt;code class="language-css"&gt;.name path {
  stroke-dasharray: 420;
  stroke-dashoffset: 420;
  animation: draw 0.9s forwards;
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class="diff-new"&gt;&lt;span class="ep-label"&gt;after&lt;/span&gt;&lt;pre class="blk-code"&gt;&lt;code class="language-css"&gt;.name .ul  { overflow: hidden; animation: wipe-box 0.9s both; }
.name svg  { animation: wipe-line 0.9s both; }
 
&amp;#64;keyframes wipe-box  { from { transform: translateX(-100%); } }
&amp;#64;keyframes wipe-line { from { transform: translateX(100%); } }&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;h3 id="a-photo-that-was-too-big"&gt;A photo that was too big&lt;/h3&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;The build now makes a 300 px WebP copy next to the original. The image tag gets a &lt;code&gt;srcset&lt;/code&gt;, so the browser picks the right one:&lt;/p&gt;
&lt;pre&gt;&lt;code class="language-html"&gt;&amp;lt;img src=&amp;quot;/me.webp&amp;quot;
     srcset=&amp;quot;/me-300.webp 300w, /me.webp 400w&amp;quot;
     sizes=&amp;quot;(max-width: 640px) min(232px, calc(80.5vw - 32px)),
            (max-width: 960px) 244px, 290px&amp;quot;
     width=&amp;quot;252&amp;quot; height=&amp;quot;290&amp;quot; fetchpriority=&amp;quot;high&amp;quot; alt=&amp;quot;...&amp;quot;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Normal screens get the small copy. Phones and high-density screens still get the full one, so the photo never looks soft.&lt;/p&gt;
&lt;div class="blk blk-bench"&gt;&lt;p class="bench-title"&gt;Profile photo&lt;span&gt;lower is better&lt;/span&gt;&lt;/p&gt;&lt;ul class="bench-rows"&gt;&lt;li class=""&gt;&lt;span class="bench-label"&gt;Original&lt;/span&gt;&lt;span class="bench-track"&gt;&lt;i style="width:100%"&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class="bench-val"&gt;19 KB&lt;/span&gt;&lt;span class="bench-note"&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="is-best"&gt;&lt;span class="bench-label"&gt;300 px copy&lt;/span&gt;&lt;span class="bench-track"&gt;&lt;i style="width:42.11%"&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class="bench-val"&gt;8 KB&lt;/span&gt;&lt;span class="bench-note"&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;
&lt;h3 id="each-page-gets-its-own-stylesheet"&gt;Each page gets its own stylesheet&lt;/h3&gt;
&lt;p&gt;The last warning was &amp;quot;reduce unused CSS&amp;quot;. The site has one stylesheet for everything: the home page, posts, project pages, photo galleries. Any single page uses a small part of it.&lt;/p&gt;
&lt;p&gt;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:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Collect every class and id used in the page's HTML.&lt;/li&gt;
&lt;li&gt;Collect every word in the page's scripts, because a script can add a class later.&lt;/li&gt;
&lt;li&gt;Go through the stylesheet and drop a rule only if &lt;strong&gt;none&lt;/strong&gt; of its selectors could match.&lt;/li&gt;
&lt;li&gt;Save what is left as &lt;code&gt;/css/p/&amp;lt;hash&amp;gt;.css&lt;/code&gt; and link it from the page.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;I kept the rules safe on purpose. Rules stay in the same order, so the cascade doesn't change. Anything tricky, like &lt;code&gt;:not()&lt;/code&gt;, &lt;code&gt;:is()&lt;/code&gt;, &lt;code&gt;:has()&lt;/code&gt; or &lt;code&gt;&amp;#64;keyframes&lt;/code&gt;, is kept whole. A few extra bytes are better than a broken page.&lt;/p&gt;
&lt;div class="blk blk-bench"&gt;&lt;p class="bench-title"&gt;CSS on the home page, gzipped&lt;span&gt;lower is better&lt;/span&gt;&lt;/p&gt;&lt;ul class="bench-rows"&gt;&lt;li class=""&gt;&lt;span class="bench-label"&gt;One stylesheet for the site&lt;/span&gt;&lt;span class="bench-track"&gt;&lt;i style="width:100%"&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class="bench-val"&gt;24 KB&lt;/span&gt;&lt;span class="bench-note"&gt;&lt;/span&gt;&lt;/li&gt;&lt;li class="is-best"&gt;&lt;span class="bench-label"&gt;Cut for this page&lt;/span&gt;&lt;span class="bench-track"&gt;&lt;i style="width:37.5%"&gt;&lt;/i&gt;&lt;/span&gt;&lt;span class="bench-val"&gt;9 KB&lt;/span&gt;&lt;span class="bench-note"&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;
&lt;h4 id="what-i-tried-first"&gt;What I tried first&lt;/h4&gt;
&lt;div class="blk blk-hypo hypo--wrong"&gt;&lt;div class="hypo-row"&gt;&lt;span class="hand"&gt;I thought&lt;/span&gt;&lt;div class="blk-prose"&gt;&lt;p&gt;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.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hypo-row"&gt;&lt;span class="hand"&gt;I tried&lt;/span&gt;&lt;div class="blk-prose"&gt;&lt;p&gt;I wrote each page's CSS into a style tag in its head and ran mobile Lighthouse again.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="hypo-row"&gt;&lt;span class="hand"&gt;what happened&lt;/span&gt;&lt;div class="blk-prose"&gt;&lt;p&gt;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.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;span class="stamp hypo-stamp"&gt;wrong&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;h2 id="what-i-learned"&gt;What I learned&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Fonts first.&lt;/strong&gt; On a text-heavy site, fonts were most of the weight and most of the LCP. Cut them to what each page uses.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Look at your LCP element.&lt;/strong&gt; For me it was five italic letters. Once I knew that, the fix was obvious.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Read the warning, then read the cause.&lt;/strong&gt; &amp;quot;Bangla page loads Latin font&amp;quot; sounded like a bug in my CSS. It was the space character.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Measure every change.&lt;/strong&gt; Inlining the CSS looked like a clear win and made things worse. I only knew because I ran the test again.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Check nothing broke.&lt;/strong&gt; Screenshots of every page before and after let me change a lot without fear.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;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.&lt;/p&gt;
</content:encoded>
			<comments xmlns="http://purl.org/rss/1.0/modules/slash/">0</comments>
		</item>
	</channel>
</rss>