Skip to content
Closed
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
118 changes: 88 additions & 30 deletions packages/vinext/src/shims/metadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -274,24 +274,34 @@ export type Metadata = {
types?: Record<string, string | URL>;
};
verification?: {
google?: string;
yahoo?: string;
yandex?: string;
other?: Record<string, string | string[]>;
google?: MetadataValueList;
yahoo?: MetadataValueList;
yandex?: MetadataValueList;
me?: MetadataValueList;
other?: Record<string, MetadataValueList>;
};
metadataBase?: URL | null;
appleWebApp?: {
capable?: boolean;
title?: string;
statusBarStyle?: string;
startupImage?: string | Array<{ url: string; media?: string }>;
startupImage?: AppleStartupImage | AppleStartupImage[];
};
formatDetection?: {
email?: boolean;
address?: boolean;
telephone?: boolean;
date?: boolean;
address?: boolean;
email?: boolean;
url?: boolean;
};
abstract?: string | null;
archives?: string | string[] | null;
assets?: string | string[] | null;
bookmarks?: string | string[] | null;
category?: string;
classification?: string | null;
facebook?: { appId?: string; admins?: string | string[] } | null;
pinterest?: { richPin?: string | boolean } | null;
itunes?: {
appId: string;
appArgument?: string;
Expand All @@ -306,10 +316,22 @@ export type Metadata = {
windows_universal?: AppLinksWindows | AppLinksWindows[];
web?: AppLinksWeb | AppLinksWeb[];
};
other?: Record<string, string | string[]>;
other?: Record<string, MetadataValueList>;
[key: string]: unknown;
};

type MetadataValue = string | number | null | undefined;
type MetadataValueList = MetadataValue | MetadataValue[];

type AppleStartupImage = string | { url: string; media?: string };

/** One entry per value; Next.js renders no tag for an empty or missing one. */
function metadataValues(value: MetadataValueList): string[] {
return (Array.isArray(value) ? value : [value])
.filter((entry) => entry != null && entry !== "")
.map(String);
}

type AppLinksApple = {
url: string | URL;
app_store_id?: string | number;
Expand Down Expand Up @@ -437,10 +459,11 @@ function isPlainObject(value: unknown): value is Record<string, unknown> {

function isOtherMetadata(value: unknown): value is NonNullable<Metadata["other"]> {
if (!isPlainObject(value)) return false;
return Object.values(value).every((item) => {
if (typeof item === "string") return true;
return Array.isArray(item) && item.every((nestedItem) => typeof nestedItem === "string");
});
const isValue = (item: unknown) =>
item == null || typeof item === "string" || typeof item === "number";
return Object.values(value).every((item) =>
Array.isArray(item) ? item.every(isValue) : isValue(item),
);
}

/**
Expand Down Expand Up @@ -1114,20 +1137,34 @@ export function MetadataHead({

// Format detection
if (metadata.formatDetection) {
const parts: string[] = [];
if (metadata.formatDetection.telephone === false) parts.push("telephone=no");
if (metadata.formatDetection.address === false) parts.push("address=no");
if (metadata.formatDetection.email === false) parts.push("email=no");
const formatDetection = metadata.formatDetection;
const parts = (["telephone", "date", "address", "email", "url"] as const)
.filter((format) => formatDetection[format] === false)
.map((format) => `${format}=no`);
if (parts.length > 0) {
elements.push(<meta key={key++} name="format-detection" content={parts.join(", ")} />);
}
}

if (metadata.abstract) {
elements.push(<meta key={key++} name="abstract" content={metadata.abstract} />);
}

for (const rel of ["archives", "assets", "bookmarks"] as const) {
for (const href of metadataValues(metadata[rel])) {
elements.push(<link key={key++} rel={rel} href={href} />);
}
}

// Category
if (metadata.category) {
elements.push(<meta key={key++} name="category" content={metadata.category} />);
}

if (metadata.classification) {
elements.push(<meta key={key++} name="classification" content={metadata.classification} />);
}

// Robots
if (metadata.robots) {
if (typeof metadata.robots === "string") {
Expand Down Expand Up @@ -1486,16 +1523,16 @@ export function MetadataHead({
// Verification
if (metadata.verification) {
const v = metadata.verification;
if (v.google)
elements.push(<meta key={key++} name="google-site-verification" content={v.google} />);
if (v.yahoo) elements.push(<meta key={key++} name="y_key" content={v.yahoo} />);
if (v.yandex) elements.push(<meta key={key++} name="yandex-verification" content={v.yandex} />);
if (v.other) {
for (const [name, content] of Object.entries(v.other)) {
const values = Array.isArray(content) ? content : [content];
for (const val of values) {
elements.push(<meta key={key++} name={name} content={val} />);
}
const verificationTags: [name: string, values: MetadataValueList][] = [
["google-site-verification", v.google],
["y_key", v.yahoo],
["yandex-verification", v.yandex],
["me", v.me],
...Object.entries(v.other ?? {}),
];
for (const [name, values] of verificationTags) {
for (const content of metadataValues(values)) {
elements.push(<meta key={key++} name={name} content={content} />);
}
}
}
Expand All @@ -1519,8 +1556,9 @@ export function MetadataHead({
);
}
if (awa.startupImage) {
const imgs =
typeof awa.startupImage === "string" ? [{ url: awa.startupImage }] : awa.startupImage;
const imgs = (Array.isArray(awa.startupImage) ? awa.startupImage : [awa.startupImage]).map(
(image) => (typeof image === "string" ? { url: image } : image),
);
for (const img of imgs) {
elements.push(
<link
Expand All @@ -1544,6 +1582,27 @@ export function MetadataHead({
elements.push(<meta key={key++} name="apple-itunes-app" content={content} />);
}

// Facebook
if (metadata.facebook) {
if (metadata.facebook.appId) {
elements.push(<meta key={key++} property="fb:app_id" content={metadata.facebook.appId} />);
}
for (const admin of metadataValues(metadata.facebook.admins)) {
elements.push(<meta key={key++} property="fb:admins" content={admin} />);
}
}

// Pinterest
if (metadata.pinterest && metadata.pinterest.richPin !== undefined) {
elements.push(
<meta
key={key++}
property="pinterest-rich-pin"
content={String(metadata.pinterest.richPin)}
/>,
);
}

// App Links
if (metadata.appLinks) {
const al = metadata.appLinks;
Expand Down Expand Up @@ -1575,8 +1634,7 @@ export function MetadataHead({
// Other custom meta tags
if (metadata.other) {
for (const [name, content] of Object.entries(metadata.other)) {
const values = Array.isArray(content) ? content : [content];
for (const val of values) {
for (const val of metadataValues(content)) {
elements.push(<meta key={key++} name={name} content={val} />);
}
}
Expand Down
109 changes: 109 additions & 0 deletions tests/features.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2700,6 +2700,14 @@ describe("metadata segment merge", () => {
});
});

it("merges other custom meta tags that hold numbers", () => {
const result = mergeMetadata([
{ other: { inherited: "keep" } },
{ other: { count: 3, list: [1, "x"] } },
]);
expect(result.other).toEqual({ inherited: "keep", count: 3, list: [1, "x"] });
});

it("preserves root openGraph when page does not override it", () => {
const result = mergeMetadata([
{ openGraph: { title: "OG Layout", siteName: "Site" } },
Expand Down Expand Up @@ -3171,6 +3179,107 @@ describe("MetadataHead rendering", () => {
expect(html).toContain('href="https://example.com/rss"');
});

// The cases below follow createMetadataElements in Next.js 16.2.6
// (packages/next/src/lib/metadata/metadata.tsx) and its resolvers.
it("renders abstract, classification and the archives, assets and bookmarks links", () => {
const html = renderMetadataToHtml(
{
abstract: "abs",
classification: "cls",
archives: ["/arch"],
assets: "/assets",
bookmarks: ["/bm1", "/bm2"],
},
"/",
);
expect(html).toContain('<meta name="abstract" content="abs">');
expect(html).toContain('<meta name="classification" content="cls">');
expect(html).toContain(
'<link rel="archives" href="/arch"><link rel="assets" href="/assets"><link rel="bookmarks" href="/bm1"><link rel="bookmarks" href="/bm2">',
);
});

it("renders facebook and pinterest tags", () => {
expect(renderMetadataToHtml({ facebook: { appId: "123" } }, "/")).toContain(
'<meta property="fb:app_id" content="123">',
);
expect(renderMetadataToHtml({ facebook: { admins: ["1", "2"] } }, "/")).toContain(
'<meta property="fb:admins" content="1"><meta property="fb:admins" content="2">',
);
expect(renderMetadataToHtml({ facebook: { admins: "1" } }, "/")).toContain(
'<meta property="fb:admins" content="1">',
);
expect(renderMetadataToHtml({ pinterest: { richPin: true } }, "/")).toContain(
'<meta property="pinterest-rich-pin" content="true">',
);
expect(renderMetadataToHtml({ pinterest: { richPin: false } }, "/")).toContain(
'<meta property="pinterest-rich-pin" content="false">',
);
});

it("renders one verification tag per value, including me", () => {
const html = renderMetadataToHtml(
{
verification: {
google: ["g1", "g2"],
yahoo: "yh",
yandex: 123,
me: ["me@example.com", "https://me.example"],
other: { foo: ["a", ""] },
},
},
"/",
);
expect(html).toBe(
[
'<meta name="google-site-verification" content="g1">',
'<meta name="google-site-verification" content="g2">',
'<meta name="y_key" content="yh">',
'<meta name="yandex-verification" content="123">',
'<meta name="me" content="me@example.com">',
'<meta name="me" content="https://me.example">',
'<meta name="foo" content="a">',
].join(""),
);
});

it("lists every disabled format in format-detection", () => {
const html = renderMetadataToHtml(
{
formatDetection: {
telephone: false,
date: false,
address: false,
email: false,
url: false,
},
},
"/",
);
expect(html).toBe(
'<meta name="format-detection" content="telephone=no, date=no, address=no, email=no, url=no">',
);
});

it("skips empty values in other", () => {
const html = renderMetadataToHtml({ other: { a: "", b: [], c: ["x", ""], n: 3 } }, "/");
expect(html).toBe('<meta name="c" content="x"><meta name="n" content="3">');
});

it("renders an Apple startup image given as a string in a list", () => {
const html = renderMetadataToHtml(
{
appleWebApp: {
startupImage: ["/st.png", { url: "/st2.png", media: "(device-width: 768px)" }],
},
},
"/",
);
expect(html).toContain(
'<link rel="apple-touch-startup-image" href="/st.png"><link rel="apple-touch-startup-image" href="/st2.png" media="(device-width: 768px)">',
);
});

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