Skip to content

Commit

Permalink
MWPW-152280 MEP: Only preload fragments that are in the 1st section (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
chrischrischris authored Jul 15, 2024
1 parent 8ef7d68 commit c44d8ad
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
9 changes: 8 additions & 1 deletion libs/features/personalization/personalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ export const preloadManifests = ({ targetManifests = [], persManifests = [] }) =

export const getFileName = (path) => path?.split('/').pop();

const isInLcpSection = (el) => {
const lcpSection = document.querySelector('body > main > div');
return lcpSection === el || lcpSection?.contains(el);
};

const createFrag = (el, url, manifestId) => {
let href = url;
try {
Expand All @@ -126,7 +131,9 @@ const createFrag = (el, url, manifestId) => {
if (isSection) {
frag = createTag('div', undefined, frag);
}
loadLink(`${localizeLink(a.href)}.plain.html`, { as: 'fetch', crossorigin: 'anonymous', rel: 'preload' });
if (isInLcpSection(el)) {
loadLink(`${localizeLink(a.href)}.plain.html`, { as: 'fetch', crossorigin: 'anonymous', rel: 'preload' });
}
return frag;
};

Expand Down
22 changes: 22 additions & 0 deletions test/features/personalization/actions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,26 @@ describe('custom actions', async () => {
},
});
});

it('Only fragments in the first section should be preloaded', async () => {
document.body.innerHTML = await readFile({ path: './mocks/personalization.html' });

let manifestJson = await readFile({ path: './mocks/actions/manifestPreloadFrags.json' });
manifestJson = JSON.parse(manifestJson);
setFetchResponse(manifestJson);

// This fragment is in the 1st section and should be preloaded
const lcpLink = 'link[href^="/test/features/personalization/mocks/fragments/fragmentReplaced"]';

// This fragment is in the 3rd section and should not be preloaded
const notLcpLink = 'link[href^="/test/features/personalization/mocks/fragments/inlineFragReplaced"]';

expect(document.querySelector(lcpLink)).not.to.exist;
expect(document.querySelector(notLcpLink)).not.to.exist;

await applyPers([{ manifestPath: '/path/to/manifest.json' }]);

expect(document.querySelector(lcpLink)).to.exist;
expect(document.querySelector(notLcpLink)).not.to.exist;
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"total": 5,
"offset": 0,
"limit": 5,
"data": [
{
"action": "replace",
"selector": ".custom-block",
"page filter (optional)": "",
"param-newoffer=123": "",
"chrome": "/test/features/personalization/mocks/fragments/fragmentReplaced",
"firefox": "",
"android": "",
"ios": ""
},
{
"action": "replace",
"selector": ".how-to",
"page filter (optional)": "",
"param-newoffer=123": "",
"chrome": "/test/features/personalization/mocks/fragments/inlineFragReplaced",
"firefox": "",
"android": "",
"ios": ""
}
],
":type": "sheet"
}

0 comments on commit c44d8ad

Please sign in to comment.