• Reading time ~ 2 min
  • 08.07.2022

Laravel Google Fonts is a package by Spatie to manage self-hosted Google Fonts in Laravel apps:

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.

Comments

No comments yet
Yurij Finiv

Yurij Finiv

Full stack

ABOUT

Professional Fullstack Developer with extensive experience in website and desktop application development. Proficient in a wide range of tools and technologies, including Bootstrap, Tailwind, HTML5, CSS3, PUG, JavaScript, Alpine.js, jQuery, PHP, MODX, and Node.js. Skilled in website development using Symfony, MODX, and Laravel. Experience: Contributed to the development and translation of MODX3 i...

About author CrazyBoy49z
WORK EXPERIENCE
Contact
Ukraine, Lutsk
+380979856297