Free rules / Open Graph & X cards
Open Graph & X Card Rules for AI Coding (free)
Ask an AI tool to “add a share image” and it often writes a relative og:image path, skips twitter:card, reuses the logo, or drops the image on Next.js pages that override openGraph. The result: a blank card when you post the link. This free rules file tells your agent which tags are required, what the image must look like, and how to check the live HTML before calling it done.
What AI tools get wrong, and the fix
| The agent does this | What the docs say |
|---|---|
| Writes og:image as /og.png | Open Graph URL values are full http:// or https:// URLs. Write https://yourdomain.com/og.png (in Next.js, set metadataBase and relative paths in metadata become absolute) |
| Sets og:title and og:image only | ogp.me lists four required properties: og:title, og:type, og:image and og:url. With an og:image it should also set og:image:alt |
| Skips twitter:card | X's large card needs twitter:card = summary_large_image and twitter:title (both marked required in X's card docs) |
| Uses the site logo as the share image | X's docs say not to use a generic image such as your logo; make one image that says what the page is |
| Sets openGraph: { title } on a Next.js page and loses the image | Next.js merges metadata shallowly, so the page's openGraph replaces the layout's. Repeat images on the page or read them from parent |
| Adds meta tags in React after load (Vite, Lovable, Bolt) | Preview crawlers read the HTML the server sends. Put the tags in index.html or use server-side metadata |
| Writes og:image:width 1200 without checking | Only state width and height that match the real file; never invent dimensions |
| Says the card is fixed | curl the page for og:/twitter: tags, curl -I the image for 200 + image/ type + under 5MB, then test with a fresh post |
How to use it
- Cursor: save the file below as
.cursor/rules/og-tags.mdc. It attaches when you edit layouts, pages,index.htmlor share-image files. - Claude Code, Codex and others: paste everything below the
---header intoCLAUDE.mdorAGENTS.md. - Then ask: “List every shareable page, show the og: and twitter: tags each one sends today, and propose fixes plus the curl checks before editing.”
The rules file
---
description: Link previews / share cards: Open Graph and X card meta tags (required tags, absolute image URLs, image size rules, Next.js metadata pitfalls, testing with curl)
globs: **/layout.tsx, **/layout.jsx, **/page.tsx, **/page.jsx, **/opengraph-image.*, **/twitter-image.*, index.html, **/index.html, **/_document.tsx, **/head.tsx
alwaysApply: false
---
# Open Graph and X card rules (link previews)
When you add or change page metadata, share images or <head> tags, follow these rules so links show a real preview card on X, Slack and other apps.
Checked on 2026-11-23 against the Open Graph protocol (https://ogp.me/), X's Summary Card with Large Image docs (archived copy, Feb 2026) and the Next.js metadata docs. If unsure whether a rule still holds, read those pages instead of guessing.
## Required tags on every shareable page
- Always set og:title, og:type, og:url and og:image. ogp.me calls these the four required properties for every page.
- If you set og:image, also set og:image:alt (what the image shows, not a caption).
- Add og:description (one or two sentences about THIS page).
- For a big image card on X, set twitter:card to summary_large_image and set twitter:title. X's docs mark both as required for that card. twitter:description, twitter:image and twitter:image:alt are optional but worth setting.
- Each page gets its own title, description and og:url. Do not copy the homepage values onto every page.
## URLs
- og:image, og:url and twitter:image must be full absolute URLs starting with https:// (ogp.me defines URL values as http:// or https:// URLs). Never write a relative path like /og.png in raw HTML.
- og:url is the canonical URL of the page, not a preview or staging URL.
- The image must load for a logged-out visitor: not behind a login, a preview-only deployment, or deployment protection.
## The image
- For summary_large_image, X's docs say: 2:1 aspect ratio, at least 300x157, at most 4096x4096 pixels, under 5MB, JPG, PNG, WEBP or GIF (first frame only), SVG not supported. 1200x600 PNG or JPG fits.
- X's docs say not to use a generic image such as the site logo. Make an image that says what the page is.
- If you write og:image:width and og:image:height, they must match the real file. Never invent dimensions; check the file or leave them out.
- Do not claim a size or format rule for a platform unless its docs state it. Say "their docs don't say" instead of guessing.
## Tags must be in the HTML the server sends
- Preview crawlers read the HTML response. Tags added by client-side JavaScript after load (for example React code that sets document.title or injects meta tags in a Vite, Lovable or Bolt app) may not be seen. Put the tags in index.html or use the framework's server-side metadata.
## Next.js App Router
- Set metadataBase once in the root layout (for example metadataBase: new URL('https://example.com')). The docs say a relative path in a URL-based metadata field without metadataBase causes a build error; an absolute URL in a field ignores metadataBase.
- Metadata from layouts and pages is merged shallowly. If a page sets openGraph: { title }, the layout's openGraph images are gone for that page. Repeat images in the page's openGraph (or read them from parent in generateMetadata). Same for twitter.
- Easiest image: an opengraph-image.(png|jpg) file in the route folder, plus opengraph-image.alt.txt for the alt text. Next.js writes the og:image, type, width and height tags. Same for twitter-image. Build fails if twitter-image is over 5MB or opengraph-image is over 8MB.
- In Next.js 16, params in generateMetadata and in opengraph-image code is a Promise: const { slug } = await params.
## Before saying it's done
1. curl -sL https://YOUR-URL | grep -iE 'og:|twitter:' and confirm every tag above is present with absolute URLs.
2. curl -sIL https://YOUR-IMAGE-URL and confirm HTTP 200, an image/ content type, and a size under 5MB.
3. Tell the user that platforms cache previews: test with a fresh post or a new URL, not one already shared.
4. List any tag you could not verify instead of claiming it works.
The tags to copy (plain HTML, Vite, Lovable, Bolt)
<!-- in <head>; every URL absolute -->
<meta property="og:title" content="My App: what it does in a few words">
<meta property="og:type" content="website">
<meta property="og:url" content="https://myapp.com/">
<meta property="og:image" content="https://myapp.com/og.png">
<meta property="og:image:alt" content="What the image shows">
<meta property="og:description" content="One or two sentences about this page.">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="My App: what it does in a few words">
<meta name="twitter:description" content="One or two sentences about this page.">
<meta name="twitter:image" content="https://myapp.com/og.png">Next.js App Router version
The width and height are left out on purpose: add them only if they match your real image. Or drop an opengraph-image.png (plus opengraph-image.alt.txt) in the route folder and Next.js writes the tags for you.
// app/layout.tsx (Next.js App Router)
export const metadata = {
metadataBase: new URL("https://myapp.com"),
title: "My App",
description: "One or two sentences about this page.",
openGraph: { type: "website", url: "/", images: [{ url: "/og.png", alt: "What the image shows" }] },
twitter: { card: "summary_large_image" },
};
// A page that overrides openGraph must repeat images, or the layout's are dropped (shallow merge).Common questions
Which Open Graph tags are required?
ogp.me lists four required properties for every page: og:title, og:type, og:image and og:url. It also says a page that specifies og:image should specify og:image:alt. og:description is optional but worth adding.
Does og:image need an absolute URL?
Yes. Open Graph URL values are URLs using http:// or https://, so a relative path like /og.png doesn't qualify. In Next.js, set metadataBase in the root layout; the docs say a relative path in a URL-based metadata field without metadataBase causes a build error.
What size should the image be for X?
X's Summary Card with Large Image docs (archived copy, Feb 2026; X's current developer site no longer hosts the card pages) say a 2:1 aspect ratio, at least 300x157 and at most 4096x4096 pixels, under 5MB, in JPG, PNG, WEBP or GIF (first frame only). SVG is not supported. 1200x600 fits.
Why did my Next.js page lose its share image?
Next.js merges metadata from layouts and pages shallowly. If a page sets its own openGraph object (for example only a title), it replaces the layout's openGraph, including images. Repeat the images in the page's openGraph, or read them from parent in generateMetadata.
I fixed the tags. Why does the old post still show no card?
Platforms cache link previews. Check the tags with curl, then test with a fresh post or a new URL rather than the one you already shared.
Sources: The Open Graph protocol, X's Summary Card with Large Image docs (Feb 2026 archive), Next.js generateMetadata and opengraph-image docs, read on Nov 23, 2026. On Next.js 16? See the free Next.js 16 rules. For more launch checks, see the free ship-safe rules.
Step-by-step link preview fix guide and a free og:image check on ShipCheck (sister project)