All writing

How This Site Pulls Live Preview Images and Devlogs from My Projects

By Bronson Dunbar ·

A portfolio goes stale the moment you ship something new. You redesign a product's preview image and the portfolio still shows the old one. You ship a big update and the project page doesn't mention it.

So this site doesn't keep its own copies. Every project tile shows that project's live link-preview image, the same one you see when you share its URL, and every game page shows its latest devlog entries. Both are read from the live sites on the server and refreshed automatically. This post explains how, and what makes it safe.

The image every project already has

Every site I build has an Open Graph image: the 1200 × 630 picture that appears when someone shares the link. It's already designed, already the right size, and I already keep it up to date. That makes it the perfect project thumbnail.

To get it, the site fetches the project's homepage on the server and reads the meta tags:

const meta = (key: string) =>
  doc.querySelector(`meta[property="${key}"]`)?.getAttribute("content") ??
  doc.querySelector(`meta[name="${key}"]`)?.getAttribute("content");

const raw = meta("og:image") ?? meta("og:image:url") ?? meta("twitter:image");

A few rules keep this robust:

  • Relative URLs are resolved against the page URL, because some sites publish og:image as /og.png.
  • Only https: images are accepted.
  • The size is always treated as 1200 × 630. Sites often declare dimensions that don't match the real file, and every place this site shows the image crops to that frame anyway.
  • The alt text comes from og:image:alt when the site provides one.

The caching problem

Next.js caches remote images aggressively, as it should. But that creates a problem: if a site replaces its preview image at the same URL, every cache keeps serving the old one. The image optimiser has a copy, the CDN has a copy and visitors' browsers have a copy.

The fix is to put the image's content in its URL. The site downloads the image, hashes it, and adds the hash as a query parameter:

export async function versionImageUrl(src: string): Promise<string> {
  const response = await fetch(src, { cache: "no-store", signal: AbortSignal.timeout(8000) });
  if (!response.ok || !response.headers.get("content-type")?.startsWith("image/")) {
    throw new Error(`Invalid OG image response (${response.status})`);
  }
  const bytes = await response.arrayBuffer();
  const version = createHash("sha256").update(new Uint8Array(bytes)).digest("hex").slice(0, 16);
  const url = new URL(src);
  url.searchParams.set("portfolio-version", version);
  return url.toString();
}

When the image changes, the hash changes, so the URL changes, so every cache treats it as a new image. When it doesn't change, the URL stays the same and every cache stays warm.

Two details:

  • Only the resulting URL is cached (for five minutes), not the image bytes. Next's data cache is the wrong place for megabytes of image data, and the image optimiser keeps its own copy anyway.
  • The preview metadata and the image hash use the same five-minute window, so they can never disagree for long.

Fallbacks, in order

The live site might be down, slow, or might not publish an image. So each project has an order of preference:

  1. The live site's og:image
  2. A stored thumbnail (in Sanity or the local project file)
  3. The project's cover image

Every remote request has an 8-second timeout. Any failure, whether a timeout, a non-HTML response or a non-image response, returns null and the next option is used. A project can also opt out entirely with liveOgImage: false and keep a pinned image. I currently do that for Ash & Oath, because its live site still points to older, unbranded artwork.

Only optimise images from hosts I trust

Next's image optimiser only processes images from hosts you list in the config. Rather than keep that list by hand, it's generated from the project list itself, plus Sanity's CDN:

export const optimisedHosts = [
  "cdn.sanity.io",
  ...new Set(projects.map((p) => new URL(p.url).hostname)),
];

Adding a project automatically allows its domain. An image from anywhere else (say, a project added in Sanity with a new domain before the config catches up) is shown unoptimised rather than breaking the page.

Devlogs, the same way

Game pages show the latest five entries from each game's public /devlog page, refreshed hourly. Every game renders its devlog with the same HTML structure, so one parser reads them all, and it keeps only the text: no HTML from another site is ever rendered here. I wrote about the devlog side in How I add a devlog to every game.

Failing well

There's one more rule, and it matters most when Sanity (the CMS) is involved: an error is not the same as "no content". If Sanity is unreachable, requests fail rather than returning an empty list, because an empty list would get cached and published as "this site has no projects". Next.js keeps serving the last good version of a page while background refreshes fail, so a temporary outage is invisible to visitors.

For the remote extras (preview images and devlogs) it's the opposite: they're optional, so a failure quietly falls back or leaves the section out.

Why bother?

Because a portfolio that maintains itself is one that stays accurate. When I update a project's preview image, this site follows within five minutes. When I ship a game update and log it, it appears here within the hour. I haven't touched a project tile by hand since.