Fix broken OG image on social link previews#3
Merged
Merged
Conversation
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>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
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
siteURL in Astro config to serve as a stable fallback origin. - Update
Layout.astroto derive the public origin from Vercel forwarded headers, falling back toAstro.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 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.originresolves tolocalhostinside the serverless function, soog:image,og:urlandcanonicalwere emitted ashttps://localhost/.... Crawlers couldn't fetchhttps://localhost/og-image.png, hence the placeholder. The image file itself was fine (1200×630 PNG, served with HTTP 200).Fix
astro.config.mjs: addsiteas a canonical fallback URL.src/layouts/Layout.astro: resolve the public origin from thex-forwarded-host/x-forwarded-protoheaders Vercel injects, falling back toAstro.site, thenAstro.url.origin.Verification
Rendered the page via the dev server with and without the Vercel forwarded headers —
og:imagenow resolves tohttps://daily-dev-roulette.vercel.app/og-image.pngin both cases (no morelocalhost).🤖 Generated with Claude Code