From f2e2bc069b53a131879714ba15a6b6437942de17 Mon Sep 17 00:00:00 2001 From: Yoshihiro Ashida Date: Tue, 17 Nov 2026 23:23:41 +0900 Subject: [PATCH] fix(metadata): render the Open Graph tags of each type like Next.js Only og:type, the article times and article:author were rendered, so an article lost its section and tags, a book's authors became article:author, and profile, music and video pages had no type-specific tags. og:determiner, og:country_name, og:ttl and an image's secure_url were dropped too. --- packages/vinext/src/shims/metadata.tsx | 159 +++++++++++++++++++++++-- tests/features.test.ts | 140 ++++++++++++++++++++++ 2 files changed, 286 insertions(+), 13 deletions(-) diff --git a/packages/vinext/src/shims/metadata.tsx b/packages/vinext/src/shims/metadata.tsx index 1323ec1c3e..bdea709508 100644 --- a/packages/vinext/src/shims/metadata.tsx +++ b/packages/vinext/src/shims/metadata.tsx @@ -248,10 +248,31 @@ export type Metadata = { faxNumbers?: string | string[]; locale?: string; alternateLocale?: string | string[]; + determiner?: string; + countryName?: string; + ttl?: number; type?: string; publishedTime?: string; modifiedTime?: string; - authors?: string[]; + expirationTime?: string; + authors?: OGList; + section?: string | null; + tags?: OGList; + isbn?: string | null; + releaseDate?: string | null; + firstName?: string | null; + lastName?: string | null; + username?: string | null; + gender?: string | null; + duration?: number | null; + albums?: OGList; + songs?: OGList; + musicians?: OGList; + creators?: OGList; + actors?: OGList; + directors?: OGList; + writers?: OGList; + series?: string | URL | null; } | null; twitter?: { card?: string; @@ -373,8 +394,15 @@ type OGAudioDescriptor = { type OGAudio = string | URL | OGAudioDescriptor; +type OGList = T | T[] | null; + +type OGMusicEntry = { url: string | URL; disc?: number; track?: number }; + +type OGActor = { url: string | URL; role?: string }; + type SocialImageDescriptor = { url: string | URL; + secureUrl?: string | URL; alt?: string; width?: number; height?: number; @@ -899,6 +927,98 @@ function resolveMetadataUrl( } } +type OpenGraphMetadata = NonNullable; + +function toList(value: OGList | undefined): T[] { + return value == null ? [] : Array.isArray(value) ? value : [value]; +} + +/** + * The tags Next.js renders for an Open Graph type, in its order + * (createMetadataElements in packages/next/src/lib/metadata/metadata.tsx). + * Next.js renders the article fields only for `type: "article"`; vinext has + * always rendered the times and authors without it, and still does. + */ +function openGraphTypeTags(og: OpenGraphMetadata): [property: string, content: string][] { + const tags: [string, string][] = []; + const tag = (property: string, value: string | number | URL | null | undefined) => { + if (value != null && value !== "") tags.push([property, String(value)]); + }; + const each = (property: string, values: OGList | undefined) => { + for (const value of toList(values)) tag(property, value); + }; + const entries = ( + property: string, + values: OGList | undefined, + ) => { + for (const value of toList(values)) { + if (typeof value === "string" || value instanceof URL) { + tag(property, value); + continue; + } + tag(property, value.url); + if ("disc" in value) tag(`${property}:disc`, value.disc); + if ("track" in value) tag(`${property}:track`, value.track); + if ("role" in value) tag(`${property}:role`, value.role); + } + }; + + switch (og.type) { + case "book": + tag("book:isbn", og.isbn); + tag("book:release_date", og.releaseDate); + each("book:author", og.authors); + each("book:tag", og.tags); + break; + case "profile": + tag("profile:first_name", og.firstName); + tag("profile:last_name", og.lastName); + tag("profile:username", og.username); + tag("profile:gender", og.gender); + break; + case "music.song": + tag("music:duration", og.duration); + entries("music:album", og.albums); + each("music:musician", og.musicians); + break; + case "music.album": + entries("music:song", og.songs); + each("music:musician", og.musicians); + tag("music:release_date", og.releaseDate); + break; + case "music.playlist": + entries("music:song", og.songs); + each("music:creator", og.creators); + break; + case "music.radio_station": + each("music:creator", og.creators); + break; + case "video.movie": + case "video.episode": + entries("video:actor", og.actors); + each("video:director", og.directors); + each("video:writer", og.writers); + tag("video:duration", og.duration); + tag("video:release_date", og.releaseDate); + each("video:tag", og.tags); + if (og.type === "video.episode") tag("video:series", og.series); + break; + case undefined: + default: { + const isArticle = og.type === "article"; + tag("article:published_time", og.publishedTime); + tag("article:modified_time", og.modifiedTime); + if (isArticle) tag("article:expiration_time", og.expirationTime); + each("article:author", og.authors); + if (isArticle) { + tag("article:section", og.section); + each("article:tag", og.tags); + } + } + } + return tags; +} + function resolveCanonicalUrl( url: string | URL, metadataBase: URL | null | undefined, @@ -1165,6 +1285,8 @@ export function MetadataHead({ // Open Graph if (metadata.openGraph) { const og = metadata.openGraph; + if (og.determiner) + elements.push(); const ogTitle = renderedTitle(og.title); if (ogTitle) elements.push(); if (og.description) @@ -1182,18 +1304,12 @@ export function MetadataHead({ elements.push(); if (og.type) elements.push(); if (og.locale) elements.push(); - if (og.publishedTime) - elements.push( - , - ); - if (og.modifiedTime) - elements.push( - , - ); - if (og.authors) { - for (const author of og.authors) { - elements.push(); - } + if (og.countryName) + elements.push(); + if (og.ttl != null) + elements.push(); + for (const [property, content] of openGraphTypeTags(og)) { + elements.push(); } if (og.images) { const imgList = @@ -1207,6 +1323,14 @@ export function MetadataHead({ , ); if (typeof img !== "string" && !(img instanceof URL)) { + if (img.secureUrl) + elements.push( + , + ); if (img.width) elements.push( , @@ -1308,6 +1432,15 @@ export function MetadataHead({ , ); if (typeof img !== "string" && !(img instanceof URL)) { + if (img.secureUrl) { + elements.push( + , + ); + } if (img.type) { elements.push(); } diff --git a/tests/features.test.ts b/tests/features.test.ts index a7eb124e6a..99dfdc4850 100644 --- a/tests/features.test.ts +++ b/tests/features.test.ts @@ -3171,6 +3171,146 @@ describe("MetadataHead rendering", () => { expect(html).toContain('href="https://example.com/rss"'); }); + // Expected tags are what Next.js 16.2.6 renders for the same metadata + // (createMetadataElements in packages/next/src/lib/metadata/metadata.tsx); + // the array fields per type follow resolve-opengraph.ts. + type OpenGraphInput = NonNullable[0]["openGraph"]>; + const openGraphTypeCases: [name: string, openGraph: OpenGraphInput, expected: string][] = [ + [ + "article", + { + type: "article", + publishedTime: "2026-01-01T00:00:00.000Z", + modifiedTime: "2026-01-02T00:00:00.000Z", + expirationTime: "2027-01-01T00:00:00.000Z", + authors: ["Seb", "https://a.example/josh"], + section: "sec", + tags: ["t1", "t2"], + }, + "og:type=article article:published_time=2026-01-01T00:00:00.000Z article:modified_time=2026-01-02T00:00:00.000Z article:expiration_time=2027-01-01T00:00:00.000Z article:author=Seb article:author=https://a.example/josh article:section=sec article:tag=t1 article:tag=t2", + ], + [ + "article with a single author and tag", + { type: "article", authors: new URL("https://au/url"), section: "S", tags: "solo-tag" }, + "og:type=article article:author=https://au/url article:section=S article:tag=solo-tag", + ], + [ + "book", + { + type: "book", + isbn: "978", + releaseDate: "2026-05-06", + authors: "https://au/solo", + tags: "bt", + }, + "og:type=book book:isbn=978 book:release_date=2026-05-06 book:author=https://au/solo book:tag=bt", + ], + [ + "profile", + { type: "profile", firstName: "F", lastName: "L", username: "u", gender: "g" }, + "og:type=profile profile:first_name=F profile:last_name=L profile:username=u profile:gender=g", + ], + [ + "music.song", + { + type: "music.song", + duration: 0, + albums: [ + "https://al/1", + { url: "https://al/2", disc: 1, track: 2 }, + { url: new URL("https://al/3") }, + ], + musicians: "https://mu/solo", + }, + "og:type=music.song music:duration=0 music:album=https://al/1 music:album=https://al/2 music:album:disc=1 music:album:track=2 music:album=https://al/3 music:musician=https://mu/solo", + ], + [ + "music.album", + { + type: "music.album", + songs: ["https://so/1", { url: "https://so/2", disc: 0, track: 3 }], + musicians: ["https://mu/1", new URL("https://mu/2")], + releaseDate: "2026-02-03", + }, + "og:type=music.album music:song=https://so/1 music:song=https://so/2 music:song:disc=0 music:song:track=3 music:musician=https://mu/1 music:musician=https://mu/2 music:release_date=2026-02-03", + ], + [ + "music.playlist", + { type: "music.playlist", songs: ["https://so/1"], creators: ["https://cr/1"] }, + "og:type=music.playlist music:song=https://so/1 music:creator=https://cr/1", + ], + [ + "music.radio_station", + { type: "music.radio_station", creators: "https://cr/solo" }, + "og:type=music.radio_station music:creator=https://cr/solo", + ], + [ + "video.movie", + { + type: "video.movie", + actors: { url: "https://ac/obj", role: "r" }, + directors: ["https://di/1", "https://di/2"], + writers: "https://wr/solo", + duration: 0, + releaseDate: "2026", + tags: ["t1", "t2"], + }, + "og:type=video.movie video:actor=https://ac/obj video:actor:role=r video:director=https://di/1 video:director=https://di/2 video:writer=https://wr/solo video:duration=0 video:release_date=2026 video:tag=t1 video:tag=t2", + ], + [ + "video.episode", + { + type: "video.episode", + actors: ["https://ac/1", { url: "https://ac/2", role: "lead" }], + directors: "https://di/1", + writers: ["https://wr/1"], + duration: 42, + releaseDate: "2026-03-04", + tags: "one-tag", + series: "https://se/1", + }, + "og:type=video.episode video:actor=https://ac/1 video:actor=https://ac/2 video:actor:role=lead video:director=https://di/1 video:writer=https://wr/1 video:duration=42 video:release_date=2026-03-04 video:tag=one-tag video:series=https://se/1", + ], + ["video.tv_show", { type: "video.tv_show" }, "og:type=video.tv_show"], + ["video.other", { type: "video.other" }, "og:type=video.other"], + ]; + it.each(openGraphTypeCases)( + "renders the Open Graph tags of type %s", + (_name, openGraph, expected) => { + const html = renderMetadataToHtml({ openGraph }, "/"); + const tags = [...html.matchAll(//g)].map( + ([, property, content]) => `${property}=${content}`, + ); + expect(tags.join(" ")).toBe(expected); + }, + ); + + it("renders og:determiner, og:country_name, og:ttl and an image's secure_url", () => { + const html = renderMetadataToHtml( + { + openGraph: { + determiner: "auto", + countryName: "JP", + ttl: 0, + images: [{ url: "https://example.com/a.png", secureUrl: "https://s.example/a.png" }], + }, + twitter: { + images: [{ url: "https://example.com/a.png", secureUrl: "https://s.example/a.png" }], + }, + }, + "/", + ); + expect(html).toContain(''); + expect(html).toContain(''); + expect(html).toContain(''); + expect(html).toContain( + '', + ); + expect(html).toContain( + '', + ); + }); + it("renders twitter:site and twitter:creator:id", () => { const html = renderToStaticMarkup( React.createElement(MetadataHead, {