From 2d10c51471f49dc7cd5aaa352fb8dea5d56346e1 Mon Sep 17 00:00:00 2001 From: jckautzmann Date: Mon, 14 Aug 2023 19:47:26 +0200 Subject: [PATCH] MWPW-134968 - [Blog2Milo] Broken links on Homepage (#1101) * MWPW-134968 - [Blog2Milo] Broken links on Homepage - introduce a configuration called 'lang-root' in the page metadata - append the lang-root config to the category links * MWPW-134968 - [Blog2Milo] Broken links on Homepage - removing the bulk performance test as it relies on the Helix Admin Service performance * MWPW-134968 - [Blog2Milo] Broken links on Homepage - address PR feedback - move the utility function to its own helpers.js below /libs/utils - rename utility function - error handling for new URL() --- libs/blocks/article-feed/article-helpers.js | 3 +- .../featured-article/featured-article.js | 3 +- .../recommended-articles.js | 3 +- libs/utils/helpers.js | 21 +++++ test/blocks/bulk-publish/performance.test.js | 89 ------------------- 5 files changed, 27 insertions(+), 92 deletions(-) create mode 100644 libs/utils/helpers.js delete mode 100644 test/blocks/bulk-publish/performance.test.js diff --git a/libs/blocks/article-feed/article-helpers.js b/libs/blocks/article-feed/article-helpers.js index 0c1130d6ff..bf4fd50d87 100644 --- a/libs/blocks/article-feed/article-helpers.js +++ b/libs/blocks/article-feed/article-helpers.js @@ -1,5 +1,6 @@ import { getConfig } from '../../utils/utils.js'; import * as taxonomyLibrary from '../../scripts/taxonomy.js'; +import { updateLinkWithLangRoot } from '../../utils/helpers.js'; /* * @@ -262,7 +263,7 @@ export function getArticleTaxonomy(article) { export function getLinkForTopic(topic, path) { const titleSubs = { 'Transformation digitale': 'Transformation numérique' }; - const catLink = [getTaxonomyModule()?.get(topic)].map((tax) => tax?.link ?? '#'); + const catLink = updateLinkWithLangRoot([getTaxonomyModule()?.get(topic)].map((tax) => tax?.link ?? '#')); if (catLink === '#') { // eslint-disable-next-line no-console diff --git a/libs/blocks/featured-article/featured-article.js b/libs/blocks/featured-article/featured-article.js index c57e5263d6..0f6e526061 100644 --- a/libs/blocks/featured-article/featured-article.js +++ b/libs/blocks/featured-article/featured-article.js @@ -1,5 +1,6 @@ import { getMetadata, createTag, getConfig } from '../../utils/utils.js'; import fetchTaxonomy from '../../scripts/taxonomy.js'; +import { updateLinkWithLangRoot } from '../../utils/helpers.js'; export default async function init(el) { const a = el.querySelector('a'); @@ -26,7 +27,7 @@ export default async function init(el) { const pic = doc.body.querySelector('picture'); const featuredImg = createTag('div', { class: 'featured-article-card-image' }, pic); - const categoryLink = createTag('a', { href: categoryTaxonomy.link }, categoryTaxonomy.name); + const categoryLink = createTag('a', { href: updateLinkWithLangRoot(categoryTaxonomy.link) }, categoryTaxonomy.name); const categoryEl = createTag('div', { class: 'featured-article-card-category' }, categoryLink); const text = doc.body.querySelector('h1, h2, h3').textContent; const title = createTag('h3', null, text); diff --git a/libs/blocks/recommended-articles/recommended-articles.js b/libs/blocks/recommended-articles/recommended-articles.js index d0a8c4e008..9df83a804f 100644 --- a/libs/blocks/recommended-articles/recommended-articles.js +++ b/libs/blocks/recommended-articles/recommended-articles.js @@ -1,6 +1,7 @@ import { createTag, getMetadata, getConfig } from '../../utils/utils.js'; import fetchTaxonomy from '../../scripts/taxonomy.js'; import { replaceKey } from '../../features/placeholders.js'; +import { updateLinkWithLangRoot } from '../../utils/helpers.js'; async function getArticleDetails(article) { const path = new URL(article.href).pathname; @@ -41,7 +42,7 @@ function getDecoratedCards(articles, taxonomy) { const cardImage = createTag('div', { class: 'article-card-image' }, imageEl); const cardBody = createTag('div', { class: 'article-card-body' }); const categoryTaxonomy = taxonomy.get(category) || 'News'; - const categoryLink = createTag('a', { href: categoryTaxonomy.link }, categoryTaxonomy.name); + const categoryLink = createTag('a', { href: updateLinkWithLangRoot(categoryTaxonomy.link) }, categoryTaxonomy.name); const categoryEl = createTag('p', { class: 'article-card-category' }, categoryLink); const titleEl = createTag('h3', null, title); const descriptionEl = createTag('p', { class: 'article-card-description' }, description); diff --git a/libs/utils/helpers.js b/libs/utils/helpers.js new file mode 100644 index 0000000000..73274a4988 --- /dev/null +++ b/libs/utils/helpers.js @@ -0,0 +1,21 @@ +import { getMetadata } from './utils.js'; + +/** + * Prefixes the link with the language root defined in the metadata + * @param link + * @returns {string|*} + */ +// eslint-disable-next-line import/prefer-default-export +export function updateLinkWithLangRoot(link) { + const langRoot = getMetadata('lang-root'); + if (!langRoot) return link; + try { + const url = new URL(link); + url.pathname = `${langRoot}${url.pathname}`; + return url.href; + } catch (e) { + // eslint-disable-next-line no-console + console.error('Could not update link with lang root', e); + return link; + } +} diff --git a/test/blocks/bulk-publish/performance.test.js b/test/blocks/bulk-publish/performance.test.js deleted file mode 100644 index 7b209bb4f1..0000000000 --- a/test/blocks/bulk-publish/performance.test.js +++ /dev/null @@ -1,89 +0,0 @@ -import { expect } from '@esm-bundle/chai'; -import { stub } from 'sinon'; - -import { - ADMIN_BASE_URL, - BULK_CONFIG_FILE_PATH, - executeActions, storeOperation, storeUrls, -} from '../../../libs/blocks/bulk-publish/bulk-publish-utils.js'; - -const TEST_TIMEOUT_MS = 30 * 60 * 1000; -const ORIGIN = 'https://main--milo--adobecom.hlx.page'; -const BASE_PAGE_PATH = '/drafts/jck/bulk-publish/test/test'; -const BASE_PAGE_URL = `${ORIGIN}${BASE_PAGE_PATH}`; -const URLS_AMOUNT = 10; - -const ogFetch = window.fetch; -window.fetch = stub(); - -const mockBulkConfigJson = { - sites: { - data: [ - { origin: ORIGIN }, - ], - }, -}; - -const stubFetchForBulkConfig = () => { - window.fetch.withArgs(BULK_CONFIG_FILE_PATH).returns( - new Promise((resolve) => { - resolve({ - ok: true, - json: () => mockBulkConfigJson, - }); - }), - ); -}; - -const unstubFetchForAdminAPI = () => { - const controller = new AbortController(); - const options = { method: 'POST', signal: controller.signal }; - for (let i = 0; i < URLS_AMOUNT; i += 1) { - const { hostname, pathname } = new URL(`${BASE_PAGE_URL}-${i}`); - const [branch, repo, owner] = hostname.split('.')[0].split('--'); - const adminUrl = `${ADMIN_BASE_URL}/preview/${owner}/${repo}/${branch}${pathname}`; - window.fetch.withArgs(adminUrl, options).returns( - ogFetch(adminUrl, options), - ); - } -}; - -const restoreFetch = () => { - window.fetch = ogFetch; -}; - -describe('Bulk preview and publish', () => { - before(() => { - stubFetchForBulkConfig(); - unstubFetchForAdminAPI(); - }); - after(() => { - restoreFetch(); - }); - - /** - * This test measures the performance of the bulk preview/publish tool. - * Set 'URLS_AMOUNT' to 1000 and run the test to measure the time it took. - * Performance history: - * - on 21/apr/2023: Previewing 1000 pages took: 115 s - */ - it.skip('Performance test: previewing 1000 URLs', async () => { - localStorage.clear(); - const operation = 'preview'; - const urls = []; - for (let i = 0; i < URLS_AMOUNT; i += 1) { - urls[i] = `${BASE_PAGE_URL}-${i}`; - } - storeUrls(urls); - storeOperation(operation); - const start = Date.now(); - const results = await executeActions(false, () => {}); - const end = Date.now(); - const duration = Math.round((end - start) / 1000); - console.log(`Previewing ${URLS_AMOUNT} pages took: ${duration} s`); - results.forEach((result, i) => { - expect(result.status.preview).equals(200, 'status.preview is not 404'); - expect(result.url).equals(`${BASE_PAGE_URL}-${i}`, 'result.url is not PAGE_URL'); - }); - }).timeout(TEST_TIMEOUT_MS); -});