diff --git a/web/app/themes/gds/app/Providers/AsyncLoaderServiceProvider.php b/web/app/themes/gds/app/Providers/AsyncLoaderServiceProvider.php deleted file mode 100644 index 5aceff73..00000000 --- a/web/app/themes/gds/app/Providers/AsyncLoaderServiceProvider.php +++ /dev/null @@ -1,79 +0,0 @@ -get_data($handle, 'crossorigin')) { - $tag = str_replace(' src', sprintf(' crossorigin="%s" src', $crossorigin), $tag); - } - - if (wp_styles()->get_data($handle, 'async')) { - $tag = str_replace(' src', " async src", $tag); - } elseif (wp_styles()->get_data($handle, 'defer')) { - $tag = str_replace(' src', " defer src", $tag); - } else { - foreach ( - [ - 'defer' => config('assets.deferred_scripts'), - 'async' => config('assets.async_scripts'), - ] as $type => $scripts - ) { - if (in_array($handle, $scripts)) { - $tag = str_replace(' src', " $type src", $tag); - } - } - } - - return $tag; - } - - /** - * Load styles asynchronously. - */ - public function styleLoaderTag(string $html, string $handle): string - { - if (is_admin()) { - return $html; - } - - $isAsync = wp_styles()->get_data($handle, 'async'); - if (!$isAsync && !in_array($handle, config('assets.async_styles'))) { - return $html; - } - - $dom = new \DOMDocument(); - $dom->loadHTML($html); - /** @var \DOMElement $tag */ - $tag = $dom->getElementsByTagName('link')->item(0); - $tag->setAttribute('media', 'print'); - $tag->setAttribute('onload', "this.media='all'"); - $tag->removeAttribute('type'); - $tag->removeAttribute('id'); - $html = $dom->saveHTML($tag); - - return $html; - } -} diff --git a/web/app/themes/gds/app/Providers/AsyncStyleLoaderServiceProvider.php b/web/app/themes/gds/app/Providers/AsyncStyleLoaderServiceProvider.php new file mode 100644 index 00000000..5deb4e8e --- /dev/null +++ b/web/app/themes/gds/app/Providers/AsyncStyleLoaderServiceProvider.php @@ -0,0 +1,45 @@ +get_data($handle, 'strategy'); + if ($strategy !== 'async') { + return $html; + } + + $dom = new \DOMDocument(); + $dom->loadHTML($html); + /** @var \DOMElement $tag */ + $tag = $dom->getElementsByTagName('link')->item(0); + $tag->setAttribute('media', 'print'); + $tag->setAttribute('onload', "this.media='all'"); + $tag->removeAttribute('type'); + $tag->removeAttribute('id'); + $html = $dom->saveHTML($tag); + + return $html; + } +} diff --git a/web/app/themes/gds/app/Providers/PerformanceServiceProvider.php b/web/app/themes/gds/app/Providers/PerformanceServiceProvider.php index 1afe5a07..6a1956e7 100644 --- a/web/app/themes/gds/app/Providers/PerformanceServiceProvider.php +++ b/web/app/themes/gds/app/Providers/PerformanceServiceProvider.php @@ -21,6 +21,7 @@ public function register() add_action('wp_enqueue_scripts', [$this, 'replaceWithModernJquery']); add_action('wp_enqueue_scripts', [$this, 'maybeRemoveJquery'], 100); + add_action('wp_enqueue_scripts', [$this, 'asyncLoadScripts'], 100); add_action('wp_print_styles', [$this, 'dequeueAssets'], 100); } @@ -56,6 +57,32 @@ function_exists('is_checkout') && (is_checkout() || is_cart()), } } + public function asyncLoadScripts(): void + { + // Async styles + collect([ + 'dashicons', + 'debug-bar', + 'shc-show-env', + 'wp-block-library-theme', + 'wp-smart-crop-renderer', + ])->each(fn (string $handle) => wp_style_add_data($handle, 'strategy', 'async')); + + // Async scripts + collect([ + 'genero-cmp/js', + ])->each(fn (string $handle) => wp_script_add_data($handle, 'strategy', 'async')); + + // Deferred scripts + collect([ + 'admin-bar', + 'debug-bar-js', + 'moxiejs', // gravityforms + 'plupload', // gravityforms + 'gform_gravityforms_utils', // gravityforms + ])->each(fn (string $handle) => wp_script_add_data($handle, 'strategy', 'defer')); + } + /** * Replace core jQuery with theme's jQuery. */ diff --git a/web/app/themes/gds/app/setup.php b/web/app/themes/gds/app/setup.php index 631ab156..9e8412c9 100755 --- a/web/app/themes/gds/app/setup.php +++ b/web/app/themes/gds/app/setup.php @@ -20,9 +20,8 @@ * @return void */ add_action('wp_enqueue_scripts', function () { - wp_enqueue_script('sage/app.js', asset('scripts/app.js')->uri(), [], null, true); + wp_enqueue_script('sage/app.js', asset('scripts/app.js')->uri(), [], null, ['strategy' => 'defer']); wp_add_inline_script('sage/app.js', asset('scripts/manifest.js')->contents(), 'before'); - if (is_single() && comments_open() && get_option('thread_comments')) { wp_enqueue_script('comment-reply'); } diff --git a/web/app/themes/gds/config/assets.php b/web/app/themes/gds/config/assets.php index 57cdb62d..31041f18 100644 --- a/web/app/themes/gds/config/assets.php +++ b/web/app/themes/gds/config/assets.php @@ -40,36 +40,4 @@ // 'bundles' => get_theme_file_path('public/entrypoints.json'), ] ], - - 'deferred_scripts' => [ - 'sage/vendor.js', - 'sage/app.js', - 'sage/editor.js', - 'comment-reply', - 'wp-embed', - 'debug-bar-js', - 'admin-bar', - 'debug-bar', - 'wp-genero-cookieconsent/js', - 'moxiejs', // gravityforms - 'plupload', // gravityforms - ], - - 'async_scripts' => [ - 'sage/fontawesome.js', - 'sage/gds.js', - ], - - 'async_styles' => [ - // 'sage/fonts.css', - 'dashicons', - 'debug-bar', - 'ptam-style-css', - 'ptam-style-css-editor', - 'shc-show-env', - 'wp-block-library-theme', - 'wp-smart-crop-renderer', - 'wp-genero-cookieconsent/css/library', - 'wp-genero-cookieconsent/css', - ], ];