Skip to content

Commit 5ad30db

Browse files
authored
Merge pull request #77 from adobe/syncwith-boiler-plate
chore: sync with boilerplate
2 parents dccd7a7 + 604026f commit 5ad30db

File tree

4 files changed

+73
-32
lines changed

4 files changed

+73
-32
lines changed

.github/pull_request_template.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ Please always provide the [GitHub issue(s)](../issues) your PR is for, as well a
33
Fix #<gh-issue-id>
44

55
Test URLs:
6-
- Before: https://main--aem-block-collection--adobe.hlx.page
7-
- After: https://<branch>--aem-block-collection--adobe.hlx.page
6+
- Before: https://main--{repo}--{owner}.aem.live/
7+
- After: https://<branch>--{repo}--{owner}.aem.live/

README.md

+14-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
# Block Collection
2-
3-
This project contains code that is featured in the [AEM Block Collection](https://www.aem.live/developer/block-collection#block-collection-1) documentation.
1+
# Your Project's Title...
2+
Your project's description...
43

54
## Environments
6-
- Preview: https://main--aem-block-collection--adobe.hlx.page/
7-
- Live: https://main--aem-block-collection--adobe.hlx.live/
5+
- Preview: https://main--{repo}--{owner}.aem.page/
6+
- Live: https://main--{repo}--{owner}.aem.live/
7+
8+
## Documentation
9+
10+
Before using the aem-block-collection, we recommand you to go through the documentation on https://www.aem.live/docs/ and more specifically:
11+
1. [Developer Tutorial](https://www.aem.live/developer/tutorial)
12+
2. [The Anatomy of a Project](https://www.aem.live/developer/anatomy-of-a-project)
13+
3. [Web Performance](https://www.aem.live/developer/keeping-it-100)
14+
4. [Markup, Sections, Blocks, and Auto Blocking](https://www.aem.live/developer/markup-sections-blocks)
15+
5. [AEM Block Collection](https://www.aem.live/developer/block-collection#block-collection-1)
816

917
## Installation
1018

@@ -24,4 +32,4 @@ npm run lint
2432
1. Add the [AEM Code Sync GitHub App](https://github.com/apps/aem-code-sync) to the repository
2533
1. Install the [AEM CLI](https://github.com/adobe/helix-cli): `npm install -g @adobe/aem-cli`
2634
1. Start AEM Proxy: `aem up` (opens your browser at `http://localhost:3000`)
27-
1. Open the `{repo}` directory in your favorite IDE and start coding :)
35+
1. Open the `{repo}` directory in your favorite IDE and start coding :)

scripts/aem.js

+57-21
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ function sampleRUM(checkpoint, data) {
1818
window.hlx = window.hlx || {};
1919
sampleRUM.enhance = () => {};
2020
if (!window.hlx.rum) {
21-
const weight = new URLSearchParams(window.location.search).get('rum') === 'on' ? 1 : 100;
21+
const param = new URLSearchParams(window.location.search).get('rum');
22+
const weight = (window.SAMPLE_PAGEVIEWS_AT_RATE === 'high' && 10)
23+
|| (window.SAMPLE_PAGEVIEWS_AT_RATE === 'low' && 1000)
24+
|| (param === 'on' && 1)
25+
|| 100;
2226
const id = Math.random().toString(36).slice(-4);
23-
const isSelected = Math.random() * weight < 1;
27+
const isSelected = param !== 'off' && Math.random() * weight < 1;
2428
// eslint-disable-next-line object-curly-newline, max-len
2529
window.hlx.rum = {
2630
weight,
@@ -32,23 +36,39 @@ function sampleRUM(checkpoint, data) {
3236
collector: (...args) => window.hlx.rum.queue.push(args),
3337
};
3438
if (isSelected) {
35-
['error', 'unhandledrejection'].forEach((event) => {
36-
window.addEventListener(event, ({ reason, error }) => {
37-
const errData = { source: 'undefined error' };
38-
try {
39-
errData.target = (reason || error).toString();
40-
errData.source = (reason || error).stack
41-
.split('\n')
42-
.filter((line) => line.match(/https?:\/\//))
43-
.shift()
44-
.replace(/at ([^ ]+) \((.+)\)/, '$1@$2')
45-
.trim();
46-
} catch (err) {
47-
/* error structure was not as expected */
48-
}
49-
sampleRUM('error', errData);
50-
});
39+
const dataFromErrorObj = (error) => {
40+
const errData = { source: 'undefined error' };
41+
try {
42+
errData.target = error.toString();
43+
errData.source = error.stack
44+
.split('\n')
45+
.filter((line) => line.match(/https?:\/\//))
46+
.shift()
47+
.replace(/at ([^ ]+) \((.+)\)/, '$1@$2')
48+
.replace(/ at /, '@')
49+
.trim();
50+
} catch (err) {
51+
/* error structure was not as expected */
52+
}
53+
return errData;
54+
};
55+
56+
window.addEventListener('error', ({ error }) => {
57+
const errData = dataFromErrorObj(error);
58+
sampleRUM('error', errData);
5159
});
60+
61+
window.addEventListener('unhandledrejection', ({ reason }) => {
62+
let errData = {
63+
source: 'Unhandled Rejection',
64+
target: reason || 'Unknown',
65+
};
66+
if (reason instanceof Error) {
67+
errData = dataFromErrorObj(reason);
68+
}
69+
sampleRUM('error', errData);
70+
});
71+
5272
sampleRUM.baseURL = sampleRUM.baseURL || new URL(window.RUM_BASE || '/', new URL('https://rum.hlx.page'));
5373
sampleRUM.collectBaseURL = sampleRUM.collectBaseURL || sampleRUM.baseURL;
5474
sampleRUM.sendPing = (ck, time, pingData = {}) => {
@@ -61,7 +81,13 @@ function sampleRUM(checkpoint, data) {
6181
t: time,
6282
...pingData,
6383
});
64-
const { href: url, origin } = new URL(`.rum/${weight}`, sampleRUM.collectBaseURL);
84+
const urlParams = window.RUM_PARAMS
85+
? `?${new URLSearchParams(window.RUM_PARAMS).toString()}`
86+
: '';
87+
const { href: url, origin } = new URL(
88+
`.rum/${weight}${urlParams}`,
89+
sampleRUM.collectBaseURL,
90+
);
6591
const body = origin === window.location.origin
6692
? new Blob([rumData], { type: 'application/json' })
6793
: rumData;
@@ -72,9 +98,16 @@ function sampleRUM(checkpoint, data) {
7298
sampleRUM.sendPing('top', timeShift());
7399

74100
sampleRUM.enhance = () => {
101+
// only enhance once
102+
if (document.querySelector('script[src*="rum-enhancer"]')) return;
103+
const { enhancerVersion, enhancerHash } = sampleRUM.enhancerContext || {};
75104
const script = document.createElement('script');
105+
if (enhancerHash) {
106+
script.integrity = enhancerHash;
107+
script.setAttribute('crossorigin', 'anonymous');
108+
}
76109
script.src = new URL(
77-
'.rum/@adobe/helix-rum-enhancer@^2/src/index.js',
110+
`.rum/@adobe/helix-rum-enhancer@${enhancerVersion || '^2'}/src/index.js`,
78111
sampleRUM.baseURL,
79112
).href;
80113
document.head.appendChild(script);
@@ -89,7 +122,7 @@ function sampleRUM(checkpoint, data) {
89122
}
90123
document.dispatchEvent(new CustomEvent('rum', { detail: { checkpoint, data } }));
91124
} catch (error) {
92-
// something went wrong
125+
// something went awry
93126
}
94127
}
95128

@@ -665,6 +698,9 @@ async function loadSections(element) {
665698
for (let i = 0; i < sections.length; i += 1) {
666699
// eslint-disable-next-line no-await-in-loop
667700
await loadSection(sections[i]);
701+
if (i === 0 && sampleRUM.enhance) {
702+
sampleRUM.enhance();
703+
}
668704
}
669705
}
670706

scripts/scripts.js

-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
loadSection,
1313
loadSections,
1414
loadCSS,
15-
sampleRUM,
1615
} from './aem.js';
1716

1817
/**
@@ -97,8 +96,6 @@ async function loadEager(doc) {
9796
await loadSection(main.querySelector('.section'), waitForFirstImage);
9897
}
9998

100-
sampleRUM.enhance();
101-
10299
try {
103100
/* if desktop (proxy for fast connection) or fonts already loaded, load fonts.css */
104101
if (window.innerWidth >= 900 || sessionStorage.getItem('fonts-loaded')) {

0 commit comments

Comments
 (0)