From 02146371ca3b0d50c63d7d01b842f3020f5eb761 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Perona?= Date: Tue, 28 May 2024 08:33:16 -0400 Subject: [PATCH] add custom delay for beacon waiting time --- assets/js/lcp-beacon.js | 4 ++-- assets/js/lcp-beacon.js.min.map | 2 +- assets/js/lcp-beacon.min.js | 2 +- .../Media/AboveTheFold/Frontend/Controller.php | 14 ++++++++++++++ .../Frontend/Subscriber/HTML/output_w_beacon.html | 2 +- 5 files changed, 19 insertions(+), 5 deletions(-) diff --git a/assets/js/lcp-beacon.js b/assets/js/lcp-beacon.js index ee26eac106..dbbb2d47a3 100644 --- a/assets/js/lcp-beacon.js +++ b/assets/js/lcp-beacon.js @@ -332,14 +332,14 @@ class RocketLcpBeacon { if (document.readyState !== 'loading') { setTimeout(() => { instance.init(); - }, 500); + }, window.rocket_lcp_data.delay); return; } document.addEventListener("DOMContentLoaded", () => { setTimeout(() => { instance.init(); - }, 500); + }, window.rocket_lcp_data.delay); }); } } diff --git a/assets/js/lcp-beacon.js.min.map b/assets/js/lcp-beacon.js.min.map index cc3e30efa2..574ab90ce3 100644 --- a/assets/js/lcp-beacon.js.min.map +++ b/assets/js/lcp-beacon.js.min.map @@ -1 +1 @@ -{"version":3,"names":[],"mappings":"","sources":["lcp-beacon.js"],"sourcesContent":["!function n(r,s,o){function c(t,e){if(!s[t]){if(!r[t]){var i=\"function\"==typeof require&&require;if(!e&&i)return i(t,!0);if(a)return a(t,!0);throw(i=new Error(\"Cannot find module '\"+t+\"'\")).code=\"MODULE_NOT_FOUND\",i}i=s[t]={exports:{}},r[t][0].call(i.exports,function(e){return c(r[t][1][e]||e)},i,i.exports,n,r,s,o)}return s[t].exports}for(var a=\"function\"==typeof require&&require,e=0;e{this._handleInfiniteLoop()},1e4);try{var e=this._generateLcpCandidates(1/0);e&&(this._initWithFirstElementWithInfo(e),this._fillATFWithoutDuplications(e))}catch(e){this.errorCode=\"script_error\",this._logMessage(\"Script Error: \"+e)}this._saveFinalResultIntoDB()}else this._finalize()}async _isValidPreconditions(){return this._isNotValidScreensize()?(this._logMessage(\"Bailing out because screen size is not acceptable\"),!1):!this._isPageCached()||!await this._isGeneratedBefore()||(this._logMessage(\"Bailing out because data is already available\"),!1)}_isPageCached(){const e=document.documentElement.nextSibling&&document.documentElement.nextSibling.data?document.documentElement.nextSibling.data:\"\";return e&&e.includes(\"Debug: cached\")}async _isGeneratedBefore(){let e=new FormData;return e.append(\"action\",\"rocket_check_lcp\"),e.append(\"rocket_lcp_nonce\",this.config.nonce),e.append(\"url\",this.config.url),e.append(\"is_mobile\",this.config.is_mobile),(await fetch(this.config.ajax_url,{method:\"POST\",credentials:\"same-origin\",body:e}).then(e=>e.json())).success}_isNotValidScreensize(){var e=window.innerWidth||document.documentElement.clientWidth,t=window.innerHeight||document.documentElement.clientHeight,i=this.config.is_mobile&&(e>this.config.width_threshold||t>this.config.height_threshold),t=!this.config.is_mobile&&(e{var t=e.getBoundingClientRect();return{element:e,rect:t}}).filter(e=>0({item:e,area:this._getElementArea(e.rect),elementInfo:this._getElementInfo(e.element)})).sort((e,t)=>t.area-e.area).slice(0,e);return n.map(e=>({element:e.item.element,elementInfo:e.elementInfo}))}_isIntersecting(e){return 0<=e.bottom&&0<=e.right&&e.top<=(window.innerHeight||document.documentElement.clientHeight)&&e.left<=(window.innerWidth||document.documentElement.clientWidth)}_getElementArea(e){return Math.min(e.width,(window.innerWidth||document.documentElement.clientWidth)-e.left)*Math.min(e.height,(window.innerHeight||document.documentElement.clientHeight)-e.top)}_getElementInfo(e){var t=e.nodeName.toLowerCase();const i={type:\"\",src:\"\",srcset:\"\",sizes:\"\",sources:[],bg_set:[],current_src:\"\"};if(\"img\"===t&&e.srcset)i.type=\"img-srcset\",i.src=e.src,i.srcset=e.srcset,i.sizes=e.sizes,i.current_src=e.currentSrc;else if(\"img\"===t)i.type=\"img\",i.src=e.src,i.current_src=e.currentSrc;else if(\"video\"===t){i.type=\"img\";var n=e.querySelector(\"source\");i.src=e.poster||(n?n.src:\"\"),i.current_src=i.src}else if(\"svg\"===t){const r=e.querySelector(\"image\");r&&(i.type=\"img\",i.src=r.getAttribute(\"href\")||\"\",i.current_src=i.src)}else if(\"picture\"===t)i.type=\"picture\",t=e.querySelector(\"img:not(picture>img)\"),i.src=t?t.src:\"\",i.sources=Array.from(e.querySelectorAll(\"source\")).map(e=>({srcset:e.srcset||\"\",media:e.media||\"\"}));else{const s=window.getComputedStyle(e,null),o=[s.getPropertyValue(\"background-image\"),getComputedStyle(e,\":after\").getPropertyValue(\"background-image\"),getComputedStyle(e,\":before\").getPropertyValue(\"background-image\")],c=o.filter(e=>\"none\"!==e).join(\"\");if(i.type=\"bg-img\",c.includes(\"image-set(\")&&(i.type=\"bg-img-set\"),!c||\"\"===c)return null;const a=[...c.matchAll(/url\\(\\s*?['\"]?\\s*?(\\S+?)\\s*?[\"']?\\s*?\\)\\s*?([a-zA-Z0-9\\s]*[x|dpcm|dpi|dppx]?)/gi)];i.bg_set=a.map(e=>e[1]?{src:e[1].trim()+(e[2]?\" \"+e[2].trim():\"\")}:{}),i.bg_set.every(e=>\"\"===e.src)&&(i.bg_set=a.map(e=>e[1]?{src:e[1].trim()}:{})),0null!==e.elementInfo)))return this._logMessage(\"No LCP candidate found.\"),void(this.performanceImages=[]);this.performanceImages=[{...e.elementInfo,label:\"lcp\"}]}_fillATFWithoutDuplications(e){e.forEach(e=>{var{element:t,elementInfo:e}=e;this._isDuplicateImage(t)||this.performanceImages.push({...e,label:\"above-the-fold\"})})}_isDuplicateImage(e){const t=this._getElementInfo(e);if(null===t)return!1;var i=\"img\"===t.type||\"img-srcset\"===t.type||\"video\"===t.type,e=\"bg-img\"===t.type||\"bg-img-set\"===t.type||\"picture\"===t.type;return(i||e)&&this.performanceImages.some(e=>e.src===t.src)}_getFinalStatus(){return\"\"!==this.errorCode?this.errorCode:10<=(new Date-this.scriptTimer)/1e3?\"timeout\":\"success\"}_saveFinalResultIntoDB(){const e=new FormData;e.append(\"action\",\"rocket_lcp\"),e.append(\"rocket_lcp_nonce\",this.config.nonce),e.append(\"url\",this.config.url),e.append(\"is_mobile\",this.config.is_mobile),e.append(\"images\",JSON.stringify(this.performanceImages)),e.append(\"status\",this._getFinalStatus()),fetch(this.config.ajax_url,{method:\"POST\",credentials:\"same-origin\",body:e,headers:{\"wpr-saas-no-intercept\":!0}}).then(e=>e.json()).then(e=>{this._logMessage(e)}).catch(e=>{this._logMessage(e)}).finally(()=>{this._finalize()})}_handleInfiniteLoop(){this._saveFinalResultIntoDB()}_finalize(){const e=document.querySelector('[data-name=\"wpr-lcp-beacon\"]');e.setAttribute(\"beacon-completed\",\"true\"),clearTimeout(this.infiniteLoopId)}_logMessage(e){this.config.debug&&console.log(e)}static run(){if(window.rocket_lcp_data){const e=new n(window.rocket_lcp_data);\"loading\"===document.readyState?document.addEventListener(\"DOMContentLoaded\",()=>{setTimeout(()=>{e.init()},500)}):setTimeout(()=>{e.init()},500)}}}).run()},{}]},{},[1]);"],"file":"lcp-beacon.js"} \ No newline at end of file +{"version":3,"names":[],"mappings":"","sources":["lcp-beacon.js"],"sourcesContent":["!function n(r,s,o){function c(t,e){if(!s[t]){if(!r[t]){var i=\"function\"==typeof require&&require;if(!e&&i)return i(t,!0);if(a)return a(t,!0);throw(i=new Error(\"Cannot find module '\"+t+\"'\")).code=\"MODULE_NOT_FOUND\",i}i=s[t]={exports:{}},r[t][0].call(i.exports,function(e){return c(r[t][1][e]||e)},i,i.exports,n,r,s,o)}return s[t].exports}for(var a=\"function\"==typeof require&&require,e=0;e{this._handleInfiniteLoop()},1e4);try{var e=this._generateLcpCandidates(1/0);e&&(this._initWithFirstElementWithInfo(e),this._fillATFWithoutDuplications(e))}catch(e){this.errorCode=\"script_error\",this._logMessage(\"Script Error: \"+e)}this._saveFinalResultIntoDB()}else this._finalize()}async _isValidPreconditions(){return this._isNotValidScreensize()?(this._logMessage(\"Bailing out because screen size is not acceptable\"),!1):!this._isPageCached()||!await this._isGeneratedBefore()||(this._logMessage(\"Bailing out because data is already available\"),!1)}_isPageCached(){const e=document.documentElement.nextSibling&&document.documentElement.nextSibling.data?document.documentElement.nextSibling.data:\"\";return e&&e.includes(\"Debug: cached\")}async _isGeneratedBefore(){let e=new FormData;return e.append(\"action\",\"rocket_check_lcp\"),e.append(\"rocket_lcp_nonce\",this.config.nonce),e.append(\"url\",this.config.url),e.append(\"is_mobile\",this.config.is_mobile),(await fetch(this.config.ajax_url,{method:\"POST\",credentials:\"same-origin\",body:e}).then(e=>e.json())).success}_isNotValidScreensize(){var e=window.innerWidth||document.documentElement.clientWidth,t=window.innerHeight||document.documentElement.clientHeight,i=this.config.is_mobile&&(e>this.config.width_threshold||t>this.config.height_threshold),t=!this.config.is_mobile&&(e{var t=e.getBoundingClientRect();return{element:e,rect:t}}).filter(e=>0({item:e,area:this._getElementArea(e.rect),elementInfo:this._getElementInfo(e.element)})).sort((e,t)=>t.area-e.area).slice(0,e);return n.map(e=>({element:e.item.element,elementInfo:e.elementInfo}))}_isIntersecting(e){return 0<=e.bottom&&0<=e.right&&e.top<=(window.innerHeight||document.documentElement.clientHeight)&&e.left<=(window.innerWidth||document.documentElement.clientWidth)}_getElementArea(e){return Math.min(e.width,(window.innerWidth||document.documentElement.clientWidth)-e.left)*Math.min(e.height,(window.innerHeight||document.documentElement.clientHeight)-e.top)}_getElementInfo(e){var t=e.nodeName.toLowerCase();const i={type:\"\",src:\"\",srcset:\"\",sizes:\"\",sources:[],bg_set:[],current_src:\"\"};if(\"img\"===t&&e.srcset)i.type=\"img-srcset\",i.src=e.src,i.srcset=e.srcset,i.sizes=e.sizes,i.current_src=e.currentSrc;else if(\"img\"===t)i.type=\"img\",i.src=e.src,i.current_src=e.currentSrc;else if(\"video\"===t){i.type=\"img\";var n=e.querySelector(\"source\");i.src=e.poster||(n?n.src:\"\"),i.current_src=i.src}else if(\"svg\"===t){const r=e.querySelector(\"image\");r&&(i.type=\"img\",i.src=r.getAttribute(\"href\")||\"\",i.current_src=i.src)}else if(\"picture\"===t)i.type=\"picture\",t=e.querySelector(\"img:not(picture>img)\"),i.src=t?t.src:\"\",i.sources=Array.from(e.querySelectorAll(\"source\")).map(e=>({srcset:e.srcset||\"\",media:e.media||\"\"}));else{const s=window.getComputedStyle(e,null),o=[s.getPropertyValue(\"background-image\"),getComputedStyle(e,\":after\").getPropertyValue(\"background-image\"),getComputedStyle(e,\":before\").getPropertyValue(\"background-image\")],c=o.filter(e=>\"none\"!==e).join(\"\");if(i.type=\"bg-img\",c.includes(\"image-set(\")&&(i.type=\"bg-img-set\"),!c||\"\"===c)return null;const a=[...c.matchAll(/url\\(\\s*?['\"]?\\s*?(\\S+?)\\s*?[\"']?\\s*?\\)\\s*?([a-zA-Z0-9\\s]*[x|dpcm|dpi|dppx]?)/gi)];i.bg_set=a.map(e=>e[1]?{src:e[1].trim()+(e[2]?\" \"+e[2].trim():\"\")}:{}),i.bg_set.every(e=>\"\"===e.src)&&(i.bg_set=a.map(e=>e[1]?{src:e[1].trim()}:{})),0null!==e.elementInfo)))return this._logMessage(\"No LCP candidate found.\"),void(this.performanceImages=[]);this.performanceImages=[{...e.elementInfo,label:\"lcp\"}]}_fillATFWithoutDuplications(e){e.forEach(e=>{var{element:t,elementInfo:e}=e;this._isDuplicateImage(t)||this.performanceImages.push({...e,label:\"above-the-fold\"})})}_isDuplicateImage(e){const t=this._getElementInfo(e);if(null===t)return!1;var i=\"img\"===t.type||\"img-srcset\"===t.type||\"video\"===t.type,e=\"bg-img\"===t.type||\"bg-img-set\"===t.type||\"picture\"===t.type;return(i||e)&&this.performanceImages.some(e=>e.src===t.src)}_getFinalStatus(){return\"\"!==this.errorCode?this.errorCode:10<=(new Date-this.scriptTimer)/1e3?\"timeout\":\"success\"}_saveFinalResultIntoDB(){const e=new FormData;e.append(\"action\",\"rocket_lcp\"),e.append(\"rocket_lcp_nonce\",this.config.nonce),e.append(\"url\",this.config.url),e.append(\"is_mobile\",this.config.is_mobile),e.append(\"images\",JSON.stringify(this.performanceImages)),e.append(\"status\",this._getFinalStatus()),fetch(this.config.ajax_url,{method:\"POST\",credentials:\"same-origin\",body:e,headers:{\"wpr-saas-no-intercept\":!0}}).then(e=>e.json()).then(e=>{this._logMessage(e)}).catch(e=>{this._logMessage(e)}).finally(()=>{this._finalize()})}_handleInfiniteLoop(){this._saveFinalResultIntoDB()}_finalize(){const e=document.querySelector('[data-name=\"wpr-lcp-beacon\"]');e.setAttribute(\"beacon-completed\",\"true\"),clearTimeout(this.infiniteLoopId)}_logMessage(e){this.config.debug&&console.log(e)}static run(){if(window.rocket_lcp_data){const e=new n(window.rocket_lcp_data);\"loading\"===document.readyState?document.addEventListener(\"DOMContentLoaded\",()=>{setTimeout(()=>{e.init()},window.rocket_lcp_data.delay)}):setTimeout(()=>{e.init()},window.rocket_lcp_data.delay)}}}).run()},{}]},{},[1]);"],"file":"lcp-beacon.js"} \ No newline at end of file diff --git a/assets/js/lcp-beacon.min.js b/assets/js/lcp-beacon.min.js index 6e9e6614e4..096e7a62a3 100644 --- a/assets/js/lcp-beacon.min.js +++ b/assets/js/lcp-beacon.min.js @@ -1,2 +1,2 @@ -!function n(r,s,o){function c(t,e){if(!s[t]){if(!r[t]){var i="function"==typeof require&&require;if(!e&&i)return i(t,!0);if(a)return a(t,!0);throw(i=new Error("Cannot find module '"+t+"'")).code="MODULE_NOT_FOUND",i}i=s[t]={exports:{}},r[t][0].call(i.exports,function(e){return c(r[t][1][e]||e)},i,i.exports,n,r,s,o)}return s[t].exports}for(var a="function"==typeof require&&require,e=0;e{this._handleInfiniteLoop()},1e4);try{var e=this._generateLcpCandidates(1/0);e&&(this._initWithFirstElementWithInfo(e),this._fillATFWithoutDuplications(e))}catch(e){this.errorCode="script_error",this._logMessage("Script Error: "+e)}this._saveFinalResultIntoDB()}else this._finalize()}async _isValidPreconditions(){return this._isNotValidScreensize()?(this._logMessage("Bailing out because screen size is not acceptable"),!1):!this._isPageCached()||!await this._isGeneratedBefore()||(this._logMessage("Bailing out because data is already available"),!1)}_isPageCached(){const e=document.documentElement.nextSibling&&document.documentElement.nextSibling.data?document.documentElement.nextSibling.data:"";return e&&e.includes("Debug: cached")}async _isGeneratedBefore(){let e=new FormData;return e.append("action","rocket_check_lcp"),e.append("rocket_lcp_nonce",this.config.nonce),e.append("url",this.config.url),e.append("is_mobile",this.config.is_mobile),(await fetch(this.config.ajax_url,{method:"POST",credentials:"same-origin",body:e}).then(e=>e.json())).success}_isNotValidScreensize(){var e=window.innerWidth||document.documentElement.clientWidth,t=window.innerHeight||document.documentElement.clientHeight,i=this.config.is_mobile&&(e>this.config.width_threshold||t>this.config.height_threshold),t=!this.config.is_mobile&&(e{var t=e.getBoundingClientRect();return{element:e,rect:t}}).filter(e=>0({item:e,area:this._getElementArea(e.rect),elementInfo:this._getElementInfo(e.element)})).sort((e,t)=>t.area-e.area).slice(0,e);return n.map(e=>({element:e.item.element,elementInfo:e.elementInfo}))}_isIntersecting(e){return 0<=e.bottom&&0<=e.right&&e.top<=(window.innerHeight||document.documentElement.clientHeight)&&e.left<=(window.innerWidth||document.documentElement.clientWidth)}_getElementArea(e){return Math.min(e.width,(window.innerWidth||document.documentElement.clientWidth)-e.left)*Math.min(e.height,(window.innerHeight||document.documentElement.clientHeight)-e.top)}_getElementInfo(e){var t=e.nodeName.toLowerCase();const i={type:"",src:"",srcset:"",sizes:"",sources:[],bg_set:[],current_src:""};if("img"===t&&e.srcset)i.type="img-srcset",i.src=e.src,i.srcset=e.srcset,i.sizes=e.sizes,i.current_src=e.currentSrc;else if("img"===t)i.type="img",i.src=e.src,i.current_src=e.currentSrc;else if("video"===t){i.type="img";var n=e.querySelector("source");i.src=e.poster||(n?n.src:""),i.current_src=i.src}else if("svg"===t){const r=e.querySelector("image");r&&(i.type="img",i.src=r.getAttribute("href")||"",i.current_src=i.src)}else if("picture"===t)i.type="picture",t=e.querySelector("img:not(picture>img)"),i.src=t?t.src:"",i.sources=Array.from(e.querySelectorAll("source")).map(e=>({srcset:e.srcset||"",media:e.media||""}));else{const s=window.getComputedStyle(e,null),o=[s.getPropertyValue("background-image"),getComputedStyle(e,":after").getPropertyValue("background-image"),getComputedStyle(e,":before").getPropertyValue("background-image")],c=o.filter(e=>"none"!==e).join("");if(i.type="bg-img",c.includes("image-set(")&&(i.type="bg-img-set"),!c||""===c)return null;const a=[...c.matchAll(/url\(\s*?['"]?\s*?(\S+?)\s*?["']?\s*?\)\s*?([a-zA-Z0-9\s]*[x|dpcm|dpi|dppx]?)/gi)];i.bg_set=a.map(e=>e[1]?{src:e[1].trim()+(e[2]?" "+e[2].trim():"")}:{}),i.bg_set.every(e=>""===e.src)&&(i.bg_set=a.map(e=>e[1]?{src:e[1].trim()}:{})),0null!==e.elementInfo)))return this._logMessage("No LCP candidate found."),void(this.performanceImages=[]);this.performanceImages=[{...e.elementInfo,label:"lcp"}]}_fillATFWithoutDuplications(e){e.forEach(e=>{var{element:t,elementInfo:e}=e;this._isDuplicateImage(t)||this.performanceImages.push({...e,label:"above-the-fold"})})}_isDuplicateImage(e){const t=this._getElementInfo(e);if(null===t)return!1;var i="img"===t.type||"img-srcset"===t.type||"video"===t.type,e="bg-img"===t.type||"bg-img-set"===t.type||"picture"===t.type;return(i||e)&&this.performanceImages.some(e=>e.src===t.src)}_getFinalStatus(){return""!==this.errorCode?this.errorCode:10<=(new Date-this.scriptTimer)/1e3?"timeout":"success"}_saveFinalResultIntoDB(){const e=new FormData;e.append("action","rocket_lcp"),e.append("rocket_lcp_nonce",this.config.nonce),e.append("url",this.config.url),e.append("is_mobile",this.config.is_mobile),e.append("images",JSON.stringify(this.performanceImages)),e.append("status",this._getFinalStatus()),fetch(this.config.ajax_url,{method:"POST",credentials:"same-origin",body:e,headers:{"wpr-saas-no-intercept":!0}}).then(e=>e.json()).then(e=>{this._logMessage(e)}).catch(e=>{this._logMessage(e)}).finally(()=>{this._finalize()})}_handleInfiniteLoop(){this._saveFinalResultIntoDB()}_finalize(){const e=document.querySelector('[data-name="wpr-lcp-beacon"]');e.setAttribute("beacon-completed","true"),clearTimeout(this.infiniteLoopId)}_logMessage(e){this.config.debug&&console.log(e)}static run(){if(window.rocket_lcp_data){const e=new n(window.rocket_lcp_data);"loading"===document.readyState?document.addEventListener("DOMContentLoaded",()=>{setTimeout(()=>{e.init()},500)}):setTimeout(()=>{e.init()},500)}}}).run()},{}]},{},[1]); +!function n(r,s,o){function c(t,e){if(!s[t]){if(!r[t]){var i="function"==typeof require&&require;if(!e&&i)return i(t,!0);if(a)return a(t,!0);throw(i=new Error("Cannot find module '"+t+"'")).code="MODULE_NOT_FOUND",i}i=s[t]={exports:{}},r[t][0].call(i.exports,function(e){return c(r[t][1][e]||e)},i,i.exports,n,r,s,o)}return s[t].exports}for(var a="function"==typeof require&&require,e=0;e{this._handleInfiniteLoop()},1e4);try{var e=this._generateLcpCandidates(1/0);e&&(this._initWithFirstElementWithInfo(e),this._fillATFWithoutDuplications(e))}catch(e){this.errorCode="script_error",this._logMessage("Script Error: "+e)}this._saveFinalResultIntoDB()}else this._finalize()}async _isValidPreconditions(){return this._isNotValidScreensize()?(this._logMessage("Bailing out because screen size is not acceptable"),!1):!this._isPageCached()||!await this._isGeneratedBefore()||(this._logMessage("Bailing out because data is already available"),!1)}_isPageCached(){const e=document.documentElement.nextSibling&&document.documentElement.nextSibling.data?document.documentElement.nextSibling.data:"";return e&&e.includes("Debug: cached")}async _isGeneratedBefore(){let e=new FormData;return e.append("action","rocket_check_lcp"),e.append("rocket_lcp_nonce",this.config.nonce),e.append("url",this.config.url),e.append("is_mobile",this.config.is_mobile),(await fetch(this.config.ajax_url,{method:"POST",credentials:"same-origin",body:e}).then(e=>e.json())).success}_isNotValidScreensize(){var e=window.innerWidth||document.documentElement.clientWidth,t=window.innerHeight||document.documentElement.clientHeight,i=this.config.is_mobile&&(e>this.config.width_threshold||t>this.config.height_threshold),t=!this.config.is_mobile&&(e{var t=e.getBoundingClientRect();return{element:e,rect:t}}).filter(e=>0({item:e,area:this._getElementArea(e.rect),elementInfo:this._getElementInfo(e.element)})).sort((e,t)=>t.area-e.area).slice(0,e);return n.map(e=>({element:e.item.element,elementInfo:e.elementInfo}))}_isIntersecting(e){return 0<=e.bottom&&0<=e.right&&e.top<=(window.innerHeight||document.documentElement.clientHeight)&&e.left<=(window.innerWidth||document.documentElement.clientWidth)}_getElementArea(e){return Math.min(e.width,(window.innerWidth||document.documentElement.clientWidth)-e.left)*Math.min(e.height,(window.innerHeight||document.documentElement.clientHeight)-e.top)}_getElementInfo(e){var t=e.nodeName.toLowerCase();const i={type:"",src:"",srcset:"",sizes:"",sources:[],bg_set:[],current_src:""};if("img"===t&&e.srcset)i.type="img-srcset",i.src=e.src,i.srcset=e.srcset,i.sizes=e.sizes,i.current_src=e.currentSrc;else if("img"===t)i.type="img",i.src=e.src,i.current_src=e.currentSrc;else if("video"===t){i.type="img";var n=e.querySelector("source");i.src=e.poster||(n?n.src:""),i.current_src=i.src}else if("svg"===t){const r=e.querySelector("image");r&&(i.type="img",i.src=r.getAttribute("href")||"",i.current_src=i.src)}else if("picture"===t)i.type="picture",t=e.querySelector("img:not(picture>img)"),i.src=t?t.src:"",i.sources=Array.from(e.querySelectorAll("source")).map(e=>({srcset:e.srcset||"",media:e.media||""}));else{const s=window.getComputedStyle(e,null),o=[s.getPropertyValue("background-image"),getComputedStyle(e,":after").getPropertyValue("background-image"),getComputedStyle(e,":before").getPropertyValue("background-image")],c=o.filter(e=>"none"!==e).join("");if(i.type="bg-img",c.includes("image-set(")&&(i.type="bg-img-set"),!c||""===c)return null;const a=[...c.matchAll(/url\(\s*?['"]?\s*?(\S+?)\s*?["']?\s*?\)\s*?([a-zA-Z0-9\s]*[x|dpcm|dpi|dppx]?)/gi)];i.bg_set=a.map(e=>e[1]?{src:e[1].trim()+(e[2]?" "+e[2].trim():"")}:{}),i.bg_set.every(e=>""===e.src)&&(i.bg_set=a.map(e=>e[1]?{src:e[1].trim()}:{})),0null!==e.elementInfo)))return this._logMessage("No LCP candidate found."),void(this.performanceImages=[]);this.performanceImages=[{...e.elementInfo,label:"lcp"}]}_fillATFWithoutDuplications(e){e.forEach(e=>{var{element:t,elementInfo:e}=e;this._isDuplicateImage(t)||this.performanceImages.push({...e,label:"above-the-fold"})})}_isDuplicateImage(e){const t=this._getElementInfo(e);if(null===t)return!1;var i="img"===t.type||"img-srcset"===t.type||"video"===t.type,e="bg-img"===t.type||"bg-img-set"===t.type||"picture"===t.type;return(i||e)&&this.performanceImages.some(e=>e.src===t.src)}_getFinalStatus(){return""!==this.errorCode?this.errorCode:10<=(new Date-this.scriptTimer)/1e3?"timeout":"success"}_saveFinalResultIntoDB(){const e=new FormData;e.append("action","rocket_lcp"),e.append("rocket_lcp_nonce",this.config.nonce),e.append("url",this.config.url),e.append("is_mobile",this.config.is_mobile),e.append("images",JSON.stringify(this.performanceImages)),e.append("status",this._getFinalStatus()),fetch(this.config.ajax_url,{method:"POST",credentials:"same-origin",body:e,headers:{"wpr-saas-no-intercept":!0}}).then(e=>e.json()).then(e=>{this._logMessage(e)}).catch(e=>{this._logMessage(e)}).finally(()=>{this._finalize()})}_handleInfiniteLoop(){this._saveFinalResultIntoDB()}_finalize(){const e=document.querySelector('[data-name="wpr-lcp-beacon"]');e.setAttribute("beacon-completed","true"),clearTimeout(this.infiniteLoopId)}_logMessage(e){this.config.debug&&console.log(e)}static run(){if(window.rocket_lcp_data){const e=new n(window.rocket_lcp_data);"loading"===document.readyState?document.addEventListener("DOMContentLoaded",()=>{setTimeout(()=>{e.init()},window.rocket_lcp_data.delay)}):setTimeout(()=>{e.init()},window.rocket_lcp_data.delay)}}}).run()},{}]},{},[1]); //# sourceMappingURL=lcp-beacon.js.map diff --git a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php index 1da1644c27..c3bf3179f9 100644 --- a/inc/Engine/Media/AboveTheFold/Frontend/Controller.php +++ b/inc/Engine/Media/AboveTheFold/Frontend/Controller.php @@ -394,6 +394,19 @@ public function inject_beacon( $html, $url, $is_mobile ): string { $height_threshold = $default_height_threshold; } + $default_delay = 500; + + /** + * Filters the delay before the LCP beacon is triggered. + * + * @param int $delay The delay in milliseconds. Default is 500. + */ + $delay = apply_filters( 'rocket_lcp_delay', $default_delay ); + + if ( ! is_int( $delay ) ) { + $delay = $default_delay; + } + $data = [ 'ajax_url' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'rocket_lcp' ), @@ -402,6 +415,7 @@ public function inject_beacon( $html, $url, $is_mobile ): string { 'elements' => $this->lcp_atf_elements(), 'width_threshold' => $width_threshold, 'height_threshold' => $height_threshold, + 'delay' => $delay, 'debug' => rocket_get_constant( 'WP_ROCKET_DEBUG' ), ]; diff --git a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_w_beacon.html b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_w_beacon.html index 17a596b635..674a29dbee 100644 --- a/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_w_beacon.html +++ b/tests/Fixtures/inc/Engine/Media/AboveTheFold/Frontend/Subscriber/HTML/output_w_beacon.html @@ -3,5 +3,5 @@ Test - +