From f0769f8cf02751c7e4b09abeb284d8e7a93052db Mon Sep 17 00:00:00 2001 From: ext-vradulescu Date: Fri, 24 Nov 2023 14:56:19 +0200 Subject: [PATCH] DEX-15414 --- solutions/scripts/scripts.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/solutions/scripts/scripts.js b/solutions/scripts/scripts.js index fbd28d591..e9d921227 100644 --- a/solutions/scripts/scripts.js +++ b/solutions/scripts/scripts.js @@ -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' }], ]); @@ -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); }); }