spatie/laravel-google-fonts
Self-host Google Fonts in Laravel with minimal setup. Register Google Fonts CSS URLs, then use the @googlefonts Blade directive to inline locally cached CSS and assets. Automatically downloads on first request, with a safe fallback to Google if needed.
Installation:
composer require spatie/laravel-google-fonts
Publish the config file:
php artisan vendor:publish --provider="Spatie\GoogleFonts\GoogleFontsServiceProvider" --tag="google-fonts-config"
Configure Fonts:
Edit config/google-fonts.php and define your fonts under the fonts key:
'fonts' => [
'default' => 'https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap',
'mono' => 'https://fonts.googleapis.com/css2?family=Fira+Code&display=swap',
],
First Use Case:
Add the @googlefonts directive to your Blade layout (e.g., resources/views/layouts/app.blade.php):
<head>
@googlefonts <!-- Loads 'default' font -->
@googlefonts('mono') <!-- Loads 'mono' font -->
</head>
Verify:
Run php artisan google-fonts:download to pre-download fonts (optional but recommended for production).
Clear cache if needed:
php artisan cache:clear
php artisan view:clear
Font Registration:
config/google-fonts.php with their Google Fonts embed URLs.'default', 'code') for easy Blade references.Blade Integration:
@googlefonts for the default font.@googlefonts('font-name').<body class="font-sans">
@googlefonts('sans') <!-- Loads 'sans' font -->
<h1 class="font-mono">Code Example</h1>
@googlefonts('mono') <!-- Loads 'mono' font -->
</body>
Dynamic Font Loading:
@googlefonts($dynamicFontName)
Asset Organization:
storage/app/public/google-fonts by default.public/google-fonts for web access:
php artisan storage:link --force
Caching:
php artisan google-fonts:clear-cache
Queueing Downloads:
// In a controller or command
\Spatie\GoogleFonts\GoogleFonts::downloadFont('font-name');
php artisan vendor:publish --provider="Spatie\GoogleFonts\GoogleFontsServiceProvider" --tag="google-fonts-queue"
Conditional Loading:
@if(Request::is('dashboard*'))
@googlefonts('dashboard')
@endif
Font Stacks:
@component('google-fonts', ['fonts' => ['sans', 'mono']])
@endcomponent
Local Overrides:
config/google-fonts.php:
'storage_path' => storage_path('app/custom-fonts'),
Custom CSS Processing:
// app/Providers/GoogleFontsServiceProvider.php
GoogleFonts::macro('customCss', function ($css) {
return str_replace('original-url', 'custom-url', $css);
});
Testing:
$this->mock(\Spatie\GoogleFonts\GoogleFonts::class)
->shouldReceive('downloadFont')
->once();
Missing Fonts:
@googlefonts('undefined-font') throws an error.config/google-fonts.php or wrap in a check:
@if(config('google-fonts.fonts.custom??'))
@googlefonts('custom')
@endif
Caching Issues:
php artisan google-fonts:clear-cache
php artisan view:clear
Storage Permissions:
storage/app/public is writable:
chmod -R 755 storage/app/public
Queue Failures:
php artisan queue:work --verbose
.env:
QUEUE_CONNECTION=database
CSS Injection Risks:
config/google-fonts.php and use allowlists.Font Subsetting:
'fonts' => [
'subset' => 'https://fonts.googleapis.com/css2?family=Inter&subset=latin-ext&display=swap',
],
Log Font Downloads:
config/google-fonts.php:
'debug' => env('GOOGLE_FONTS_DEBUG', false),
storage/logs/laravel.log for download errors.Verify Downloaded Files:
storage/app/public/google-fonts for missing or corrupted files.php artisan google-fonts:download --font=font-name
Network Requests:
<head>).google-fonts/Inter.woff2).Blade Directive Errors:
@if blocks to debug:
@if(false)
@googlefonts('problematic-font')
@endif
Custom Blade Directives:
@googlefonts directive in AppServiceProvider:
Blade::directive('googlefonts', function ($expression) {
return "<?php echo \\Spatie\\GoogleFonts\\GoogleFonts::render($expression); ?>";
});
Font Processing Hooks:
// app/Providers/GoogleFontsServiceProvider.php
$this->app->bind(\Spatie\GoogleFonts\CssParser::class, function () {
return new CustomCssParser();
});
Asset Storage:
// config/google-fonts.php
'storage_handler' => \App\Services\CustomFontStorage::class,
Queue Observers:
GoogleFontDownloaded event:
// app/Providers/EventServiceProvider.php
protected $listen = [
\Spatie\GoogleFonts\Events\GoogleFontDownloaded::class => [
\App\Listeners\LogFontDownload::class,
],
];
Local Font Fallbacks:
/* In your CSS */
body {
font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
}
Environment-Specific Fonts:
'fonts' => [
'default' => env('GOOGLE_FONT_DEFAULT', 'https://fonts.googleapis.com/css2?family=Inter&display=swap'),
],
Dynamic Config Loading:
// In
How can I help you explore Laravel packages today?