-
Notifications
You must be signed in to change notification settings - Fork 169
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MWPW-159113 [Personalization] Parallelize personalization network requests to be as efficient as possible #2970
base: stage
Are you sure you want to change the base?
Changes from 13 commits
b28d725
40949a2
afd675d
6324d09
ba72b1c
9eacd32
ed95461
d33f201
49609b3
c9f170d
fd4a085
bbaf789
ae83c48
c388e34
3180394
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,8 @@ | |
/* eslint-disable no-console */ | ||
|
||
import { createTag, getConfig, loadLink, loadScript, localizeLink } from '../../utils/utils.js'; | ||
import { getEntitlementMap } from './entitlements.js'; | ||
|
||
const entitlementsPromise = import('./entitlements.js'); | ||
|
||
/* c8 ignore start */ | ||
const PHONE_SIZE = window.screen.width < 550 || window.screen.height < 550; | ||
|
@@ -691,6 +692,7 @@ async function getPersonalizationVariant(manifestPath, variantNames = [], varian | |
|
||
const variantInfo = buildVariantInfo(variantNames); | ||
|
||
const { getEntitlementMap } = await entitlementsPromise; | ||
const entitlementKeys = Object.values(await getEntitlementMap()); | ||
const hasEntitlementTag = entitlementKeys.some((tag) => variantInfo.allNames.includes(tag)); | ||
|
||
|
@@ -1003,32 +1005,40 @@ export async function applyPers(manifests, postLCP = false) { | |
config.mep.martech = `|${pznVariants.join('--')}|${pznManifests.join('--')}`; | ||
} | ||
|
||
export const combineMepSources = async (persEnabled, promoEnabled, mepParam) => { | ||
export const combineMepSources = async (persEnabled, promoEnabled, promoUtilsPromise, mepParam) => { | ||
let persManifests = []; | ||
let initialPersManifestsPromises = []; | ||
let initialPersManifests = []; | ||
|
||
if (persEnabled) { | ||
persManifests = persEnabled.toLowerCase() | ||
initialPersManifests = persEnabled.toLowerCase() | ||
.split(/,|(\s+)|(\\n)/g) | ||
.filter((path) => path?.trim()) | ||
.map((manifestPath) => ({ manifestPath })); | ||
initialPersManifestsPromises = initialPersManifests.map(({ manifestPath }) => { | ||
const normalizedURL = normalizePath(manifestPath); | ||
return fetch(normalizedURL, { mode: 'same-origin' }); | ||
}); | ||
} | ||
|
||
if (promoEnabled) { | ||
const { default: getPromoManifests } = await import('./promo-utils.js'); | ||
const { default: getPromoManifests } = await promoUtilsPromise; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could also move code from promo-utils.js directly here, i think it was not a good decision in the first place to have a separate file. |
||
persManifests = persManifests.concat(getPromoManifests(promoEnabled, PAGE_URL.searchParams)); | ||
} | ||
|
||
if (mepParam && mepParam !== 'off') { | ||
const persManifestPaths = persManifests.map((manifest) => { | ||
const { manifestPath } = manifest; | ||
if (manifestPath.startsWith('/')) return manifestPath; | ||
try { | ||
const url = new URL(manifestPath); | ||
return url.pathname; | ||
} catch (e) { | ||
return manifestPath; | ||
} | ||
}); | ||
const persManifestPaths = persManifests | ||
.concat(initialPersManifests) | ||
.map((manifest) => { | ||
const { manifestPath } = manifest; | ||
if (manifestPath.startsWith('/')) return manifestPath; | ||
try { | ||
const url = new URL(manifestPath); | ||
return url.pathname; | ||
} catch (e) { | ||
return manifestPath; | ||
} | ||
}); | ||
|
||
mepParam.split('---').forEach((manifestPair) => { | ||
const manifestPath = manifestPair.trim().toLowerCase().split('--')[0]; | ||
|
@@ -1037,15 +1047,22 @@ export const combineMepSources = async (persEnabled, promoEnabled, mepParam) => | |
} | ||
}); | ||
} | ||
return persManifests; | ||
return initialPersManifestsPromises.concat(persManifests | ||
.filter((m) => !('disabled' in m) || !m.disabled) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if i remember correct, we shouldn't filter out disabled manifests. if you are still able to see a disabled manifest in the page then we are good |
||
.map((manifest) => { | ||
const normalizedURL = normalizePath(manifest.manifestPath); | ||
return fetch(normalizedURL, { mode: 'same-origin' }); | ||
})); | ||
}; | ||
|
||
export async function init(enablements = {}) { | ||
let manifests = []; | ||
const { | ||
mepParam, mepHighlight, mepButton, pzn, promo, target, postLCP, | ||
} = enablements; | ||
const promoUtilsPromise = promo ? import('./promo-utils.js') : null; | ||
const config = getConfig(); | ||
let manifestPromises = []; | ||
if (!postLCP) { | ||
config.mep = { | ||
handleFragmentCommand, | ||
|
@@ -1058,14 +1075,29 @@ export async function init(enablements = {}) { | |
experiments: [], | ||
geoPrefix: config.locale?.prefix.split('/')[1]?.toLowerCase() || 'en-us', | ||
}; | ||
manifests = manifests.concat(await combineMepSources(pzn, promo, mepParam)); | ||
manifests?.forEach((manifest) => { | ||
if (manifest.disabled) return; | ||
const normalizedURL = normalizePath(manifest.manifestPath); | ||
loadLink(normalizedURL, { as: 'fetch', crossorigin: 'anonymous', rel: 'preload' }); | ||
}); | ||
|
||
manifestPromises = manifestPromises | ||
.concat(await combineMepSources(pzn, promo, promoUtilsPromise, mepParam)); | ||
} | ||
|
||
manifestPromises = manifestPromises.map((mPromise) => mPromise.then(async (resp) => { | ||
try { | ||
if (!resp.ok) { | ||
/* c8 ignore next 5 */ | ||
if (resp.status === 404) { | ||
throw new Error('File not found'); | ||
} | ||
throw new Error(`Invalid response: ${resp.status} ${resp.statusText}`); | ||
} | ||
const manifestData = await resp.json(); | ||
return { manifestData, manifestPath: resp.url }; | ||
} catch (e) { | ||
/* c8 ignore next 3 */ | ||
console.log(`Error loading content: ${resp.url}`, e.message || e); | ||
} | ||
return null; | ||
})); | ||
manifests = (await Promise.all(manifestPromises)).filter(Boolean); | ||
if (target === true || (target === 'gnav' && postLCP)) { | ||
const { getTargetPersonalization } = await import('../../martech/martech.js'); | ||
const { targetManifests, targetPropositions } = await getTargetPersonalization(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: inline
normalizePath