Skip to content

Commit

Permalink
feat(lib): update scripts/aem.js to aem.js@2.3.0 (#378)
Browse files Browse the repository at this point in the history
Test URL: https://update-lib-aem-minor-2-3-0--aem-boilerplate--adobe.hlx.live/
Release Notes: https://github.com/adobe/aem-lib/releases/tag/v2.3.0

Co-authored-by: semantic-release-bot <semantic-release-bot@martynus.net>
  • Loading branch information
adobe-bot and semantic-release-bot authored Jul 1, 2024
1 parent 4cc1faa commit c633b9e
Showing 1 changed file with 49 additions and 13 deletions.
62 changes: 49 additions & 13 deletions scripts/aem.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
* for instance the href of a link, or a search term
*/
function sampleRUM(checkpoint, data = {}) {
sampleRUM.baseURL = sampleRUM.baseURL || new URL(window.RUM_BASE == null ? 'https://rum.hlx.page' : window.RUM_BASE, window.location);
sampleRUM.baseURL = sampleRUM.baseURL
|| new URL(window.RUM_BASE == null ? 'https://rum.hlx.page' : window.RUM_BASE, window.location);
sampleRUM.defer = sampleRUM.defer || [];
const defer = (fnname) => {
sampleRUM[fnname] = sampleRUM[fnname]
|| ((...args) => sampleRUM.defer.push({ fnname, args }));
sampleRUM[fnname] = sampleRUM[fnname] || ((...args) => sampleRUM.defer.push({ fnname, args }));
};
sampleRUM.drain = sampleRUM.drain
|| ((dfnname, fn) => {
Expand All @@ -48,28 +48,57 @@ function sampleRUM(checkpoint, data = {}) {
window.hlx = window.hlx || {};
if (!window.hlx.rum) {
const usp = new URLSearchParams(window.location.search);
const weight = (usp.get('rum') === 'on') ? 1 : 100; // with parameter, weight is 1. Defaults to 100.
const weight = usp.get('rum') === 'on' ? 1 : 100; // with parameter, weight is 1. Defaults to 100.
const id = Math.random().toString(36).slice(-4);
const random = Math.random();
const isSelected = (random * weight < 1);
const isSelected = random * weight < 1;
const firstReadTime = window.performance ? window.performance.timeOrigin : Date.now();
const urlSanitizers = {
full: () => window.location.href,
origin: () => window.location.origin,
path: () => window.location.href.replace(/\?.*$/, ''),
};
// eslint-disable-next-line object-curly-newline, max-len
window.hlx.rum = { weight, id, random, isSelected, firstReadTime, sampleRUM, sanitizeURL: urlSanitizers[window.hlx.RUM_MASK_URL || 'path'] };
window.hlx.rum = {
weight,
id,
random,
isSelected,
firstReadTime,
sampleRUM,
sanitizeURL: urlSanitizers[window.hlx.RUM_MASK_URL || 'path'],
};
}

const { weight, id, firstReadTime } = window.hlx.rum;
if (window.hlx && window.hlx.rum && window.hlx.rum.isSelected) {
const knownProperties = ['weight', 'id', 'referer', 'checkpoint', 't', 'source', 'target', 'cwv', 'CLS', 'FID', 'LCP', 'INP', 'TTFB'];
const knownProperties = [
'weight',
'id',
'referer',
'checkpoint',
't',
'source',
'target',
'cwv',
'CLS',
'FID',
'LCP',
'INP',
'TTFB',
];
const sendPing = (pdata = data) => {
// eslint-disable-next-line max-len
const t = Math.round(window.performance ? window.performance.now() : (Date.now() - firstReadTime));
const t = Math.round(
window.performance ? window.performance.now() : Date.now() - firstReadTime,
);
// eslint-disable-next-line object-curly-newline, max-len, no-use-before-define
const body = JSON.stringify({ weight, id, referer: window.hlx.rum.sanitizeURL(), checkpoint, t, ...data }, knownProperties);
const body = JSON.stringify(
{
weight, id, referer: window.hlx.rum.sanitizeURL(), checkpoint, t, ...data,
},
knownProperties,
);
const url = new URL(`.rum/${weight}`, sampleRUM.baseURL).href;
navigator.sendBeacon(url, body);
// eslint-disable-next-line no-console
Expand All @@ -80,7 +109,10 @@ function sampleRUM(checkpoint, data = {}) {
lazy: () => {
// use classic script to avoid CORS issues
const script = document.createElement('script');
script.src = new URL('.rum/@adobe/helix-rum-enhancer@^1/src/index.js', sampleRUM.baseURL).href;
script.src = new URL(
'.rum/@adobe/helix-rum-enhancer@^1/src/index.js',
sampleRUM.baseURL,
).href;
document.head.appendChild(script);
return true;
},
Expand Down Expand Up @@ -133,11 +165,15 @@ function init() {
const errData = { source: 'undefined error' };
try {
errData.target = (reason || error).toString();
errData.source = (reason || error).stack.split('\n')
.filter((line) => line.match(/https?:\/\//)).shift()
errData.source = (reason || error).stack
.split('\n')
.filter((line) => line.match(/https?:\/\//))
.shift()
.replace(/at ([^ ]+) \((.+)\)/, '$1@$2')
.trim();
} catch (err) { /* error structure was not as expected */ }
} catch (err) {
/* error structure was not as expected */
}
sampleRUM('error', errData);
});
});
Expand Down

0 comments on commit c633b9e

Please sign in to comment.