1
1
/*
2
- * Copyright 2023 Adobe. All rights reserved.
2
+ * Copyright 2024 Adobe. All rights reserved.
3
3
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4
4
* you may not use this file except in compliance with the License. You may obtain a copy
5
5
* of the License at http://www.apache.org/licenses/LICENSE-2.0
22
22
* for instance the href of a link, or a search term
23
23
*/
24
24
function sampleRUM ( checkpoint , data = { } ) {
25
+ const SESSION_STORAGE_KEY = 'aem-rum' ;
26
+ sampleRUM . baseURL = sampleRUM . baseURL
27
+ || new URL ( window . RUM_BASE == null ? 'https://rum.hlx.page' : window . RUM_BASE , window . location ) ;
25
28
sampleRUM . defer = sampleRUM . defer || [ ] ;
26
29
const defer = ( fnname ) => {
27
30
sampleRUM [ fnname ] = sampleRUM [ fnname ] || ( ( ...args ) => sampleRUM . defer . push ( { fnname, args } ) ) ;
@@ -53,12 +56,21 @@ function sampleRUM(checkpoint, data = {}) {
53
56
. join ( '' ) ;
54
57
const random = Math . random ( ) ;
55
58
const isSelected = random * weight < 1 ;
56
- const firstReadTime = Date . now ( ) ;
59
+ const firstReadTime = window . performance ? window . performance . timeOrigin : Date . now ( ) ;
57
60
const urlSanitizers = {
58
61
full : ( ) => window . location . href ,
59
62
origin : ( ) => window . location . origin ,
60
63
path : ( ) => window . location . href . replace ( / \? .* $ / , '' ) ,
61
64
} ;
65
+ // eslint-disable-next-line max-len
66
+ const rumSessionStorage = sessionStorage . getItem ( SESSION_STORAGE_KEY )
67
+ ? JSON . parse ( sessionStorage . getItem ( SESSION_STORAGE_KEY ) )
68
+ : { } ;
69
+ // eslint-disable-next-line max-len
70
+ rumSessionStorage . pages = ( rumSessionStorage . pages ? rumSessionStorage . pages : 0 )
71
+ + 1
72
+ /* noise */ + ( Math . floor ( Math . random ( ) * 20 ) - 10 ) ;
73
+ sessionStorage . setItem ( SESSION_STORAGE_KEY , JSON . stringify ( rumSessionStorage ) ) ;
62
74
// eslint-disable-next-line object-curly-newline, max-len
63
75
window . hlx . rum = {
64
76
weight,
@@ -68,8 +80,10 @@ function sampleRUM(checkpoint, data = {}) {
68
80
firstReadTime,
69
81
sampleRUM,
70
82
sanitizeURL : urlSanitizers [ window . hlx . RUM_MASK_URL || 'path' ] ,
83
+ rumSessionStorage,
71
84
} ;
72
85
}
86
+
73
87
const { weight, id, firstReadTime } = window . hlx . rum ;
74
88
if ( window . hlx && window . hlx . rum && window . hlx . rum . isSelected ) {
75
89
const knownProperties = [
@@ -85,32 +99,35 @@ function sampleRUM(checkpoint, data = {}) {
85
99
'FID' ,
86
100
'LCP' ,
87
101
'INP' ,
102
+ 'TTFB' ,
88
103
] ;
89
104
const sendPing = ( pdata = data ) => {
105
+ // eslint-disable-next-line max-len
106
+ const t = Math . round (
107
+ window . performance ? window . performance . now ( ) : Date . now ( ) - firstReadTime ,
108
+ ) ;
90
109
// eslint-disable-next-line object-curly-newline, max-len, no-use-before-define
91
110
const body = JSON . stringify (
92
111
{
93
- weight,
94
- id,
95
- referer : window . hlx . rum . sanitizeURL ( ) ,
96
- checkpoint,
97
- t : Date . now ( ) - firstReadTime ,
98
- ...data ,
112
+ weight, id, referer : window . hlx . rum . sanitizeURL ( ) , checkpoint, t, ...data ,
99
113
} ,
100
114
knownProperties ,
101
115
) ;
102
- const url = `https://rum.hlx.page/.rum/${ weight } ` ;
103
- // eslint-disable-next-line no-unused-expressions
116
+ const url = new URL ( `.rum/${ weight } ` , sampleRUM . baseURL ) . href ;
104
117
navigator . sendBeacon ( url , body ) ;
105
118
// eslint-disable-next-line no-console
106
119
console . debug ( `ping:${ checkpoint } ` , pdata ) ;
107
120
} ;
108
121
sampleRUM . cases = sampleRUM . cases || {
122
+ load : ( ) => sampleRUM ( 'pagesviewed' , { source : window . hlx . rum . rumSessionStorage . pages } ) || true ,
109
123
cwv : ( ) => sampleRUM . cwv ( data ) || true ,
110
124
lazy : ( ) => {
111
125
// use classic script to avoid CORS issues
112
126
const script = document . createElement ( 'script' ) ;
113
- script . src = 'https://rum.hlx.page/.rum/@adobe/helix-rum-enhancer@^1/src/index.js' ;
127
+ script . src = new URL (
128
+ '.rum/@adobe/helix-rum-enhancer@^1/src/index.js' ,
129
+ sampleRUM . baseURL ,
130
+ ) . href ;
114
131
document . head . appendChild ( script ) ;
115
132
return true ;
116
133
} ,
@@ -355,6 +372,48 @@ function decorateTemplateAndTheme() {
355
372
if ( theme ) addClasses ( document . body , theme ) ;
356
373
}
357
374
375
+ /**
376
+ * Wrap inline text content of block cells within a <p> tag.
377
+ * @param {Element } block the block element
378
+ */
379
+ function wrapTextNodes ( block ) {
380
+ const validWrappers = [
381
+ 'P' ,
382
+ 'PRE' ,
383
+ 'UL' ,
384
+ 'OL' ,
385
+ 'PICTURE' ,
386
+ 'TABLE' ,
387
+ 'H1' ,
388
+ 'H2' ,
389
+ 'H3' ,
390
+ 'H4' ,
391
+ 'H5' ,
392
+ 'H6' ,
393
+ ] ;
394
+
395
+ const wrap = ( el ) => {
396
+ const wrapper = document . createElement ( 'p' ) ;
397
+ wrapper . append ( ...el . childNodes ) ;
398
+ el . append ( wrapper ) ;
399
+ } ;
400
+
401
+ block . querySelectorAll ( ':scope > div > div' ) . forEach ( ( blockColumn ) => {
402
+ if ( blockColumn . hasChildNodes ( ) ) {
403
+ const hasWrapper = ! ! blockColumn . firstElementChild
404
+ && validWrappers . some ( ( tagName ) => blockColumn . firstElementChild . tagName === tagName ) ;
405
+ if ( ! hasWrapper ) {
406
+ wrap ( blockColumn ) ;
407
+ } else if (
408
+ blockColumn . firstElementChild . tagName === 'PICTURE'
409
+ && ( blockColumn . children . length > 1 || ! ! blockColumn . textContent . trim ( ) )
410
+ ) {
411
+ wrap ( blockColumn ) ;
412
+ }
413
+ }
414
+ } ) ;
415
+ }
416
+
358
417
/**
359
418
* Decorates paragraphs containing a single link as buttons.
360
419
* @param {Element } element container element
@@ -618,6 +677,7 @@ function decorateBlock(block) {
618
677
block . classList . add ( 'block' ) ;
619
678
block . dataset . blockName = shortBlockName ;
620
679
block . dataset . blockStatus = 'initialized' ;
680
+ wrapTextNodes ( block ) ;
621
681
const blockWrapper = block . parentElement ;
622
682
blockWrapper . classList . add ( `${ shortBlockName } -wrapper` ) ;
623
683
const section = block . closest ( '.section' ) ;
@@ -706,4 +766,5 @@ export {
706
766
toClassName ,
707
767
updateSectionsStatus ,
708
768
waitForLCP ,
769
+ wrapTextNodes ,
709
770
} ;
0 commit comments