Last active
May 5, 2024 08:24
-
-
Save joshuafredrickson/aa416f7cb4e02cbec8c5a1e554e3dfee to your computer and use it in GitHub Desktop.
Preload font files found in Roots Sage 10 (bud.js) manifest.json
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
<?php | |
use Illuminate\Support\Str; | |
/** | |
* Add `rel="preload"` to font files found in the manifest | |
*/ | |
add_filter('wp_head', function (): void { | |
echo collect( | |
json_decode(asset('manifest.json')->contents()) | |
)->keys()->filter(function ($item) { | |
return Str::endsWith($item, ['.otf', '.eot', '.woff', '.woff2', '.ttf']); | |
})->map(function ($item) { | |
return sprintf( | |
'<link rel="preload" href="%s" as="font" crossorigin>', | |
asset($item)->uri() | |
); | |
})->implode("\n"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by the Sage 10 Laravel Mix package: https://github.com/MWDelaney/sage-fonts-preload