Skip to content

Commit

Permalink
Squashed 'plugins/experimentation/' changes from 09f8a72..638e0db
Browse files Browse the repository at this point in the history
638e0db fix: fragment content replacement
c61fdaf doc: update instrumentation details for plugin approach

git-subtree-dir: plugins/experimentation
git-subtree-split: 638e0db53a4fa7886ef9713acc853fc7b02c76be
  • Loading branch information
FentPams committed Jun 21, 2024
1 parent 2b6a7d2 commit efa27f2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
7 changes: 6 additions & 1 deletion 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 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 efa27f2

Please sign in to comment.