Manage self-hosted Google Fonts in Laravel apps

Overview

PACKAGE IN DEVELOPMENT, DO NOT USE YET

Manage self-hosted Google Fonts in Laravel apps

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package makes self-hosting Google Fonts as frictionless as possible for Laravel users. To load fonts in your application, register a Google Fonts embed URL and load it with the @googlefonts Blade directive.

// config/google-fonts.php

return [
    'fonts' => [
        'inter' => 'https://fonts.googleapis.com/css2?family=Inter:ital,wght@0,400;0,700;1,400;1,700',
    ],
];
{{-- resources/views/layouts/app.blade.php --}}

<head>
    @googlefonts('inter')
head>

When fonts are requested the first time, this package will scrape the CSS, fetch the assets from Google's servers, store them locally, and render the CSS inline.

If anything goes wrong in this process, the package falls back to a tag to load the fonts from Google.

This command will find usages of @googlefonts, and cache the CSS and fonts so they're ready for use. You can commit these files in Git so the app never has to hit Google servers again.

Why we created 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.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/laravel-google-fonts

You may optionally publish the config file:

php artisan vendor:publish --provider="Spatie\GoogleFonts\GoogleFontsServiceProvider" --tag="google-fonts-config"

Here's what the config file looks like:

return [

    /*
     * Here you can register fonts to call from the @googlefonts Blade directive.
     * The google-fonts:prefetch command will prefetch these fonts.
     */
    'fonts' => [
        // 'sans' => 'https://fonts.googleapis.com/css2?family=Inter:ital,wght@0,400;0,700;1,400;1,700',
    ],

    /*
     * Thid disk that will be used to store local Google Fonts. The public disk
     * is the default because it can be served over HTTP with storage:link.
     */
    'disk' => 'public',

    /*
     * Prepend all files that are written to the selected disk with this path.
     * This allows separating the fonts from other data in the public disk.
     */
    'path' => 'fonts',

    /*
     * By default, CSS will be inlined to reduce the amount of round trips
     * browsers need to make in order to load the requested font files.
     */
    'inline' => true,

    /*
     * When something goes wrong fonts are loaded directly from Google.
     * With fallback disabled, this package will throw an exception.
     */
    'fallback' => true,

    /*
     * This user agent will be used to request the stylesheet from Google Fonts.
     * This is the Safari 14 user agent that only targets modern browsers. If
     * you want to target older browsers, use different user agent string.
     */
    'user_agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Safari/605.1.15',

];

Usage

To add fonts to your application, grab an embed code from Google fonts, register it in the config and use the @googlefonts Blade directive.

// config/google-fonts.php

return [
    'fonts' => [
        'inter' => 'https://fonts.googleapis.com/css2?family=Inter:ital,wght@0,400;0,700;1,400;1,700',
    ],
];
{{-- resources/views/layouts/app.blade.php --}}

<head>
    @googlefonts('inter')
head>

This will inline the CSS, so the browser needs to do one less round-trip. If you prefer an external CSS file, you may disable the inline option in the package configuration.

Fonts are stored in a fonts folder on the public disk. You'll need to run php artisan storage:link to ensure the files can be served over HTTP. If you wish to store fonts in the git repository, make sure storage/app/public is not ignored.

If you want to serve fonts from a CDN, you may set up a different disk configuration.

Prefetching fonts

If you want to make sure fonts are ready to go before anyone visits your site, you can prefetch them with this artisan command.

php artisan google-fonts:prefetch

Caveats for legacy browsers

Google Fonts' servers sniff the visitor's user agent header to determine which font format to serve. This means fonts work in all modern and legacy browsers.

This package isn't able to tailor to different user agents. With the default configuration, only browsers that can handle WOFF 2.0 font files are supported. At the time of writing, this is >95% of all users according to caniuse. Most notably, IE doesn't support WOFF 2.0.

If you need to serve fonts to a legacy browser, you may specify a different user agent string in the configuration. Keep in mind that makes the page load heavier for all visitors, including modern browsers.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

You might also like...
Perform Self-Diagnosis Tests On Your Laravel Application
Perform Self-Diagnosis Tests On Your Laravel Application

Perform Self-Diagnosis Tests On Your Laravel Application This package allows you to run self-diagnosis tests on your Laravel application. It comes wit

Custom Blade components to add sortable/drag-and-drop HTML elements in your apps.
Custom Blade components to add sortable/drag-and-drop HTML elements in your apps.

Laravel Blade Sortable Demo Repo Installation You can install the package via composer: composer require asantibanez/laravel-blade-sortable After the

Live Helper Chat - live support for your website. Featuring web and mobile apps, Voice & Video & ScreenShare. Supports Telegram, Twilio (whatsapp), Facebook messenger including building a bot.

Live helper chat It's an open-source powered application, which brings simplicity and usability in one place. With live helper chat you can bring live

Laravel-tagmanager - An easier way to add Google Tag Manager to your Laravel application.

Laravel TagManager An easier way to add Google Tag Manager to your Laravel application. Including recommended GTM events support. Requirements Laravel

Laravel Podcast is Laravel 5.5 web app that enables you to manage RSS feeds for your favorite podcasts and listen to the episodes in a seamless UI and User Authentication.
Laravel Podcast is Laravel 5.5 web app that enables you to manage RSS feeds for your favorite podcasts and listen to the episodes in a seamless UI and User Authentication.

Laravel Podcast is Laravel 5.5 web app that enables you to manage RSS feeds for your favorite podcasts and listen to the episodes in a seamless UI and

Packagit is amazing laravel modules management, you could manage huge project with many separate laravel modules.

Packagit is amazing laravel modules management, you could manage huge project with many separate laravel modules.

Laravel-OvalFi helps you Set up, test, and manage your OvalFi integration directly in your Laravel App.
Laravel-OvalFi helps you Set up, test, and manage your OvalFi integration directly in your Laravel App.

OvalFi Laravel Package Laravel-OvalFi helps you Set up, test, and manage your OvalFi integration directly in your Laravel App. Installation You can in

Google Cloud Storage filesystem driver for Laravel
Google Cloud Storage filesystem driver for Laravel

Google Cloud Storage filesystem driver for Laravel Google Cloud Storage filesystem driver for Laravel. This started as a fork from Superbalist/laravel

Laravel real-time CRUD using Google Firebase.
Laravel real-time CRUD using Google Firebase.

Laravel real-time CRUD using Google Firebase.

Comments
  • Add method to access url

    Add method to access url

    This PR adds a method url() which enables you to access the url of the font stored locally (or else the fallback). Sometimes this is needed, for example to use this package with the Filament admin panel: https://filamentphp.com/docs/2.x/admin/appearance#including-frontend-assets.

    Filament::registerStyles([
        app(GoogleFonts::class)->load()->url(),
    ]);
    
    opened by zepfietje 3
  • feat: add blocking option

    feat: add blocking option

    This PR implements the idea suggested at https://github.com/spatie/laravel-google-fonts/discussions/22

    The blocking option determines whether the rendering of the page should be blocked while loading the font or whether the font can also be applied after the page has loaded.

    opened by jaulz 2
  • Fix tests and avoid double slashes in public urls

    Fix tests and avoid double slashes in public urls

    Currently, the tests fail because this check in it_loads_google_fonts is wrong:

    $this->assertEquals($fullCssPath, $fonts->url());
    

    $fullCssPath is $this->disk()->path($expectedFileName), which contains the file system path, while $fonts->url() returns the public path for the browser. These are not the same. Instead of disk()->path(), disk()->url() should be used to compare this value.

    Moreover, I noticed that if the configured path prefix (google-fonts.path) is null (which is the case in the tests), the url returned by Fonts::url() always contains a double slash in the path. This doesn't look good, and for some web servers, it even leads to an error. Therefore, fixed the code in e31c046892379069adf83c4a477e69c1ca7efc40 to make sure the double slashes are avoided.

    opened by fjahn 1
Releases(1.2.1)
  • 1.2.1(Sep 7, 2022)

    What's Changed

    • Fix tests and avoid double slashes in public urls by @fjahn in https://github.com/spatie/laravel-google-fonts/pull/28

    New Contributors

    • @fjahn made their first contribution in https://github.com/spatie/laravel-google-fonts/pull/28

    Full Changelog: https://github.com/spatie/laravel-google-fonts/compare/1.2.0...1.2.1

    Source code(tar.gz)
    Source code(zip)
  • 1.2.0(Feb 28, 2022)

    What's Changed

    • Add method to access url by @zepfietje in https://github.com/spatie/laravel-google-fonts/pull/21

    New Contributors

    • @zepfietje made their first contribution in https://github.com/spatie/laravel-google-fonts/pull/21

    Full Changelog: https://github.com/spatie/laravel-google-fonts/compare/1.1.0...1.2.0

    Source code(tar.gz)
    Source code(zip)
  • 1.1.0(Jan 13, 2022)

  • 1.0.0(Jun 23, 2021)

  • 0.0.1(Jun 21, 2021)

Owner
Spatie
We create products and courses for the developer community
Spatie
Intranet Home Page is a highly-configurable self-hosted browser homepage with integrations for public and local data feeds.

Intranet-Home-Page Created in response to personal "dashboards" that are little more than pages with a list of frequently accessed links, Intranet Hom

null 56 Aug 27, 2022
Automatically disable Google's FLoC in Laravel apps

Automatically disable Google's FLoC in Laravel apps This package will automatically disable Google's FLoC. Support us We invest a lot of resources int

Spatie 68 Oct 21, 2022
A plugin for Blessing Skin Server that can let you display Google Ads with Google AdSense in the website.

A plugin for Blessing Skin Server that can let you display Google Ads with Google AdSense in the website.

Big_Cake 2 Jan 25, 2022
A Laravel Wrapper for the CoinDCX API. Now easily connect and consume the CoinDCX Public API in your Laravel apps without any hassle.

This package provides a Laravel Wrapper for the CoinDCX API and allows you to easily communicate with it. Important Note This package is in early deve

Moinuddin S. Khaja 2 Feb 16, 2022
Ignition: a beautiful error page for Laravel apps

Ignition is a beautiful and customizable error page for Laravel applications running on Laravel 5.5 and newer. It is the default error page for all Laravel 6 applications.

Spatie 260 Dec 29, 2022
Add Server-Timing header information from within your Laravel apps.

Laravel Server Timings Add Server-Timing header information from within your Laravel apps. Installation You can install the package via composer: comp

Beyond Code 498 Dec 15, 2022
A Docker container for Laravel web apps

Ubuntu Docker container for Laravel web applications Docker-laravel is a LEMP image for running Laravel web applications. It extends docker-base, whic

Mark Macdonald 121 Nov 18, 2022
A collection of classes to be extended/used in laravel apps for quick development

laraquick A collection of classes to be extended/used in laravel applications for quick development. Introduction The library contains traits with wel

Ezra Obiwale 35 Dec 13, 2022
Postier is a Laravel API automation platform to transfer data and to sync apps.

Postier is a Laravel API automation platform to transfer data and to sync apps. You can build workflows with data and actions of multiple apps and apply logics to the data!

null 55 Oct 28, 2022
Enhance your laravel apps with WhatsApp's Cloud API

Enhance your laravel apps with WhatsApp's Cloud API Use Whatsapp API in your Laravel app! Support us Investing on this package is defintely a good mov

Ricardo Sawir 10 Dec 1, 2022