@@ -216,11 +216,11 @@ export default async function detect( {
216
216
) ;
217
217
218
218
/**
219
- * Monitors embed for mutations .
219
+ * Monitors embed wrapper for resizes .
220
220
*
221
221
* @param {HTMLDivElement } embedWrapper Embed wrapper DIV.
222
222
*/
223
- function monitorEmbedForMutations ( embedWrapper ) {
223
+ function monitorEmbedWrapperForResizes ( embedWrapper ) {
224
224
// If the embed lacks any scripting, then short-circuit since it can't possibly be doing any mutations.
225
225
if ( ! embedWrapper . querySelector ( 'script, [onload]' ) ) {
226
226
return ;
@@ -230,22 +230,27 @@ export default async function detect( {
230
230
}
231
231
const xpath = embedWrapper . dataset . odXpath ;
232
232
let timeoutId = 0 ;
233
- const observer = new MutationObserver ( ( ) => {
233
+ const observer = new ResizeObserver ( ( entries ) => {
234
+ const [ entry ] = entries ;
234
235
if ( timeoutId > 0 ) {
235
236
clearTimeout ( timeoutId ) ;
236
237
}
238
+ log (
239
+ `[Embed Optimizer] Pending embed height of ${ entry . contentRect . height } px for ${ xpath } `
240
+ ) ;
241
+ // TODO: Is the timeout really needed? We can just keep updating the height of the element until the URL metrics are sent when the page closes.
237
242
timeoutId = setTimeout ( ( ) => {
238
- const rect = embedWrapper . getBoundingClientRect ( ) ;
239
243
log (
240
- `[Embed Optimizer] Embed height of ${ rect . height } px for ${ xpath } `
244
+ `[Embed Optimizer] Final embed height of ${ entry . contentRect . height } px for ${ xpath } `
241
245
) ;
246
+ observer . disconnect ( ) ;
242
247
// TODO: Now amend URL metrics with this rect.height.
243
248
} , EMBED_LOAD_WAIT_MS ) ;
244
249
} ) ;
245
- observer . observe ( embedWrapper , { childList : true , subtree : true } ) ;
250
+ observer . observe ( embedWrapper , { box : 'content-box' } ) ;
246
251
}
247
252
for ( const embedWrapper of embedWrappers ) {
248
- monitorEmbedForMutations ( embedWrapper ) ;
253
+ monitorEmbedWrapperForResizes ( embedWrapper ) ;
249
254
}
250
255
251
256
// Wait until the resources on the page have fully loaded.
0 commit comments