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

Closes #6571: Reduce the processing on not-cached page when checking the LCP/ATF data #6610

Merged
merged 8 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions assets/js/lcp-beacon.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class RocketLcpBeacon {
this.infiniteLoopId = null;
}

init() {
if ( ! this._isValidPreconditions() ) {
async init() {
if ( ! await this._isValidPreconditions() ) {
this._finalize();
return;
}
Expand All @@ -32,35 +32,41 @@ class RocketLcpBeacon {
this._saveFinalResultIntoDB();
}

_isValidPreconditions() {
async _isValidPreconditions() {
// Check the screensize first because starting any logic.
if ( this._isNotValidScreensize() ) {
this._logMessage('Bailing out because screen size is not acceptable');
return false;
}

if ( this._isGeneratedBefore() ) {
if ( this._isPageCached() && await this._isGeneratedBefore() ) {
this._logMessage('Bailing out because data is already available');
return false;
}

return true;
}

_isGeneratedBefore() {
_isPageCached() {
const signature = document.documentElement.nextSibling && document.documentElement.nextSibling.data ? document.documentElement.nextSibling.data : '';

return signature && signature.includes( 'Debug: cached' );
}

async _isGeneratedBefore() {
// AJAX call to check if there are any records for the current URL.
let data_check = new FormData();
data_check.append('action', 'rocket_check_lcp');
data_check.append('rocket_lcp_nonce', this.config.nonce);
data_check.append('url', this.config.url);
data_check.append('is_mobile', this.config.is_mobile);

const lcp_data_response = fetch(this.config.ajax_url, {
method: "POST",
credentials: 'same-origin',
body: data_check
});

const lcp_data_response = await fetch(this.config.ajax_url, {
method: "POST",
credentials: 'same-origin',
body: data_check
})
.then(data => data.json());
return lcp_data_response.success;
}

Expand Down
3 changes: 1 addition & 2 deletions 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