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

feat: Add decoration handler for handling fragment decoration #39

Merged
merged 7 commits into from
Aug 6, 2024
Merged
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
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ runEager.call(document, {
// See more details on the dedicated Experiments page linked below
experimentsMetaTagPrefix: 'experiment',
experimentsQueryParameter: 'experiment',
/* Fragment experiment needs redecoration */
// See more details below
decorationFunction: (el) => {
/* handle custom decoration here, for example: */
buildBlock(el);
decorateBlock(el);
}
});
```
Expand All @@ -176,6 +184,13 @@ For detailed implementation instructions on the different features, please read
- [Campaigns](/documentation/campaigns.md)
- [Experiments](/documentation/experiments.md)
**Cases of passing `decorationFunction`**
Fragment replacement is handled by async observer, which may execute before or after default decoration complete. So, you need to provide a decoration method to redecorate. There are several common cases:
1. Have a selector for an element inside a block and the block needs to be redecorated => sample code above
2. Have a `.block` selector and need to redecorate => switch block status to `"loading"` and call `loadBlock(el)`
3. Have a `.section` selector and need to redecorate => call `decorateBlocks(el)`
4. Have a `main` selector and need to redecorate => call `decorateMain(el)`
## Extensibility & integrations
If you need to further integrate the experimentation plugin with custom analytics reporting or other 3rd-party libraries, you can listen for the `aem:experimentation` event:
Expand Down
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export const DEFAULT_OPTIONS = {
// Experimentation related properties
experimentsMetaTagPrefix: 'experiment',
experimentsQueryParameter: 'experiment',

// Redecoration function for fragments
decorateFunction: () => {},
};

/**
Expand Down Expand Up @@ -436,6 +439,8 @@ function watchMutationsAndApplyFragments(
if (url && new URL(url, window.location.origin).pathname !== window.location.pathname) {
// eslint-disable-next-line no-await-in-loop
res = await replaceInner(new URL(url, window.location.origin).pathname, el, entry.selector);
// eslint-disable-next-line no-await-in-loop
await pluginOptions.decorateFunction(el);
} else {
res = url;
}
Expand Down
Loading