Skip to content

Commit

Permalink
MWPW-146001 parallelize literals call (#2187)
Browse files Browse the repository at this point in the history
* MWPW-146001 parallelize literals call

* MWPW-146001 move promise to the function

* MWPW-146001 fix commerce library

* MWPW-146001 update literals endpoint

* MWPW-146001 fixing commerce ut
  • Loading branch information
npeltier authored Apr 26, 2024
1 parent f847222 commit dac9c7e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
14 changes: 11 additions & 3 deletions libs/blocks/merch/merch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
} from '../../utils/utils.js';
import { replaceKey } from '../../features/placeholders.js';

export const PRICE_LITERALS_URL = 'https://milo.adobe.com/libs/commerce/price-literals.json';
export const PRICE_LITERALS_URL = 'https://www.adobe.com/federal/commerce/price-literals.json';
export const CHECKOUT_LINK_CONFIG_PATH = '/commerce/checkout-link.json'; // relative to libs.

export const PRICE_TEMPLATE_DISCOUNT = 'discount';
Expand Down Expand Up @@ -142,6 +142,14 @@ export async function fetchEntitlements() {
return fetchEntitlements.promise;
}

export async function fetchLiterals(url) {
fetchLiterals.promise = fetchLiterals.promise ?? new Promise((resolve) => {
fetch(url)
.then((response) => response.json().then(({ data }) => resolve(data)));
});
return fetchLiterals.promise;
}

export async function fetchCheckoutLinkConfigs(base = '') {
fetchCheckoutLinkConfigs.promise = fetchCheckoutLinkConfigs.promise
?? fetch(`${base}${CHECKOUT_LINK_CONFIG_PATH}`).catch(() => {
Expand Down Expand Up @@ -318,10 +326,10 @@ export async function initService(force = false) {
fetchEntitlements.promise = undefined;
fetchCheckoutLinkConfigs.promise = undefined;
}
const { env, commerce = {}, locale } = getConfig();
commerce.priceLiteralsPromise = fetchLiterals(PRICE_LITERALS_URL);
initService.promise = initService.promise ?? polyfills().then(async () => {
const commerceLib = await import('../../deps/commerce.js');
const { env, commerce = {}, locale } = getConfig();
commerce.priceLiteralsURL = PRICE_LITERALS_URL;
const service = await commerceLib.init(() => ({
env,
commerce,
Expand Down
6 changes: 3 additions & 3 deletions libs/deps/commerce.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions test/blocks/merch/merch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import merch, {
buildCta,
getCheckoutContext,
initService,
PRICE_LITERALS_URL,
fetchLiterals,
fetchCheckoutLinkConfigs,
getCheckoutLinkConfig,
getDownloadAction,
fetchEntitlements,
getModalAction,
getCheckoutAction,
PRICE_LITERALS_URL,
} from '../../../libs/blocks/merch/merch.js';

import { mockFetch, unmockFetch, readMockText } from './mocks/fetch.js';
Expand Down Expand Up @@ -64,7 +65,6 @@ const CHECKOUT_LINK_CONFIGS = {

const config = {
codeRoot: '/libs',
commerce: { priceLiteralsURL: PRICE_LITERALS_URL },
env: { name: 'prod' },
imsClientId: 'test_client_id',
placeholders: { 'upgrade-now': 'Upgrade Now', download: 'Download' },
Expand Down Expand Up @@ -129,6 +129,7 @@ describe('Merch Block', () => {
document.head.innerHTML = await readMockText('head.html');
document.body.innerHTML = await readMockText('body.html');
({ setCheckoutLinkConfigs, setSubscriptionsData } = await mockFetch());
config.commerce = { priceLiteralsPromise: fetchLiterals(PRICE_LITERALS_URL) };
setCheckoutLinkConfigs(CHECKOUT_LINK_CONFIGS);
});

Expand Down

0 comments on commit dac9c7e

Please sign in to comment.