Skip to content

Commit

Permalink
feat: add support for proper tracking in a speculative pre-rendering …
Browse files Browse the repository at this point in the history
…context (#102)

* feat: add support for proper tracking in a speculative pre-rendering context

* test: add unit tests
  • Loading branch information
ramboz authored Oct 7, 2024
1 parent 08fa2c0 commit aba822a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,10 @@ export function setup() {
/* c8 ignore next 18 */
export function init() {
setup();
sampleRUM();
// Prerender-aware initialization
if (document.prerendering) {
document.addEventListener('prerenderingchange', sampleRUM, { once: true });
} else {
sampleRUM();
}
}
10 changes: 9 additions & 1 deletion test/setup/setup.test.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/* eslint-env mocha */
import { runTests } from '@web/test-runner-mocha';
import { expect } from '@esm-bundle/chai';
import { setup } from '../../src/setup.js';
import { setup, init } from '../../src/setup.js';

runTests(() => {
it('setup - defines window.hlx', () => {
Expand All @@ -33,6 +33,14 @@
expect(window.hlx.codeBasePath).to.be.equal('/some/path');
expect(window.hlx.lighthouse).to.be.false;
});

it('init - does not enable RUM during prerendering', () => {
Object.defineProperty(document, 'prerendering', { value: true });
init();
expect(window.hlx.rum).to.be.undefined;
document.dispatchEvent(new Event('prerenderingchange'));
expect(window.hlx.rum).to.not.be.undefined;
});
});
</script>
</body>
Expand Down

0 comments on commit aba822a

Please sign in to comment.