Laravel Google Fonts Package
Published on by Paul Redmond
Laravel Google Fonts is a package by Spatie to manage self-hosted Google Fonts in Laravel apps:
— Freek Van der Herten 🔭 (@freekmurze) June 21, 2021
This package works by registering the font embed URL you’d typically get from Google. This package will scrape the CSS, fetch assets, store them locally, and render the CSS inline during the first request. Inline rendering reduces a trip to the server, but if anything goes wrong during this process, this package will fall back to the <link>
tag to fetch the CSS and assets directly from Google’s CDN.
Apart from the performance benefit, keeping your self-hosted fonts and CSS can be a chore, and this package takes care of that for you!
You can even fetch the assets and CSS ahead of time, perhaps during a new deployment with the provided artisan command:
php artisan google-fonts:prefetch
Awesome, but why would I want to do that?
The package’s readme has an explanation of why you might want to use this package:
Google Fonts hosts an impressive catalog of fonts, but relying on it has its costs. By hosting fonts on an external domain, browsers need to perform an additional DNS lookup. This slows down the initial page load. In addition, you’re directing your visitors to Google property, which privacy-minded users might not appreciate.
You can download fonts from Google Fonts and self-host them, but it’s more work than embedding a code. Keeping up with the latest font version can also be a chore.
This package makes self-hosting Google Fonts as frictionless as possible for Laravel users.
Learn More
You can learn more about this package, get full installation instructions, and view the source code on GitHub. Thanks again to Freek Van der Herten and the rest of the Spatie crew for yet another helpful package! If you find it useful, consider supporting Spatie, who maintains many valuable packages in the Laravel and PHP ecosystem.