Skip to content

Engineering

Shipping genuinely fast websites with Astro

Why static-first architecture wins for content sites, and the concrete techniques I use to keep Lighthouse scores at 100 without giving up interactivity.

Alex CarterUpdated5 min read

Every few years the pendulum swings between “ship everything as JavaScript” and “ship as little JavaScript as possible”. For content sites (blogs, portfolios, docs, marketing pages) the second camp has clearly won, and Astro is the sharpest tool in it.

This post is the playbook I use on every content site I build. None of it is exotic. The boring techniques, applied consistently, get you to a Lighthouse 100 that stays at 100 after the marketing team adds their fourth embed.

The islands model, briefly

Astro renders your entire page to static HTML at build time. Components that need interactivity opt in explicitly:

src/pages/index.astro
---
import Chart from '../components/Chart.tsx';
---
<!-- Rendered to HTML, zero JS shipped -->
<h1>Traffic report</h1>
<!-- Hydrated only when it scrolls into view -->
<Chart client:visible />

Everything else (headers, footers, article bodies, cards) ships as plain HTML and CSS. The result is that your baseline JavaScript cost is close to zero, and every kilobyte you do ship is a deliberate decision.

Measure before you optimize

Before touching anything, get a baseline you can trust. Lab numbers from your laptop lie. I use three sources:

SourceWhat it tells youWhen to check
Lighthouse (CI)Regressions between commitsEvery pull request
WebPageTest, 3G/MotoWhat a real slow device experiencesBefore and after a change
CrUX / field dataWhat your visitors actually experiencedMonthly

The field data is the one that counts. A site can score 100 in the lab and still have a poor Largest Contentful Paint (LCP) in the field because the hero image is served from an origin two continents away.

Terminal window
# One-off lab run with a throttled mobile profile
npx lighthouse https://example.com --preset=perf --form-factor=mobile --throttling-method=simulate

Techniques that actually move the needle

1. Prefetch on hover and viewport

Astro’s built-in prefetch turns full page loads into near-instant navigations:

astro.config.ts
export default defineConfig({
site: 'https://example.com',
prefetch: {
prefetchAll: true,
defaultStrategy: 'viewport',
},
});

By the time a visitor clicks a link, the next page’s HTML is usually already in the cache. You get SPA-feeling navigation without a router, a hydration step, or a client-side state bug budget.

2. Self-host your fonts

A @fontsource import costs one line and removes an entire third-party origin:

Terminal window
npm install @fontsource-variable/inter
Terminal window
pnpm add @fontsource-variable/inter
Terminal window
bun add @fontsource-variable/inter
src/styles/global.css
@import '@fontsource-variable/inter';

No connection setup to fonts.googleapis.com, no layout shift from late-loading fonts, no consent-mode headaches. Variable fonts mean one file covers every weight you use, and the package ships unicode-range subsets so a Latin-only page never downloads Cyrillic glyphs.

Then preload the one file that matters for first paint:

src/layouts/BaseLayout.astro
---
import inter from '@fontsource-variable/inter/files/inter-latin-wght-normal.woff2?url';
---
<link rel="preload" href={inter} as="font" type="font/woff2" crossorigin />

That single tag moves the font request from “after CSS is parsed” to “in parallel with CSS”. On a throttled connection it is worth 200–400ms of LCP.

3. Optimize the images you forgot about

Hero images get attention; the avatar in your footer doesn’t. Audit with the network tab sorted by size. The offenders are rarely where you expect.

Astro’s image pipeline handles the mechanical part. Keep images next to your content, reference them in frontmatter, and let the build produce the sizes:

---
import { Image } from 'astro:assets';
---
<Image
src={post.data.image}
alt=""
widths={[720, 1080, 1440]}
sizes="(min-width: 768px) 704px, calc(100vw - 2.5rem)"
loading="eager"
fetchpriority="high"
/>

Three rules that cover 95% of cases:

  1. Explicit dimensions everywhere. The browser reserves space before the image arrives, so nothing jumps.
  2. loading="eager" + fetchpriority="high" on the LCP image only. Everything below the fold is lazy.
  3. WebP by default, AVIF when the build budget allows. The compression gap between PNG and WebP for screenshots is routinely 10×.

4. Ship CSS, not a CSS runtime

Tailwind 4 compiles to a single stylesheet with only the classes you use. On this site that is around 13KB gzipped for every page, cached once. Compare that with a CSS-in-JS runtime that has to execute before it can paint anything.

The second half of this rule: avoid @import chains at runtime. One stylesheet, preloaded by the HTML parser, beats five small ones discovered one after another.

5. Keep third-party scripts off the critical path

Analytics, comments, embeds: each one is a negotiation. My defaults:

Script typeStrategy
Analyticsdefer, or a server-side proxy with no client script
Comments (giscus)Lazy-load when the section scrolls into view
Video embedsStatic thumbnail; load the player on click
Chat widgetsDon’t

If a script cannot be deferred, it needs a written justification. That sounds bureaucratic until the fourth marketing tag shows up.

What I stopped worrying about

  • Client-side routing. Prefetched MPA navigation is indistinguishable for content sites.
  • Skeleton screens. If the page renders in under 100ms, a skeleton is just a flash of fake content.
  • Complex state management. Static pages don’t have state. This is a feature.
  • Service workers. The HTTP cache plus prefetch covers the realistic cases, with none of the update-staleness bugs.

The scoreboard

This theme, the one you’re reading, ships roughly 5KB of JavaScript on a typical page: the theme toggle, mobile navigation, and a lazily-loaded search index. The rest is HTML, one stylesheet, and two self-hosted fonts.

MetricBefore (SPA rewrite)After (Astro)
JavaScript per page284 KB5 KB
Largest Contentful Paint2.9 s0.4 s
Total Blocking Time410 ms0 ms
Lighthouse performance71100

Lighthouse gives it a 100 in every category, and more importantly, it feels instant on a mid-range Android phone over a mediocre connection, which is where most of the web actually lives.

Share
All articles

Alex Carter

Full-stack Developer

Get new posts by email

One email when I publish something new. No digests, no spam, unsubscribe in one click.

No spam, ever. Unsubscribe with one click.

Related articles

↑↓ navigate↵ openesc close