-
Notifications
You must be signed in to change notification settings - Fork 111
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
Add ability to prime URL metrics #1850
base: trunk
Are you sure you want to change the base?
Changes from 24 commits
57f9958
71cd706
da1d275
bfed250
6ec82e3
af0172e
bff9973
c155fc1
7640ef7
f05f6c0
0631daf
5d93fdc
3118fb3
d8cfa02
8879f60
72b18fd
f5bba48
b0422eb
db1fd81
63564c4
4fd36b6
82a16b5
a50f23e
9201c86
163d24e
57d77ea
2bc9f49
d17dede
97b888f
931861b
0a7c1f1
969f6b5
c1e245d
b7eeef7
41f5085
42bfb98
7487ae8
49f9eab
fed3012
591a013
17e24c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,8 @@ const consoleLogPrefix = '[Optimization Detective]'; | |
|
||
const storageLockTimeSessionKey = 'odStorageLockTime'; | ||
|
||
let odPrimeUrlMetricsVerificationToken = ''; | ||
|
||
/** | ||
* Checks whether storage is locked. | ||
* | ||
|
@@ -34,6 +36,10 @@ const storageLockTimeSessionKey = 'odStorageLockTime'; | |
* @return {boolean} Whether storage is locked. | ||
*/ | ||
function isStorageLocked( currentTime, storageLockTTL ) { | ||
if ( '' !== odPrimeUrlMetricsVerificationToken ) { | ||
return false; | ||
} | ||
|
||
if ( storageLockTTL === 0 ) { | ||
return false; | ||
} | ||
|
@@ -429,6 +435,18 @@ export default async function detect( { | |
} ); | ||
} | ||
|
||
/** @type {HTMLIFrameElement|null} */ | ||
const urlPrimeIframeElement = win.parent.document.querySelector( | ||
'iframe#od-prime-url-metrics-iframe' | ||
); | ||
if ( | ||
urlPrimeIframeElement && | ||
urlPrimeIframeElement.dataset.odPrimeUrlMetricsVerificationToken | ||
) { | ||
odPrimeUrlMetricsVerificationToken = | ||
urlPrimeIframeElement.dataset.odPrimeUrlMetricsVerificationToken; | ||
} | ||
|
||
// TODO: Does this make sense here? Should it be moved up above the isViewportNeeded condition? | ||
// As an alternative to this, the od_print_detection_script() function can short-circuit if the | ||
// od_is_url_metric_storage_locked() function returns true. However, the downside with that is page caching could | ||
|
@@ -663,6 +681,14 @@ export default async function detect( { | |
|
||
// Wait for the page to be hidden. | ||
await new Promise( ( resolve ) => { | ||
if ( '' !== odPrimeUrlMetricsVerificationToken ) { | ||
window.parent.postMessage( | ||
'OD_PRIME_URL_METRICS_REQUEST_SUCCESS', | ||
'*' | ||
); | ||
resolve(); | ||
} | ||
|
||
win.addEventListener( 'pagehide', resolve, { once: true } ); | ||
win.addEventListener( 'pageswap', resolve, { once: true } ); | ||
doc.addEventListener( | ||
|
@@ -767,6 +793,13 @@ export default async function detect( { | |
); | ||
} | ||
url.searchParams.set( 'hmac', urlMetricHMAC ); | ||
if ( '' !== odPrimeUrlMetricsVerificationToken ) { | ||
url.searchParams.set( | ||
'prime_url_metrics_verification_token', | ||
odPrimeUrlMetricsVerificationToken | ||
); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Authentication for REST API
In #1835 PR, WP nonces are introduced for REST API requests for logged-in users. This may allow us to eliminate the custom token authentication if URL metrics are collected exclusively from logged-in users. |
||
|
||
navigator.sendBeacon( | ||
url, | ||
new Blob( [ JSON.stringify( urlMetric ) ], { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parent and
IFRAME
communication is handled via postMessage. A message is sent to the parent, and the promise resolves immediately.If the promise isn't resolved immediately, navigating to a new URL causes the code following the promise to never execute. This is because changing the
iframe.src
does not trigger events likepagehide
,pageswap
, orvisibilitychange
.performance/plugins/optimization-detective/detect.js
Lines 568 to 569 in 6ca5c4b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder. Do we even need to post a message here? As soon as the
iframe
is destroyed won't it automatically cause the URL Metric to be sent, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is we need to signal the parent that we can move to next URL or breakpoint using
postMessage
as theload
event can't be used. Check this comment for detailed explanation #1850 (comment) .Will it makes sense to send the
postMessage
after thenavigator.sendBeacon
then?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think it makes sense to send the message after the beacon is sent, definitely.