Skip to content
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

3.16 - Handle different errors from the beacon script #6603

Merged
merged 10 commits into from
Apr 30, 2024
46 changes: 36 additions & 10 deletions assets/js/lcp-beacon.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ class RocketLcpBeacon {
constructor( config ) {
this.config = config;
this.performanceImages = [];
this.errorCode = '';
this.scriptTimer = new Date();
this.infiniteLoopId = null;
}

init() {
Expand All @@ -10,11 +13,20 @@ class RocketLcpBeacon {
return;
}

// Use _generateLcpCandidates method to get all the elements in the viewport.
const above_the_fold_images = this._generateLcpCandidates( Infinity );
if ( above_the_fold_images ) {
this._initWithFirstElementWithInfo( above_the_fold_images );
this._fillATFWithoutDuplications( above_the_fold_images );
this.infiniteLoopId = setTimeout( () => {
this._handleInfiniteLoop();
}, 10000 );

try {
// Use _generateLcpCandidates method to get all the elements in the viewport.
const above_the_fold_images = this._generateLcpCandidates( Infinity );
if ( above_the_fold_images ) {
this._initWithFirstElementWithInfo( above_the_fold_images );
this._fillATFWithoutDuplications( above_the_fold_images );
}
} catch ( err ) {
this.errorCode = 'script_error';
this._logMessage( 'Script Error: ' + err );
}

this._saveFinalResultIntoDB();
Expand Down Expand Up @@ -245,20 +257,29 @@ class RocketLcpBeacon {
this.performanceImages.some(item => item.src === elementInfo.src);
}

_saveFinalResultIntoDB() {
if ( ! this.performanceImages ) {
return;
_getFinalStatus() {
if ( '' !== this.errorCode ) {
return this.errorCode;
}

const scriptTime = ( new Date() - this.scriptTimer ) / 1000;
if ( 10 <= scriptTime ) {
return 'timeout';
}

return 'success';
}

_saveFinalResultIntoDB() {
const data = new FormData();
data.append('action', 'rocket_lcp');
data.append('rocket_lcp_nonce', this.config.nonce);
data.append('url', this.config.url);
data.append('is_mobile', this.config.is_mobile);
data.append('images', JSON.stringify(this.performanceImages));
data.append('status', 'success');
data.append('status', this._getFinalStatus());

fetch(rocket_lcp_data.ajax_url, {
fetch(this.config.ajax_url, {
method: "POST",
credentials: 'same-origin',
body: data,
Expand All @@ -278,9 +299,14 @@ class RocketLcpBeacon {
});
}

_handleInfiniteLoop() {
this._saveFinalResultIntoDB();
}

_finalize() {
const beaconscript = document.querySelector('[data-name="wpr-lcp-beacon"]');
beaconscript.setAttribute('beacon-completed', 'true');
clearTimeout( this.infiniteLoopId );
}

_logMessage( msg ) {
Expand Down
3 changes: 2 additions & 1 deletion assets/js/lcp-beacon.js.min.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading