From 17a7cf97a922d6579b3c112a66ef21779e2aa6b8 Mon Sep 17 00:00:00 2001 From: Aaron Moore Date: Tue, 22 Oct 2024 18:40:35 +0100 Subject: [PATCH] Use URL class for adding lite extension more accurately --- .../litePageTransforms/index.test.ts | 9 +++-- .../litePageTransforms/transformAnchorTags.ts | 37 ++++++++++++------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/server/Document/Renderers/litePageTransforms/index.test.ts b/src/server/Document/Renderers/litePageTransforms/index.test.ts index b97cc83fc3e..669ad44c3e2 100644 --- a/src/server/Document/Renderers/litePageTransforms/index.test.ts +++ b/src/server/Document/Renderers/litePageTransforms/index.test.ts @@ -1,24 +1,25 @@ import litePageTransforms from '.'; describe('litePageTransforms', () => { + beforeAll(() => { + process.env.SIMORGH_BASE_URL = 'https://www.bbc.com'; + }); describe('anchor tags', () => { it('should append .lite suffix to valid hrefs', () => { const html = ` - News News News News - News + News `; const modifiedHtml = litePageTransforms(html); expect(modifiedHtml).toEqual(` - News News News News - News + News `); }); diff --git a/src/server/Document/Renderers/litePageTransforms/transformAnchorTags.ts b/src/server/Document/Renderers/litePageTransforms/transformAnchorTags.ts index c90f21f1a07..72ae7936a3d 100644 --- a/src/server/Document/Renderers/litePageTransforms/transformAnchorTags.ts +++ b/src/server/Document/Renderers/litePageTransforms/transformAnchorTags.ts @@ -15,26 +15,36 @@ const VALID_DOMAINS = [ const RESERVED_ROUTE_EXTENSIONS = ['amp', 'app', 'lite']; -const isValidHref = (href: string) => { - const url = new URL(href, 'http://localhost'); +const addLiteExtension = (href?: string) => { + if (!href) return null; - const extension = url?.pathname?.split('.')?.pop() || ''; - const startsWithHash = href?.startsWith('#'); + const url = new URL(href, getEnvConfig().SIMORGH_BASE_URL); - const hasReservedRouteExtension = - RESERVED_ROUTE_EXTENSIONS.includes(extension); + const extension = url.pathname?.split('.')?.pop() || ''; const isValidDomain = VALID_DOMAINS.includes(url.hostname); const isWsService = SERVICES.includes( - url?.pathname?.split('/')?.[1] as Services, + url.pathname?.split('/')?.[1] as Services, ); - return ( + const startsWithHash = url.href?.startsWith('#'); + + const hasReservedRouteExtension = + RESERVED_ROUTE_EXTENSIONS.includes(extension); + + const shouldAddLiteExtension = isValidDomain && isWsService && !hasReservedRouteExtension && - !startsWithHash - ); + !startsWithHash; + + if (shouldAddLiteExtension) { + url.pathname += '.lite'; + + return url; + } + + return null; }; export default (html: string) => { @@ -45,13 +55,12 @@ export default (html: string) => { anchorTags.forEach(tag => { const href = tag?.match(/href="([^"]*)"/)?.[1]; - if (href && isValidHref(href)) { - const newUrl = new URL(href, getEnvConfig().SIMORGH_BASE_URL); - newUrl.pathname += '.lite'; + const urlWithLite = addLiteExtension(href); + if (href && urlWithLite) { modifiedHtml = modifiedHtml.replace( tag, - tag.replace(href, newUrl.href), + tag.replace(href, urlWithLite.href), ); } });