spatie/laravel-google-fonts
Self-host Google Fonts in Laravel with minimal setup. Register Google Fonts CSS URLs in config and load them via the @googlefonts Blade directive. On first request it downloads CSS/assets, caches locally, inlines CSS, and falls back to Google if needed.
## Getting Started
1. **Installation**:
```bash
composer require spatie/laravel-google-fonts
php artisan vendor:publish --provider="Spatie\GoogleFonts\GoogleFontsServiceProvider" --tag="google-fonts-config"
config/google-fonts.php).Register Fonts:
Add Google Fonts embed URLs to the fonts array in the config:
'fonts' => [
'default' => 'https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap',
'secondary' => 'https://fonts.googleapis.com/css2?family=Roboto:wght@300;400&display=swap',
],
First Use Case:
Load fonts in Blade views using the @googlefonts directive:
<head>
@googlefonts <!-- Loads 'default' font -->
@googlefonts('secondary') <!-- Loads 'secondary' font -->
</head>
Prefetch Fonts (Optional): Run the Artisan command to pre-download fonts for immediate availability:
php artisan google-fonts:fetch
config/google-fonts.php for consistency.@googlefonts('code')) to avoid hardcoding URLs.fallback to false in debug mode to enforce local hosting (throws exceptions if fonts fail to load).inline: true (default) to reduce HTTP requests.preload: true to prioritize font loading via <link rel="preload">.--lazy flag with google-fonts:fetch to skip already downloaded fonts:
php artisan google-fonts:fetch --lazy
nonce to the directive for Content Security Policy compliance:
@googlefonts(['nonce' => csp_nonce()])
storage/app/public/fonts (default). Run php artisan storage:link to make them publicly accessible.disk: 's3') in the config to offload fonts to a CDN.storage/app/public/fonts from .gitignore if fonts should be version-controlled.user_agent in config to target older browsers (e.g., IE) if needed:
'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
Storage Link Requirement:
php artisan storage:link will break font loading (404 errors).php artisan storage:link to your deployment script.Debug Mode Fallback:
APP_DEBUG=true), the package falls back to Google’s CDN if local fonts fail to load.'fallback' => false in debug mode to catch issues early:
'fallback' => env('APP_DEBUG') ? false : true,
Font URL Parsing:
.woff2 extensions (e.g., family=Roboto:wght@400) may cause parsing errors.&display=swap is required).Preload Tag Issues:
Caching Headers:
Cache-Control headers if stored in public disk.storage/app/public with proper caching rules.storage/app/public/fonts after running google-fonts:fetch.
ls storage/app/public/fonts
@googlefonts(['debug' => true]) (if supported) or check rendered HTML for errors.Spatie\GoogleFonts exceptions.Custom Fetch Logic:
Override the Spatie\GoogleFonts\Fetchers\FontFetcher class to implement custom download logic (e.g., proxy requests).
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->bind(
\Spatie\GoogleFonts\Fetchers\FontFetcher::class,
\App\CustomFontFetcher::class
);
}
Dynamic Font Registration:
Register fonts programmatically via the GoogleFonts facade:
use Spatie\GoogleFonts\Facades\GoogleFonts;
GoogleFonts::addFont('dynamic-font', 'https://fonts.googleapis.com/...');
Event Listeners: Listen for font fetch events to log or process downloads:
// app/Providers/EventServiceProvider.php
protected $listen = [
\Spatie\GoogleFonts\Events\FontsFetched::class => [
\App\Listeners\LogFontDownload::class,
],
];
--lazy with google-fonts:fetch in CI/CD to avoid redundant downloads.google-fonts:fetch periodically to update fonts if Google releases new versions.fallback in .env for local testing:
GOOGLE_FONTS_FALLBACK=false
---
How can I help you explore Laravel packages today?