Skip to content

Commit

Permalink
Merge commit 'efa27f2bc90a0a346b9dadc1b4e7ec6e1e90eb19' into experime…
Browse files Browse the repository at this point in the history
…ntation-v2-integration
  • Loading branch information
FentPams committed Jun 21, 2024
2 parents b04889d + efa27f2 commit e148709
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
7 changes: 6 additions & 1 deletion plugins/experimentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@ const experimentationConfig = {
};
window.aem.plugins.add('experimentation', { // use window.hlx instead of your project has this
condition: () => document.head.querySelector('[name^="experiment"],[name^="campaign-"],[name^="audience-"]')
condition: () =>
// page level metadata
document.head.querySelector('[name^="experiment"],[name^="campaign-"],[name^="audience-"]')
// decorated section metadata
|| document.querySelector('.section[class*=experiment],.section[class*=audience],.section[class*=campaign]')
// undecorated section metadata
|| [...document.querySelectorAll('.section-metadata div')].some((d) => d.textContent.match(/Experiment|Campaign|Audience/i)),
options: experimentationConfig,
url: '/plugins/experimentation/src/index.js',
Expand Down
12 changes: 9 additions & 3 deletions plugins/experimentation/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function getAllSectionMeta(block, scope) {
* @param {HTMLElement} el
* @return Returns the path that was loaded or null if the loading failed
*/
async function replaceInner(path, el) {
async function replaceInner(path, el, selector) {
try {
const resp = await fetch(path);
if (!resp.ok) {
Expand All @@ -197,7 +197,13 @@ async function replaceInner(path, el) {
// parse with DOMParser to guarantee valid HTML, and no script execution(s)
const dom = new DOMParser().parseFromString(html, 'text/html');
// eslint-disable-next-line no-param-reassign
const newEl = dom.querySelector(el.tagName === 'MAIN' ? 'main' : 'main > div');
let newEl;
if (selector) {
newEl = dom.querySelector(selector);
}
if (!newEl) {
newEl = dom.querySelector(el.tagName === 'MAIN' ? 'main' : 'main > div');
}
el.innerHTML = newEl.innerHTML;
return path;
} catch (e) {
Expand Down Expand Up @@ -412,7 +418,7 @@ function watchMutationsAndApplyFragments(
let res;
if (url && new URL(url, window.location.origin).pathname !== window.location.pathname) {
// eslint-disable-next-line no-await-in-loop
res = await replaceInner(url, el);
res = await replaceInner(url, el, entry.selector);
} else {
res = url;
}
Expand Down

0 comments on commit e148709

Please sign in to comment.