Skip to content

Instantly share code, notes, and snippets.

@joshuafredrickson
Last active May 5, 2024 08:24
Show Gist options
  • Save joshuafredrickson/aa416f7cb4e02cbec8c5a1e554e3dfee to your computer and use it in GitHub Desktop.
Save joshuafredrickson/aa416f7cb4e02cbec8c5a1e554e3dfee to your computer and use it in GitHub Desktop.
Preload font files found in Roots Sage 10 (bud.js) manifest.json
<?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");
});
@joshuafredrickson
Copy link
Author

Inspired by the Sage 10 Laravel Mix package: https://github.com/MWDelaney/sage-fonts-preload

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment