Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 146 additions & 13 deletions packages/vinext/src/shims/metadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | URL>;
section?: string | null;
tags?: OGList<string>;
isbn?: string | null;
releaseDate?: string | null;
firstName?: string | null;
lastName?: string | null;
username?: string | null;
gender?: string | null;
duration?: number | null;
albums?: OGList<string | URL | OGMusicEntry>;
songs?: OGList<string | URL | OGMusicEntry>;
musicians?: OGList<string | URL>;
creators?: OGList<string | URL>;
actors?: OGList<string | URL | OGActor>;
directors?: OGList<string | URL>;
writers?: OGList<string | URL>;
series?: string | URL | null;
} | null;
twitter?: {
card?: string;
Expand Down Expand Up @@ -373,8 +394,15 @@ type OGAudioDescriptor = {

type OGAudio = string | URL | OGAudioDescriptor;

type OGList<T> = 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;
Expand Down Expand Up @@ -899,6 +927,98 @@ function resolveMetadataUrl(
}
}

type OpenGraphMetadata = NonNullable<Metadata["openGraph"]>;

function toList<T>(value: OGList<T> | 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<string | URL> | undefined) => {
for (const value of toList(values)) tag(property, value);
};
const entries = (
property: string,
values: OGList<string | URL | OGMusicEntry | OGActor> | 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,
Expand Down Expand Up @@ -1165,6 +1285,8 @@ export function MetadataHead({
// Open Graph
if (metadata.openGraph) {
const og = metadata.openGraph;
if (og.determiner)
elements.push(<meta key={key++} property="og:determiner" content={og.determiner} />);
const ogTitle = renderedTitle(og.title);
if (ogTitle) elements.push(<meta key={key++} property="og:title" content={ogTitle} />);
if (og.description)
Expand All @@ -1182,18 +1304,12 @@ export function MetadataHead({
elements.push(<meta key={key++} property="og:site_name" content={og.siteName} />);
if (og.type) elements.push(<meta key={key++} property="og:type" content={og.type} />);
if (og.locale) elements.push(<meta key={key++} property="og:locale" content={og.locale} />);
if (og.publishedTime)
elements.push(
<meta key={key++} property="article:published_time" content={og.publishedTime} />,
);
if (og.modifiedTime)
elements.push(
<meta key={key++} property="article:modified_time" content={og.modifiedTime} />,
);
if (og.authors) {
for (const author of og.authors) {
elements.push(<meta key={key++} property="article:author" content={author} />);
}
if (og.countryName)
elements.push(<meta key={key++} property="og:country_name" content={og.countryName} />);
if (og.ttl != null)
elements.push(<meta key={key++} property="og:ttl" content={String(og.ttl)} />);
for (const [property, content] of openGraphTypeTags(og)) {
elements.push(<meta key={key++} property={property} content={content} />);
}
if (og.images) {
const imgList =
Expand All @@ -1207,6 +1323,14 @@ export function MetadataHead({
<meta key={key++} property="og:image" content={resolveSocialImageUrl(img, base)} />,
);
if (typeof img !== "string" && !(img instanceof URL)) {
if (img.secureUrl)
elements.push(
<meta
key={key++}
property="og:image:secure_url"
content={stringifyUrl(img.secureUrl)}
/>,
);
if (img.width)
elements.push(
<meta key={key++} property="og:image:width" content={String(img.width)} />,
Expand Down Expand Up @@ -1308,6 +1432,15 @@ export function MetadataHead({
<meta key={key++} name="twitter:image" content={resolveSocialImageUrl(img, base)} />,
);
if (typeof img !== "string" && !(img instanceof URL)) {
if (img.secureUrl) {
elements.push(
<meta
key={key++}
name="twitter:image:secure_url"
content={stringifyUrl(img.secureUrl)}
/>,
);
}
if (img.type) {
elements.push(<meta key={key++} name="twitter:image:type" content={img.type} />);
}
Expand Down
140 changes: 140 additions & 0 deletions tests/features.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Parameters<typeof renderMetadataToHtml>[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(/<meta property="([^"]*)" content="([^"]*)">/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('<meta property="og:determiner" content="auto">');
expect(html).toContain('<meta property="og:country_name" content="JP">');
expect(html).toContain('<meta property="og:ttl" content="0">');
expect(html).toContain(
'<meta property="og:image" content="https://example.com/a.png"><meta property="og:image:secure_url" content="https://s.example/a.png">',
);
expect(html).toContain(
'<meta name="twitter:image:secure_url" content="https://s.example/a.png">',
);
});

it("renders twitter:site and twitter:creator:id", () => {
const html = renderToStaticMarkup(
React.createElement(MetadataHead, {
Expand Down
Loading