Skip to content

Commit

Permalink
feat: Add hreflang
Browse files Browse the repository at this point in the history
  • Loading branch information
basecode committed Feb 19, 2024
1 parent a104908 commit 1f04b4a
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions solutions/scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,27 @@ export const DEFAULT_COUNTRY = 'au';

export const METADATA_ANAYTICS_TAGS = 'analytics-tags';

const HREFLANG_MAP = [
['en-ro', { baseUrl: 'https://www.bitdefender.ro', pageType: '.html' }],
['de', { baseUrl: 'https://www.bitdefender.de', pageType: '.html' }],
['sv', { baseUrl: 'https://www.bitdefender.se', pageType: '.html' }],
['pt', { baseUrl: 'https://www.bitdefender.pt', pageType: '.html' }],
['en-sv', { baseUrl: 'https://www.bitdefender.se', pageType: '.html' }],
['pt-BR', { baseUrl: 'https://www.bitdefender.com.br', pageType: '.html' }],
['en', { baseUrl: 'https://www.bitdefender.com', pageType: '.html' }],
['it', { baseUrl: 'https://www.bitdefender.it', pageType: '.html' }],
['fr', { baseUrl: 'https://www.bitdefender.fr', pageType: '.html' }],
['nl-BE', { baseUrl: 'https://www.bitdefender.br', pageType: '.html' }],
['es', { baseUrl: 'https://www.bitdefender.es', pageType: '.html' }],
['en-AU', { baseUrl: 'https://www.bitdefender.com.au', pageType: '' }],
['ro', { baseUrl: 'https://www.bitdefender.ro', pageType: '.html' }],
['nl', { baseUrl: 'https://www.bitdefender.nl', pageType: '.html' }],
['en-GB', { baseUrl: 'https://www.bitdefender.co.uk', pageType: '.html' }],
['zh-hk', { baseUrl: 'https://www.bitdefender.com/zh-hk', pageType: '' }],
['zh-tw', { baseUrl: 'https://www.bitdefender.com/zh-tw', pageType: '' }],
['x-default', { baseUrl: 'https://www.bitdefender.com', pageType: '.html' }],
];

window.hlx.plugins.add('rum-conversion', {
load: 'lazy',
url: '../plugins/rum-conversion/src/index.js',
Expand Down Expand Up @@ -284,6 +305,23 @@ export default function decorateLinkedPictures(main) {
});
}

function addHreflangTags() {
if (document.querySelectorAll('head link[hreflang]').length > 0) return;

const path = window.location.pathname;
const pathCount = path.split('/').filter(String).length;

Object.keys(HREFLANG_MAP).forEach((key) => {
const hreflang = HREFLANG_MAP[key][0];
const href = `${HREFLANG_MAP[key][1].baseUrl}${path}${pathCount > 1 ? HREFLANG_MAP[key][1].pageType: ''}`;

Check failure on line 316 in solutions/scripts/scripts.js

View workflow job for this annotation

GitHub Actions / linting

Operator ':' must be spaced
const ln = document.createElement('link');
ln.setAttribute('rel', 'alternate');
ln.setAttribute('hreflang', hreflang);
ln.setAttribute('href', href);
document.querySelector('head').appendChild(ln);
});
}

/**
* Decorates the main element.
* @param {Element} main The main element
Expand All @@ -297,6 +335,7 @@ export function decorateMain(main) {
decorateLinkedPictures(main);
decorateSections(main);
decorateBlocks(main);
addHreflangTags();
}

/**
Expand Down

0 comments on commit 1f04b4a

Please sign in to comment.