From de4829e89ee0c8e807201136af45eb478f9ff28c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ilyas=20St=C3=A9phane=20T=C3=BCrkben?= Date: Wed, 24 May 2023 15:17:25 +0200 Subject: [PATCH 1/3] MWPW-131664: icrease merch resilience to errors --- libs/blocks/merch/merch.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/libs/blocks/merch/merch.js b/libs/blocks/merch/merch.js index 14d5417533..ae7f47da6d 100644 --- a/libs/blocks/merch/merch.js +++ b/libs/blocks/merch/merch.js @@ -91,10 +91,13 @@ window.tacocat.loadPromise = new Promise((resolve) => { loadScript(literalScriptUrl) .catch(() => ({})) /* ignore if literals fail */ - .then(() => loadScript(scriptUrl)) - .then(() => { - runTacocat(tacocatEnv, country, language); - resolve(); + .then(() => loadScript(scriptUrl).then(() => true)) + .catch(() => false) + .then((success) => { + if (success) { + runTacocat(tacocatEnv, country, language); + } + resolve(success); }); }); From be1f539c13d6dadaa5aebd8fdbf1844ad5fb53c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ilyas=20St=C3=A9phane=20T=C3=BCrkben?= Date: Wed, 24 May 2023 15:57:49 +0200 Subject: [PATCH 2/3] fix(#748): icrease merch resilience to errors --- libs/blocks/merch/merch.js | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/libs/blocks/merch/merch.js b/libs/blocks/merch/merch.js index ae7f47da6d..1b6036c89f 100644 --- a/libs/blocks/merch/merch.js +++ b/libs/blocks/merch/merch.js @@ -91,13 +91,14 @@ window.tacocat.loadPromise = new Promise((resolve) => { loadScript(literalScriptUrl) .catch(() => ({})) /* ignore if literals fail */ - .then(() => loadScript(scriptUrl).then(() => true)) - .catch(() => false) - .then((success) => { - if (success) { - runTacocat(tacocatEnv, country, language); - } - resolve(success); + .then(() => loadScript(scriptUrl)) + .then(() => { + runTacocat(tacocatEnv, country, language); + resolve(false); + }) + .catch((error) => { + console.error('Failed to load tacocat', error); + resolve(true); }); }); @@ -164,10 +165,8 @@ function getCheckoutContext(searchParams, config) { export default async function init(el) { if (!el?.classList?.contains('merch')) return undefined; - try { - await window.tacocat.loadPromise; - } catch (e) { - console.error('Tacocat not loaded', e); + const fail = await window.tacocat.loadPromise; + if (fail) { return undefined; } const { searchParams } = new URL(el.href); From 0108f32a95d27089924d76f0eededf4fc00ae48c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ilyas=20St=C3=A9phane=20T=C3=BCrkben?= Date: Wed, 24 May 2023 16:01:07 +0200 Subject: [PATCH 3/3] fix test --- test/blocks/merch/merch.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/blocks/merch/merch.test.js b/test/blocks/merch/merch.test.js index ef4bd40fac..f5d90affc1 100644 --- a/test/blocks/merch/merch.test.js +++ b/test/blocks/merch/merch.test.js @@ -12,7 +12,7 @@ document.body.innerHTML = await readFile({ path: './mocks/body.html' }); describe('Merch Block', () => { before(async () => { Object.assign(window.tacocat, { - loadPromise: Promise.resolve(), + loadPromise: Promise.resolve(false), price: { optionProviders: [] }, defaults: { apiKey: 'wcms-commerce-ims-ro-user-milo', @@ -346,7 +346,7 @@ describe('Merch Block', () => { }); it('does not initialize the block when tacocat fails to load', async () => { - window.tacocat.loadPromise = Promise.reject(new Error('404')); + window.tacocat.loadPromise = Promise.resolve(true); let el = document.querySelector('.merch.cta.notacocat'); el = await merch(el); expect(el).to.be.undefined;