Open Graph image not showing on a Next.js + Sanity site: rescue

By Nayan Kyada · · 7 min read

Part of The Sanity + Next.js Guide

If your Open Graph image is not showing on a Next.js + Sanity site, links shared on LinkedIn, Slack, WhatsApp or X look bare: a grey box, a favicon, or the wrong picture from an old campaign. It is almost never a Sanity bug. It is a metadata wiring problem that nobody noticed because nobody shares their own links to test them.

This post covers the symptoms, what leaving it broken costs, the fix path, and what to ask a developer who says it is a quick job.

Symptoms that point to a broken Open Graph setup

These are the patterns I see on inherited sites:

  • Every page shows the same image, usually the homepage one, or none at all.
  • The preview works on staging but not on the live domain.
  • The title and description appear, but the image slot is empty.
  • A fixed image shows in the debugger tool but old links still look wrong.
  • Blog posts that editors gave a hero image still share without one.

The quickest check takes thirty seconds. Open the page, view source, and search for og:image. If there is no tag, the problem is in your code. If there is a tag, copy the URL into a browser. If it is relative, returns a 404, or downloads a 12 MB file, you have found it.

What it costs to ignore

A link with no image gets noticeably less attention in a feed than one with a clean card. For a marketing site that depends on social posts, newsletters, and people pasting links into chats, that is a quiet tax on every campaign. It also makes the site look unmaintained to a prospect who pastes your URL into Slack before a call.

It is not a direct ranking factor, so I would not call it an emergency. But it is cheap to fix and visible to every non-technical stakeholder, which is why it tends to be the thing that finally triggers a call.

Cause 1: no metadataBase, so URLs are relative

Open Graph crawlers need absolute URLs. In the App Router, if you set images without a base URL, Next.js has to guess, and on some deployments it resolves to a preview or localhost-style host. Set metadataBase once in the root layout, from an environment variable that is the production domain.

This is also the setup that causes the related mess where a staging host leaks into tags. If that sounds familiar, read staging site indexed by Google, because the same environment variable bug often causes both.

Cause 2: generateMetadata never reads the Sanity image

On many inherited builds, metadata is hardcoded in the layout, or the page-level generateMetadata fetches the title and description but skips the image field. Editors fill in a social image in the Studio and nothing happens.

The fix is to project the image in the same GROQ query you already use for the page, then build a 1200 by 630 URL from it.

// app/blog/[slug]/page.tsx
import type { Metadata } from "next";
import { client } from "@/sanity/lib/client";
import { urlFor } from "@/sanity/lib/image";
 
const query = `*[_type == "post" && slug.current == $slug][0]{
  title,
  excerpt,
  "social": coalesce(socialImage, mainImage)
}`;
 
export async function generateMetadata({
  params,
}: {
  params: Promise<{ slug: string }>;
}): Promise<Metadata> {
  const { slug } = await params;
  const post = await client.fetch(query, { slug });
  if (!post) return {};
 
  const image = post.social?.asset
    ? urlFor(post.social).width(1200).height(630).fit("crop").format("jpg").url()
    : "/og-default.jpg";
 
  return {
    title: post.title,
    description: post.excerpt,
    openGraph: {
      title: post.title,
      description: post.excerpt,
      images: [{ url: image, width: 1200, height: 630 }],
    },
    twitter: { card: "summary_large_image", images: [image] },
  };
}

The coalesce gives editors a dedicated social image but falls back to the hero. The default image path keeps pages without any image from sharing blank. Because params is a Promise in current Next.js, it has to be awaited, and older code that reads it directly will fail.

Cause 3: the image is wrong for a crawler

Even with a valid URL, the card can fail. Common reasons:

  • The file is an original upload, several megabytes, and the crawler gives up. Always request a sized, compressed version through the image pipeline as above.
  • The format is something the platform does not render. JPEG or PNG is the safe choice for social cards, so ask for it explicitly rather than relying on automatic format negotiation, which can hand a crawler AVIF or WebP.
  • The crop cuts off the subject. If your Studio uses hotspot data, the Sanity URL builder respects it when you crop. If it does not, see Sanity image-url hotspot not working.

Cause 4: the page is behind something the crawler cannot pass

Password-protected staging, Vercel deployment protection, or a bot-blocking firewall rule will block the scrape. The preview then fails even though the tags are perfect. Test with the platform debugger against the exact production URL. If the debugger reports a fetch error rather than a missing tag, look at protection and firewall settings, not code.

The same family of issues, where a crawler sees something different than a browser does, is covered in Next.js pages not indexed by Google.

Cause 5: stale caches on the platform side

Social platforms cache the first scrape of a URL, sometimes for days. After deploying the fix, re-scrape the page using the platform's own debugger tools. If a platform will not refresh, share the link with a harmless query string once to force a new fetch. Do not conclude the fix failed before you have done this.

How to verify the fix

  1. View source on a live page and confirm og:image is an absolute production URL.
  2. Open that image URL directly and check it loads fast and is the right crop.
  3. Run three different page types through the debugger tools: homepage, a blog post, and a page with no image set.
  4. Paste a link into Slack and LinkedIn as a last visual check.
  5. Check that the image alt and descriptive fields exist on the Sanity side too. Missing alt text is a separate but common companion problem, covered in Sanity images missing alt text.

What to ask a developer

  • Is metadataBase set from an environment variable, and which value does production use?
  • Which Sanity field feeds the social image, and what is the fallback when it is empty?
  • Does the share image get a fixed 1200 by 630 crop in a web-safe format, or is the original file served?
  • Can you show me the debugger result for three page types, not just the homepage?

A good answer is specific and fast. A vague answer about plugins or waiting for caches suggests they have not looked at the tags.

When this is part of a bigger problem

Broken share cards are usually one symptom of metadata that was never treated as a real feature: missing canonicals, wrong sitemap entries, no structured data. If you suspect that, this is work I do. The performance and Sanity audit, from $800, covers metadata, rendering, and Core Web Vitals in one pass, with a prioritised fix list you can hand to any developer.

Frequently asked questions

Why is my Open Graph image not showing when I share a Next.js link?

The usual causes are a relative image URL with no metadataBase, an image that is too large or the wrong format, a page with no image field filled in, or a platform that cached an old scrape. Check the raw HTML for an og:image tag with an absolute URL first.

How do I refresh a cached Open Graph preview?

Fix the tag on the live site, then re-scrape the URL with the platform's debugger tool, such as the Facebook Sharing Debugger or LinkedIn Post Inspector. Changing the image URL also forces a fresh fetch.

What size should a Sanity image be for an Open Graph preview?

Aim for 1200 by 630 pixels, served as JPEG or PNG under roughly 5 MB, and ideally much smaller. You can request that exact crop from the Sanity image pipeline instead of asking editors to upload it.