fix(metadata): encode metadata social image URLs - #3070
Open
NriotHrreion wants to merge 1 commit into
Open
Conversation
commit: |
Contributor
Performance benchmarksCompared 0 improved · 0 regressed · 6 within ±1.5%
View detailed results and traces 🟢 improvement · 🔴 regression · ⚫ change below 1.5% · paired base/head |
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.
Closes #3066
Overview
Next.js percent-encodes og and twitter image URLs during metadata resolution, while vinext could emit absolute string URLs unchanged when they contained spaces or other characters requiring encoding. This produced invalid metadata URLs and could cause social crawlers or link-preview generators to fail when fetching CMS-provided images.
This PR restores Next.js-compatible URL serialization for social image metadata.
What changed
In
packages/vinext/src/shims/metadata.tsx,resolveSocialImageUrl()usesresolveMetadataUrl()to resolve social image URLs.vinext/packages/vinext/src/shims/metadata.tsx
Lines 835 to 842 in 20fdac4
However,
resolveMetadataUrl()will return the original URL if there is nometadataBaseprovided.vinext/packages/vinext/src/shims/metadata.tsx
Lines 751 to 759 in 20fdac4
So just pass the social image URL into
new URL(), and the URL will be encoded automatically. Ifnew URL()fails, this means it may be a relative URL, then just pass it to theresolveMetadataUrl().Testing
pnpm test tests/features.test.ts