Skip to content
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

Mark dom-action items that were executed on the page to not re-apply them #1213

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
12 changes: 6 additions & 6 deletions bundlesize.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
"brotiliSize": 133
},
"dist/alloy.js": {
"uncompressedSize": 589421,
"gzippedSize": 89564,
"brotiliSize": 69620
"uncompressedSize": 593980,
"gzippedSize": 90304,
"brotiliSize": 70171
},
"dist/alloy.min.js": {
"uncompressedSize": 123486,
"gzippedSize": 40839,
"brotiliSize": 35412
"uncompressedSize": 124393,
"gzippedSize": 41151,
"brotiliSize": 35684
}
}
13 changes: 12 additions & 1 deletion src/components/Personalization/createFetchDataHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ governing permissions and limitations under the License.
*/
import { groupBy, isNonEmptyArray } from "../../utils/index.js";
import PAGE_WIDE_SCOPE from "../../constants/pageWideScope.js";
import uuid from "../../utils/uuid.js";

const DECISIONS_HANDLE = "personalization:decisions";

Expand Down Expand Up @@ -56,7 +57,17 @@ export default ({
},
});
}
const propositions = handles.map((handle) => createProposition(handle));
const propositions = handles.map((handle) => {
const proposition = {
...handle,
items: handle.items.map((item) => {
item.id = !item.id || item.id === "0" ? uuid() : item.id;
return item;
}),
};
ninaceban marked this conversation as resolved.
Show resolved Hide resolved
return createProposition(proposition);
});

const {
page: pagePropositions = [],
view: viewPropositions = [],
Expand Down
33 changes: 23 additions & 10 deletions src/components/Personalization/dom-actions/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ governing permissions and limitations under the License.
import { awaitSelector } from "../../../utils/dom/index.js";
import { hideElements, showElements } from "../flicker/index.js";
import { selectNodesWithEq } from "./dom/index.js";

export { default as setText } from "./setText.js";
export { default as setHtml } from "./setHtml.js";
export { default as appendHtml } from "./appendHtml.js";
Expand All @@ -26,24 +25,38 @@ export { default as setAttributes } from "./setAttributes.js";
export { default as swapImage } from "./swapImage.js";
export { default as rearrangeChildren } from "./rearrangeChildren.js";

const renderContent = (elements, content, decorateProposition, renderFunc) => {
const executions = elements.map((element) =>
renderFunc(element, content, decorateProposition),
);
const renderContent = (
elements,
content,
decorateProposition,
renderFunc,
renderedHandler,
) => {
const executions = elements.map((element) => {
if(renderedHandler.shouldRender(element)) {
return renderFunc(element, content, decorateProposition, renderedHandler.markAsRendered);
}
return Promise.resolve();
});

return Promise.all(executions);
};

export const createAction = (renderFunc) => {
return (itemData, decorateProposition) => {
return (itemData, decorateProposition, renderedHandler) => {
const { selector, prehidingSelector, content } = itemData;

hideElements(prehidingSelector);

return awaitSelector(selector, selectNodesWithEq)
.then((elements) =>
renderContent(elements, content, decorateProposition, renderFunc),
)
.then((elements) => {
return renderContent(
elements,
content,
decorateProposition,
renderFunc,
renderedHandler,
);
})
.then(
() => {
// if everything is OK, show elements
Expand Down
4 changes: 2 additions & 2 deletions src/components/Personalization/dom-actions/appendHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
getRemoteScriptsUrls,
} from "./scripts.js";

export default (container, html, decorateProposition) => {
export default (container, html, decorateProposition, markAsRendered) => {
const fragment = createFragment(html);
addNonceToInlineStyleElements(fragment);
const elements = getChildNodes(fragment);
Expand All @@ -35,7 +35,7 @@ export default (container, html, decorateProposition) => {
});

decorateProposition(container);

markAsRendered(container);
executeInlineScripts(container, scripts);

return executeRemoteScripts(scriptsUrls);
Expand Down
7 changes: 7 additions & 0 deletions src/components/Personalization/dom-actions/dom/addCssClass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { isNonEmptyString } from "../../../../utils/index.js";

export default (element, name) => {
if (name && isNonEmptyString(name)) {
element.classList.add(name);
}
};
8 changes: 8 additions & 0 deletions src/components/Personalization/dom-actions/dom/hasCssClass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { isNonEmptyString } from "../../../../utils/index.js";

export default (element, name) => {
if (name && isNonEmptyString(name)) {
return element.classList.contains(name);
}
return false;
};
4 changes: 2 additions & 2 deletions src/components/Personalization/dom-actions/insertHtmlAfter.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
executeRemoteScripts,
} from "./scripts.js";

export default (container, html, decorateProposition) => {
export default (container, html, decorateProposition, markRendered) => {
const fragment = createFragment(html);
addNonceToInlineStyleElements(fragment);
const elements = getChildNodes(fragment);
Expand All @@ -36,7 +36,7 @@ export default (container, html, decorateProposition) => {
insertAfter(insertionPoint, element);
insertionPoint = element;
});

markRendered(container);
executeInlineScripts(container, scripts);

return executeRemoteScripts(scriptsUrls);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
getRemoteScriptsUrls,
} from "./scripts.js";

export default (container, html, decorateProposition) => {
export default (container, html, decorateProposition, markRendered) => {
const fragment = createFragment(html);
addNonceToInlineStyleElements(fragment);
const elements = getChildNodes(fragment);
Expand All @@ -33,7 +33,7 @@ export default (container, html, decorateProposition) => {
decorateProposition(element);
insertBefore(container, element);
});

markRendered(container);
executeInlineScripts(container, scripts);

return executeRemoteScripts(scriptsUrls);
Expand Down
4 changes: 2 additions & 2 deletions src/components/Personalization/dom-actions/prependHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
getRemoteScriptsUrls,
} from "./scripts.js";

export default (container, html, decorateProposition) => {
export default (container, html, decorateProposition, markRendered) => {
const fragment = createFragment(html);
addNonceToInlineStyleElements(fragment);
const elements = getChildNodes(fragment);
Expand All @@ -53,7 +53,7 @@ export default (container, html, decorateProposition) => {

i -= 1;
}

markRendered(container);
executeInlineScripts(container, scripts);

return executeRemoteScripts(scriptsUrls);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ governing permissions and limitations under the License.

import { getChildren, insertAfter, insertBefore } from "./dom/index.js";

export default (container, { from, to }, decorateProposition) => {
export default (container, { from, to }, decorateProposition, markRendered) => {
const children = getChildren(container);
const elementFrom = children[from];
const elementTo = children[to];
Expand All @@ -31,4 +31,5 @@ export default (container, { from, to }, decorateProposition) => {

decorateProposition(elementTo);
decorateProposition(elementFrom);
markRendered(container);
};
9 changes: 7 additions & 2 deletions src/components/Personalization/dom-actions/replaceHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ governing permissions and limitations under the License.
import { removeNode } from "../../../utils/dom/index.js";
import insertHtmlBefore from "./insertHtmlBefore.js";

export default (container, html, decorateProposition) => {
return insertHtmlBefore(container, html, decorateProposition).then(() => {
export default (container, html, decorateProposition, markRendered) => {
return insertHtmlBefore(
container,
html,
decorateProposition,
markRendered,
).then(() => {
removeNode(container);
});
};
3 changes: 2 additions & 1 deletion src/components/Personalization/dom-actions/setAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ governing permissions and limitations under the License.

import { setAttribute } from "./dom/index.js";

export default (container, attributes, decorateProposition) => {
export default (container, attributes, decorateProposition, markRendered) => {
Object.keys(attributes).forEach((key) => {
setAttribute(container, key, attributes[key]);
});

decorateProposition(container);
markRendered(container);
};
4 changes: 2 additions & 2 deletions src/components/Personalization/dom-actions/setHtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const clear = (container) => {
childNodes.forEach(removeNode);
};

export default (container, html, decorateProposition) => {
export default (container, html, decorateProposition, markRendered) => {
clear(container);
return appendHtml(container, html, decorateProposition);
return appendHtml(container, html, decorateProposition, markRendered);
};
3 changes: 2 additions & 1 deletion src/components/Personalization/dom-actions/setStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ governing permissions and limitations under the License.

import { setStyle } from "./dom/index.js";

export default (container, styles, decorateProposition) => {
export default (container, styles, decorateProposition, markRendered) => {
const { priority, ...style } = styles;

Object.keys(style).forEach((key) => {
setStyle(container, key, style[key], priority);
});

decorateProposition(container);
markRendered(container);
};
3 changes: 2 additions & 1 deletion src/components/Personalization/dom-actions/setText.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ OF ANY KIND, either express or implied. See the License for the specific languag
governing permissions and limitations under the License.
*/

export default (container, text, decorateProposition) => {
export default (container, text, decorateProposition, markRendered) => {
decorateProposition(container);
container.textContent = text;
markRendered(container);
};
3 changes: 2 additions & 1 deletion src/components/Personalization/dom-actions/swapImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { SRC } from "../../../constants/elementAttribute.js";
import { removeAttribute, setAttribute } from "./dom/index.js";
import { isImage, loadImage } from "./images.js";

export default (container, url, decorateProposition) => {
export default (container, url, decorateProposition, markRendered) => {
if (!isImage(container)) {
return;
}
Expand All @@ -29,4 +29,5 @@ export default (container, url, decorateProposition) => {

// Replace the image "src"
setAttribute(container, SRC, url);
markRendered(container);
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ governing permissions and limitations under the License.

import createDecorateProposition from "./createDecorateProposition.js";
import { DOM_ACTION_CLICK } from "../dom-actions/initDomActionsModules.js";
import createRenderedHandler from "./createRenderedHandler.js";

export default ({
modules,
Expand Down Expand Up @@ -64,9 +65,14 @@ export default ({
item.getProposition().getNotification(),
storeInteractionMeta,
);
const renderedHandler = createRenderedHandler(
item.getProposition().getScopeType(),
item.getIdentifier(),
);

return {
render: () => modules[type](item.getData(), decorateProposition),
render: () =>
modules[type](item.getData(), decorateProposition, renderedHandler),
setRenderAttempted: true,
includeInNotification: true,
};
Expand Down
29 changes: 29 additions & 0 deletions src/components/Personalization/handlers/createRenderedHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2024 Adobe. All rights reserved.
This file is licensed to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under
the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import hasCssClass from "../dom-actions/dom/hasCssClass.js";
import addCssClass from "../dom-actions/dom/addCssClass.js";

export default (scopeType, itemIdentifier) => {
return {
shouldRender: (element) => {
if (scopeType === "view") {
return !hasCssClass(element, itemIdentifier);
}
return true;
},
markAsRendered: (element) => {
if (scopeType === "view") {
addCssClass(element, itemIdentifier);
}
},
};
};
13 changes: 11 additions & 2 deletions src/components/Personalization/handlers/injectCreateProposition.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from "../constants/scopeType.js";

export default ({ preprocess, isPageWideSurface }) => {
const createItem = (item, proposition) => {
const createItem = (item, proposition, itemIdentifier) => {
const { id, schema, data, characteristics: { trackingLabel } = {} } = item;

const schemaType = data ? data.type : undefined;
Expand All @@ -29,6 +29,9 @@ export default ({ preprocess, isPageWideSurface }) => {
getId() {
return id;
},
getIdentifier() {
return itemIdentifier;
},
getSchema() {
return schema;
},
Expand Down Expand Up @@ -63,6 +66,10 @@ export default ({ preprocess, isPageWideSurface }) => {
) => {
const { id, scope, scopeDetails, items = [] } = payload;
const { characteristics: { scopeType } = {} } = scopeDetails || {};
const keyedItems = items.map((item) => ({
key: `alloy-${item.id}`,
item,
}));

return {
getScope() {
Expand All @@ -78,7 +85,9 @@ export default ({ preprocess, isPageWideSurface }) => {
return PROPOSITION_SCOPE_TYPE;
},
getItems() {
return items.map((item) => createItem(item, this));
return keyedItems.map((keyedItem) =>
createItem(keyedItem.item, this, keyedItem.key),
);
},
getNotification() {
return { id, scope, scopeDetails };
Expand Down
2 changes: 1 addition & 1 deletion src/components/StreamingMedia/createTrackMediaSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default ({
getPlayerDetails,
legacy,
},
edgeConfigOverrides
edgeConfigOverrides,
});

mediaSessionCacheManager.storeSession({
Expand Down
Loading
Loading