Skip to content
Rohan Khatua

Writing

Why My Link Previews Showed No Image on WhatsApp

5 min readweb

When I shared rohankhatua.dev on WhatsApp, the preview showed no image. I assumed the Open Graph tags were missing. They weren't: the Next.js layout already declared a full openGraph block with a 1200×630 image. So the question was what production actually served.

Look at what production serves

The tags were fine. The image they pointed to was not:

bash
curl -sI -A "WhatsApp/2.23.20.0" https://rohankhatua.dev/og-image.jpg
# HTTP/2 200
# content-type: image/jpeg

file public/og-image.jpg
# public/og-image.jpg: PNG image data, 800 x 800, 8-bit/color RGBA, non-interlaced

The file called og-image.jpg was really an 800×800 PNG. It was served as a JPEG, the tags said it was 1200×630, and it weighed 313 KB. Here's the full list of what was wrong:

ProblemEffect
og-image.jpg was an 800×800 PNG, declared as a 1200×630 JPEGApps that check the type or size can reject it
It was 313 KBWhatsApp drops preview images over roughly 300 KB
/projects and /blog had no openGraph of their ownSharing them showed the home page's title and link
Blog posts without a coverShowed no image at all

The Next.js metadata trap

The third and fourth rows come from how Next.js merges metadata. It's a shallow merge: if a page sets openGraph at all, it replaces the layout's entire openGraph object, image included. Blog posts set their own openGraph, so a post without a cover had no image. And /projects and /blog set nothing, so they inherited the home page's URL and title.

The Next.js docs suggest pulling shared fields into a variable and spreading it. I put the card and the shared fields in lib/site.ts:

typescript
export const OG_IMAGE = {
  url: `${SITE_URL}/og.png`,
  width: 1200,
  height: 630,
  type: 'image/png',
  alt: 'Rohan Khatua, beside a chessboard of his interests and projects',
};

export const sharedOpenGraph = {
  type: 'website' as const,
  locale: 'en_US',
  siteName: 'Rohan Khatua',
  images: [OG_IMAGE],
};

Every page now spreads ...sharedOpenGraph and adds its own url, title and description. Blog posts use their cover if they have one and fall back to OG_IMAGE if not. The Twitter tags had the same problem: every subpage inherited the home page's twitter:title. Once I removed the title and description from the layout's Twitter block, each page's Twitter tags followed its own Open Graph title and description.

Drawing the card

The home page has a chessboard of my interests and projects, and I wanted the card to show it. Next has an opengraph-image.tsx convention that renders the image on request, but I wasn't confident ImageResponse would work on Cloudflare Workers through OpenNext, and it would bundle WebAssembly into the Worker. So I generate a static PNG with a script, bun run og, and commit the result.

The script uses ImageResponse from next/og and builds the board from the same data files the home page uses. To reuse the real piece and icon components, it renders each one to SVG markup and swaps the CSS variables for hex colours, since the image renderer can't read the site's stylesheet:

typescript
/** A component's SVG as an image, with the CSS variables it uses resolved. */
function svg(element: React.ReactElement, currentColor: string) {
  const markup = renderToStaticMarkup(element)
    .replace(/ class="[^"]*"/, '')
    .replace(/color-mix\(in srgb, var\(--ink\) 72%, var\(--paper\)\)/g, '#474747')
    .replace(/var\(--(paper|ink)\)/g, (_, name: 'paper' | 'ink') => color[name])
    .replace(/currentColor/g, currentColor)
    .replace('<svg', '<svg xmlns="http://www.w3.org/2000/svg"');
  return `data:image/svg+xml;base64,${Buffer.from(markup).toString('base64')}`;
}

Fonts were the other snag. The renderer can't use variable fonts or WOFF2, so the script asks Google Fonts' CSS API for a specific width and weight of Mona Sans and downloads the static TTF it links to. The first card came out at 1200×630 and 56 KB.

A test that checks the file against the tags

The original bug was a file that didn't match its own metadata, so the test checks exactly that. It reads the PNG header and compares it with OG_IMAGE:

typescript
describe('link-preview image', () => {
  const png = readFileSync(new URL('../public/og.png', import.meta.url));

  test('is a PNG of the declared size', () => {
    expect(png.subarray(1, 4).toString('ascii')).toBe('PNG');
    expect(OG_IMAGE.type).toBe('image/png');
    expect(png.readUInt32BE(16)).toBe(OG_IMAGE.width);
    expect(png.readUInt32BE(20)).toBe(OG_IMAGE.height);
  });

  test('is small enough for WhatsApp (under 300 KB)', () => {
    expect(png.length).toBeLessThan(300 * 1024);
  });
});

In a PNG, the width and height sit at bytes 16 and 20 of the IHDR chunk, so there's no need for an image library.

Then WhatsApp cropped it

The first card shipped, and the preview finally had an image. It also cut my name and the board in half. In the chat preview I was looking at, WhatsApp showed only a square from the middle of the 1200×630 image.

So I redrew it around that square. My name sits in the centre with strips of chessboard on either side, and all six interest squares fit inside the middle 630×630. The pieces at the far edges only appear in the wide version that iMessage, Slack, LinkedIn and X show. The redrawn card is 41 KB. The line under my name now shows my job title, read from the same data as my résumé, instead of a list of languages. The full description already appears in the preview text.

I checked the square by cropping the centre of the image myself, not by testing in WhatsApp. WhatsApp also caches previews it has already fetched, so to see a new card you have to share a URL it hasn't seen, such as one with ?v=4 on the end.

The squash-merge gotcha

One more lesson, unrelated to images. The first PR had already been squash-merged by the time I pushed the redesign to its branch. The squash only included the commits that existed at merge time, so production got working tags and the first card, the one WhatsApp cut in half. The redesign commits were left behind on a merged branch. I moved them onto a fresh branch off main and opened a second PR. Both are now merged and deployed.

What I'd do differently

  • Check what production serves first. curl -I and file found the real bug in about a minute. Reading the metadata code would never have shown it.
  • Design for the square crop from the start. In a WhatsApp preview, the middle square of a 1200×630 image may be all anyone sees.
  • Check a PR's state before pushing follow-ups. If it's already merged, start a new branch instead of adding to the old one.

An agent did most of the digging and wrote the generator script. Deciding what the card should say, and noticing that the first version looked bad in a real chat, was down to me.