Skip to content

Commit

Permalink
DEX-15414
Browse files Browse the repository at this point in the history
  • Loading branch information
vradulescu-bd committed Nov 24, 2023
1 parent 9aa0950 commit f0769f8
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions solutions/scripts/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ const hreflangMap = new Map([
['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: '' }],
['en-AU', { baseUrl: 'https://www.bitdefender.com.au', pageType: '', hasIndexPages: true }],
['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: '' }],
['zh-hk', { baseUrl: 'https://www.bitdefender.com/zh-hk', pageType: '', hasIndexPages: true }],
['zh-tw', { baseUrl: 'https://www.bitdefender.com/zh-tw', pageType: '', hasIndexPages: true }],
['x-default', { baseUrl: 'https://www.bitdefender.com', pageType: 'html' }],
]);

Expand Down Expand Up @@ -503,11 +503,20 @@ async function loadLazy(doc) {
sampleRUM.observe(main.querySelectorAll('div[data-block-name]'));
sampleRUM.observe(main.querySelectorAll('picture > img'));

hreflangMap.forEach(({ baseUrl, pageType }, key) => {
hreflangMap.forEach(({ baseUrl, pageType, hasIndexPages }, key) => {
const link = document.createElement('link');
link.setAttribute('rel', 'alternate');
link.setAttribute('hreflang', key);
link.setAttribute('href', `${baseUrl}${window.location.pathname.replace(/\/us\/en/, '')}${pageType ? `.${pageType}` : ''}`);

const suffix = `${pageType ? `.${pageType}` : ''}`;
const lastCharFromHref = window.location.pathname.slice(-1);
const isCurrentIndexPage = lastCharFromHref === '/';

let href = `${baseUrl}${window.location.pathname.replace(/\/us\/en/, '')}`;
href = isCurrentIndexPage && !hasIndexPages ? href.slice(0, -1) : href;
href = `${href}${suffix}`;

link.setAttribute('href', href);
document.head.appendChild(link);
});
}
Expand Down

0 comments on commit f0769f8

Please sign in to comment.