Skip to content

Fix broken OG image on social link previews#3

Merged
finallyjay merged 1 commit into
mainfrom
fix-og-image
Jun 3, 2026
Merged

Fix broken OG image on social link previews#3
finallyjay merged 1 commit into
mainfrom
fix-og-image

Conversation

@finallyjay
Copy link
Copy Markdown
Owner

Problem

On X/social link previews the card showed the title and description but a broken-image placeholder instead of the OG image.

Root cause: in SSR on Vercel, Astro.url.origin resolves to localhost inside the serverless function, so og:image, og:url and canonical were emitted as https://localhost/.... Crawlers couldn't fetch https://localhost/og-image.png, hence the placeholder. The image file itself was fine (1200×630 PNG, served with HTTP 200).

Fix

  • astro.config.mjs: add site as a canonical fallback URL.
  • src/layouts/Layout.astro: resolve the public origin from the x-forwarded-host / x-forwarded-proto headers Vercel injects, falling back to Astro.site, then Astro.url.origin.

Verification

Rendered the page via the dev server with and without the Vercel forwarded headers — og:image now resolves to https://daily-dev-roulette.vercel.app/og-image.png in both cases (no more localhost).

Note: X caches cards ~7 days. After deploy, share the link once with a cache-buster query (e.g. ?v=2) to force a re-crawl.

🤖 Generated with Claude Code

Behind Vercel's proxy, Astro.url.origin resolves to "localhost" in the
serverless function, so og:image, og:url and canonical pointed at
https://localhost/... and X/social crawlers couldn't load the preview image.

Resolve the public origin from the x-forwarded-host/proto headers, falling
back to a configured `site`, then Astro.url.origin.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings June 3, 2026 13:35
@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 3, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
daily-dev-roulette Ready Ready Preview, Comment Jun 3, 2026 1:35pm

@finallyjay finallyjay merged commit 3d241f8 into main Jun 3, 2026
3 checks passed
@finallyjay finallyjay deleted the fix-og-image branch June 3, 2026 13:36
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes broken social link preview OG images on Vercel SSR by ensuring absolute OG/canonical URLs use a publicly reachable origin instead of localhost.

Changes:

  • Configure a canonical site URL in Astro config to serve as a stable fallback origin.
  • Update Layout.astro to derive the public origin from Vercel forwarded headers, falling back to Astro.site / Astro.url.origin.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/layouts/Layout.astro Builds OG/canonical absolute URLs using forwarded host/proto (with fallbacks) to avoid localhost origins in SSR.
astro.config.mjs Sets site to provide a canonical fallback origin for absolute URL generation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/layouts/Layout.astro
Comment on lines +20 to +24
const forwardedHost = Astro.request.headers.get("x-forwarded-host");
const forwardedProto = Astro.request.headers.get("x-forwarded-proto") ?? "https";
const origin = forwardedHost
? `${forwardedProto}://${forwardedHost}`
: (Astro.site?.origin ?? Astro.url.origin);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants