Skip to content

Commit

Permalink
Fixes #350: Use WP 6.3 core method to async load assets
Browse files Browse the repository at this point in the history
  • Loading branch information
oxyc committed Feb 12, 2024
1 parent e36f5e6 commit 3e8f0cc
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 113 deletions.
79 changes: 0 additions & 79 deletions web/app/themes/gds/app/Providers/AsyncLoaderServiceProvider.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

/**
* @see https://make.wordpress.org/core/2023/07/14/registering-scripts-with-async-and-defer-attributes-in-wordpress-6-3/
*/
class AsyncStyleLoaderServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
add_filter('style_loader_tag', [$this, 'styleLoaderTag'], 999, 2);
}

/**
* Load styles asynchronously.
*/
public function styleLoaderTag(string $html, string $handle): string
{
if (is_admin()) {
return $html;
}

$strategy = wp_styles()->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;
}
}
27 changes: 27 additions & 0 deletions web/app/themes/gds/app/Providers/PerformanceServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -56,6 +57,32 @@ function_exists('is_checkout') && (is_checkout() || is_cart()),
}
}

public function asyncLoadScripts() : void

Check failure on line 60 in web/app/themes/gds/app/Providers/PerformanceServiceProvider.php

View workflow job for this annotation

GitHub Actions / Run tests

There must not be a space before the colon in a return type declaration
{
// 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.
*/
Expand Down
3 changes: 1 addition & 2 deletions web/app/themes/gds/app/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
32 changes: 0 additions & 32 deletions web/app/themes/gds/config/assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
];

0 comments on commit 3e8f0cc

Please sign in to comment.