-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #350: Use WP 6.3 core method to async load assets
- Loading branch information
Showing
5 changed files
with
76 additions
and
113 deletions.
There are no files selected for viewing
79 changes: 0 additions & 79 deletions
79
web/app/themes/gds/app/Providers/AsyncLoaderServiceProvider.php
This file was deleted.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
web/app/themes/gds/app/Providers/AsyncStyleLoaderServiceProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters