Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions libs/blocks/merch/merch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
shouldAllowKrTrial, getCountry,
} from '../../utils/utils.js';
import { replaceKey } from '../../features/placeholders.js';
import { mepMasStudioUrls } from './mas-mep-utils.js';

// MAS Component Names
export const COMMERCE_LIBRARY = 'commerce';
Expand Down Expand Up @@ -1116,27 +1115,25 @@ export async function initService(force = false, attributes = {}) {
});
initService.promise = initService.promise
?? polyfills().then(async () => {
await loadMasComponent(COMMERCE_LIBRARY);
const useGeoMarket = isMasGeoDetectionEnabled();

// Load fragment-client.js when maslibs is present
// Load all independent resources in parallel
const fragmentClientUrl = getFragmentClientUrl();
if (fragmentClientUrl) {
const { loadScript: loadScriptUtil } = await import('../../utils/utils.js');
try {
await loadScriptUtil(fragmentClientUrl, 'module');
} catch (e) {
log?.error('Failed to load fragment-client.js:', e);
}
}
const [, , { language, locale, country }, validatedMarket] = await Promise.all([

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two look independent but aren't. Old order:

await getLocaleSettings()  → getCountry() → getAkamaiCode() → writes sessionStorage.akamai
await getValidatedMarket() → getCountry() → reads it ✅

The sequential await was doing load-bearing work — priming the cache before the second read. In parallel, both call getCountry() before either has written it. getAkamaiCode (geo.js:7) has no in-flight dedup and is cache: 'no-cache', so nothing collapses them.

I ran this locally against the branch — sequential gives 1 geo2.adobe.com fetch, parallel gives 2. So: two concurrent geo fetches per geo-enabled page load. Related risk I did not reproduce, but the code path is there — getValidatedMarket() resolving on a null country falls back to currLang.defaultMarket || 'us' (market.js:38), which would be a wrong-country price render on a slow or failing geo call.

Keeps most of the win — chain just the market call off the settings that prime the cache:

const localeSettingsPromise = getLocaleSettings(miloLocale);
const [, , { language, locale, country }, validatedMarket] = await Promise.all([
  loadMasComponent(COMMERCE_LIBRARY),
  fragmentClientUrl ? loadScript(fragmentClientUrl, 'module').catch(...) : Promise.resolve(),
  localeSettingsPromise,
  useGeoMarket
    ? localeSettingsPromise.then(() => import('../../utils/market.js')
        .then(({ getValidatedMarket }) => getValidatedMarket()))
    : Promise.resolve(null),
]);

loadMasComponent / loadScript / getLocaleSettings are genuinely independent, so that's where the real gain was anyway. Alternative: memoize the in-flight promise in getAkamaiCode — fixes the double-fetch for every caller, since getCountry and resolveDetectedMarketCountry both funnel through it.

loadMasComponent(COMMERCE_LIBRARY),
fragmentClientUrl
? loadScript(fragmentClientUrl, 'module').catch((e) => {
log?.error('Failed to load fragment-client.js:', e);
})
: Promise.resolve(),
getLocaleSettings(miloLocale),
useGeoMarket
? import('../../utils/market.js').then(({ getValidatedMarket }) => getValidatedMarket())
: Promise.resolve(null),
]);

const { language, locale, country } = await getLocaleSettings(miloLocale);
const useGeoMarket = isMasGeoDetectionEnabled();
let countryFromMarket = country;
if (useGeoMarket) {
const { getValidatedMarket } = await import('../../utils/market.js');
const validatedMarket = await getValidatedMarket();
if (validatedMarket) countryFromMarket = validatedMarket.toUpperCase();
}
if (useGeoMarket && validatedMarket) countryFromMarket = validatedMarket.toUpperCase();
let service = document.head.querySelector('mas-commerce-service');
if (!service) {
setPreview(attributes);
Expand Down Expand Up @@ -1623,6 +1620,7 @@ export default async function init(el) {
// Rebuilt merch href keeps only the OSI; stash the original for
// the "Edit OSI" badge.
if (getConfig()?.mep?.preview) {
const { mepMasStudioUrls } = await import('./mas-mep-utils.js');
mepMasStudioUrls.set(merch, el.href);
merch.dataset.masBlock = 'ost';
}
Expand Down
Loading