Skip to content

Commit 81ba3e9

Browse files
authored
Merge pull request #202 from newfold-labs/fix/PRESS7-432-Optimize-Critical-CSS-Loading-error
optimize critical css loading error
2 parents 91f21c5 + 9a7e1a3 commit 81ba3e9

File tree

5 files changed

+37
-24
lines changed

5 files changed

+37
-24
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('lodash', 'react', 'react-jsx-runtime', 'wp-api-fetch', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-i18n'), 'version' => '769b0c231a65a628defd');
1+
<?php return array('dependencies' => array('lodash', 'react', 'wp-api-fetch', 'wp-data', 'wp-dom-ready', 'wp-element', 'wp-i18n'), 'version' => '3778bcaad0ca52323915');

build/performance/performance.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

includes/JetpackBoost/JetpackBoost.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ public function __construct( Container $container ) {
4242
* @return array SDK data.
4343
*/
4444
public function add_to_runtime( $sdk ) {
45-
$is_jetpack_boost_enabled = is_plugin_active( 'jetpack-boost/jetpack-boost.php' );
46-
$jetpack_options = get_option( 'jetpack_options', false );
47-
$is_jetpack_boost_connected = is_array( $jetpack_options ) && isset( $jetpack_options['id'] ) ? true : false;
45+
$is_jetpack_boost_enabled = is_plugin_active( 'jetpack-boost/jetpack-boost.php' );
46+
47+
if ( isset( $_GET['page'] ) && 'nfd-performance' === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
48+
delete_option( 'jb_get_started' );
49+
}
4850

4951
$values = array(
5052
'is_active' => $is_jetpack_boost_enabled,
@@ -56,7 +58,6 @@ public function add_to_runtime( $sdk ) {
5658
'minify_css' => $is_jetpack_boost_enabled ? get_option( 'jetpack_boost_status_minify-css', false ) : false,
5759
'minify_css_excludes' => implode( ',', get_option( 'jetpack_boost_ds_minify_css_excludes', array( 'admin-bar', 'dashicons', 'elementor-app' ) ) ),
5860
'install_token' => PluginInstaller::rest_get_plugin_install_hash(),
59-
'jetpack_boost_connected' => $is_jetpack_boost_connected,
6061
);
6162

6263
return array_merge( $sdk, array( 'jetpackboost' => $values ) );

includes/RestApi/JetpackController.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,7 @@ public function set_options( $request ) {
9696
);
9797
}
9898

99-
if ( 'critical-css' === $field['id'] && 1 === (int) $field['value'] && class_exists( 'Automattic\Jetpack_Boost\Lib\Critical_CSS\Data_Sync_Actions\Regenerate_CSS' ) ) {
100-
$css = new Regenerate_CSS();
101-
$css->handle( null, null );
102-
} elseif ( 'critical-css-premium' === $field['id'] ) {
99+
if ( 'critical-css-premium' === $field['id'] ) {
103100
$field['id'] = 'critical-css';
104101
}
105102

src/sections/JetpackBoost/index.js

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -128,36 +128,51 @@ const JetpackBoost = () => {
128128

129129
const handleRegenerateClick = async () => {
130130
setCssIsGenerating( true );
131-
let iframe;
132-
try {
133-
await new Promise( ( resolve ) => setTimeout( resolve, 1000 ) );
134-
if ( ! sdk.jetpack_boost_connected ) {
135-
await apiFetch( {
136-
url: NewfoldRuntime.createApiUrl(
137-
'/jetpack-boost/v1/connection'
138-
),
139-
method: 'POST',
140-
} );
141-
}
131+
132+
const response = await apiFetch( {
133+
url: NewfoldRuntime.createApiUrl( '/jetpack-boost/v1/connection' ),
134+
method: 'GET',
135+
} );
136+
137+
if ( ! response?.connected ) {
142138
await apiFetch( {
143139
url: NewfoldRuntime.createApiUrl(
144-
'/newfold-performance/v1/jetpack/regenerate_critical_css'
140+
'/jetpack-boost/v1/connection'
145141
),
146142
method: 'POST',
147143
} );
144+
}
145+
146+
let iframe;
147+
try {
148+
await new Promise( ( resolve ) => setTimeout( resolve, 1000 ) );
148149
const adminUrl = `${ siteUrl }/wp-admin/admin.php?page=jetpack-boost`;
149150
iframe = document.createElement( 'iframe' );
150151
iframe.src = adminUrl;
151152
document.body.appendChild( iframe );
152153
iframe.style.height = '0';
153-
iframe.onload = function () {
154+
iframe.onload = async function () {
154155
try {
156+
await new Promise( ( resolve ) =>
157+
setTimeout( resolve, 500 )
158+
);
155159
const iframeDocument =
156160
iframe.contentDocument || iframe.contentWindow.document;
157161

162+
const regenerateButton = iframeDocument.querySelector(
163+
'div[data-testid="critical-css-meta"] button'
164+
);
165+
166+
if ( regenerateButton ) {
167+
regenerateButton.click();
168+
}
169+
await new Promise( ( resolve ) =>
170+
setTimeout( resolve, 300 )
171+
);
158172
const progressBar = iframeDocument.querySelector(
159173
'div[role="progressbar"]'
160174
);
175+
161176
if ( ! progressBar ) {
162177
iframe?.remove();
163178
setCssIsGenerating( false );

0 commit comments

Comments
 (0)