Skip to content

Commit

Permalink
fix(ssr): add hreflang to alternate rss link (#8756)
Browse files Browse the repository at this point in the history
* fix(ssr): add hreflang to rss link

* test(ssr): update alternate test cases
  • Loading branch information
caugner authored May 4, 2023
1 parent 47910ee commit f71415e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion ssr/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export default function render(
? "noindex, nofollow"
: "index, follow";
const robotsMeta = `<meta name="robots" content="${robotsContent}">`;
const rssLink = `<link rel="alternate" type="application/rss+xml" title="MDN Blog RSS Feed" href="/en-US/blog/rss.xml" />`;
const rssLink = `<link rel="alternate" type="application/rss+xml" title="MDN Blog RSS Feed" href="/${DEFAULT_LOCALE}/blog/rss.xml" hreflang="en" />`;
const ssr_data = [...translations, ...webfontTags, rssLink, robotsMeta];
let html = buildHtml;
html = html.replace(
Expand Down
20 changes: 11 additions & 9 deletions testing/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,14 @@ test("content built foo page", () => {
expect($('script[src="/static/js/ga.js"]')).toHaveLength(1);

// Because this en-US page has a French translation
expect($('link[rel="alternate"]')).toHaveLength(4);
expect($('link[rel="alternate"][hreflang="en"]')).toHaveLength(1);
expect($('link[rel="alternate"]')).toHaveLength(5);
expect($('link[rel="alternate"][hreflang="en"]')).toHaveLength(2);
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);
const toEnUSURL = $('link[rel="alternate"][hreflang="en"]').attr("href");
const toEnUSURL = $('link[rel="alternate"][hreflang="en"]')
.first()
.attr("href");
const toFrURL = $('link[rel="alternate"][hreflang="fr"]').attr("href");
// The domain is hardcoded because the URL needs to be absolute and when
// building static assets for Dev or Stage, you don't know what domain is
Expand Down Expand Up @@ -176,8 +178,8 @@ test("content built French foo page", () => {
const htmlFile = path.join(builtFolder, "index.html");
const html = fs.readFileSync(htmlFile, "utf-8");
const $ = cheerio.load(html);
expect($('link[rel="alternate"]')).toHaveLength(4);
expect($('link[rel="alternate"][hreflang="en"]')).toHaveLength(1);
expect($('link[rel="alternate"]')).toHaveLength(5);
expect($('link[rel="alternate"][hreflang="en"]')).toHaveLength(2);
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);
Expand Down Expand Up @@ -232,8 +234,8 @@ test("content built zh-CN page for hreflang tag testing", () => {
const $ = cheerio.load(html);
// The built page should not have duplicate hreflang tags,
// when zh-TW translation is also available.
expect($('link[rel="alternate"]')).toHaveLength(4);
expect($('link[rel="alternate"][hreflang="en"]')).toHaveLength(1);
expect($('link[rel="alternate"]')).toHaveLength(5);
expect($('link[rel="alternate"][hreflang="en"]')).toHaveLength(2);
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);
Expand Down Expand Up @@ -261,8 +263,8 @@ test("content built zh-TW page with en-US fallback image", () => {
const htmlFile = path.join(builtFolder, "index.html");
const html = fs.readFileSync(htmlFile, "utf-8");
const $ = cheerio.load(html);
expect($('link[rel="alternate"]')).toHaveLength(4);
expect($('link[rel="alternate"][hreflang="en"]')).toHaveLength(1);
expect($('link[rel="alternate"]')).toHaveLength(5);
expect($('link[rel="alternate"][hreflang="en"]')).toHaveLength(2);
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);
Expand Down

0 comments on commit f71415e

Please sign in to comment.