Skip to content

Commit

Permalink
Use URL class for adding lite extension more accurately
Browse files Browse the repository at this point in the history
  • Loading branch information
amoore108 committed Oct 22, 2024
1 parent edd1d4d commit 17a7cf9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -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 = `
<a href="https://www.bbc.com/news">News</a>
<a href="https://www.bbc.com/serbian/lat">News</a>
<a href="https://www.bbc.com/mundo">News</a>
<a href="https://www.bbcrussian.com/news">News</a>
<a href="/news">News</a>
<a href="https://www.bbc.com/news">News</a>
`;

const modifiedHtml = litePageTransforms(html);

expect(modifiedHtml).toEqual(`
<a href="https://www.bbc.com/news.lite">News</a>
<a href="https://www.bbc.com/serbian/lat.lite">News</a>
<a href="https://www.bbc.com/mundo.lite">News</a>
<a href="https://www.bbcrussian.com/news.lite">News</a>
<a href="/news.lite">News</a>
<a href="https://www.bbc.com/news.lite">News</a>
`);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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),
);
}
});
Expand Down

0 comments on commit 17a7cf9

Please sign in to comment.