Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance(opengraph): fix og:image and add og:{image:*,site_name,type} #9795

Merged
merged 5 commits into from
Nov 6, 2023
Merged
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
11 changes: 10 additions & 1 deletion client/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,21 @@
-->
<meta property="og:url" content="https://developer.mozilla.org" />
<meta property="og:title" content="MDN Web Docs" />
<meta property="og:locale" content="en-US" />
<meta property="og:type" content="website" />
<meta property="og:locale" content="en_US" />
<meta
property="og:description"
content="The MDN Web Docs site provides information about Open Web technologies including HTML, CSS, and APIs for both Web sites and progressive web apps."
/>
<meta property="og:image" content="%PUBLIC_URL%/mdn-social-share.png" />
<meta property="og:image:type" content="image/png" />
<meta property="og:image:height" content="1080" />
<meta property="og:image:width" content="1920" />
<meta
property="og:image:alt"
content="The MDN Web Docs logo, featuring a blue accent color, displayed on a solid black background."
/>
<meta property="og:site_name" content="MDN Web Docs" />
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:creator" content="MozDevNet" />

Expand Down
8 changes: 7 additions & 1 deletion ssr/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,16 @@ export default function render(
)
.join("");

// Open Graph protocol expects `language_TERRITORY` format.
const ogLocale = (locale || (doc && doc.locale) || DEFAULT_LOCALE).replace(
"-",
"_"
);

const og = new Map([
["title", escapedPageTitle],
["url", canonicalURL],
["locale", locale || (doc && doc.locale) || "en-US"],
["locale", ogLocale],
]);

if (pageDescription) {
Expand Down
6 changes: 3 additions & 3 deletions testing/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ test("content built zh-CN page for hreflang tag and copying image testing", () =
expect($('link[rel="alternate"][hreflang="fr"]')).toHaveLength(1);
expect($('link[rel="alternate"][hreflang="zh"]')).toHaveLength(1);
expect($('link[rel="alternate"][hreflang="zh-Hant"]')).toHaveLength(1);
expect($('meta[property="og:locale"]').attr("content")).toBe("zh-CN");
expect($('meta[property="og:locale"]').attr("content")).toBe("zh_CN");
expect($('meta[property="og:title"]').attr("content")).toBe(
"<foo>: 测试网页 | MDN"
);
Expand Down Expand Up @@ -273,7 +273,7 @@ test("content built zh-TW page with en-US fallback image", () => {
expect($('link[rel="alternate"][hreflang="fr"]')).toHaveLength(1);
expect($('link[rel="alternate"][hreflang="zh"]')).toHaveLength(1);
expect($('link[rel="alternate"][hreflang="zh-Hant"]')).toHaveLength(1);
expect($('meta[property="og:locale"]').attr("content")).toBe("zh-TW");
expect($('meta[property="og:locale"]').attr("content")).toBe("zh_TW");
expect($('meta[property="og:title"]').attr("content")).toBe(
"<foo>: 測試網頁 | MDN"
);
Expand Down Expand Up @@ -1165,7 +1165,7 @@ test("404 page", () => {
expect($("title").text()).toContain("Page not found");
expect($("h1").text()).toContain("Page not found");
expect($('meta[name="robots"]').attr("content")).toBe("noindex, nofollow");
expect($('meta[property="og:locale"]').attr("content")).toBe("en-US");
expect($('meta[property="og:locale"]').attr("content")).toBe("en_US");
});

test("plus page", () => {
Expand Down