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.
Pros:
Cons:
<style> tags into the <head>.@stack for CSS, ensure @googlefonts is placed in the correct stack (e.g., @stack('styles')).storage/app/public/google-fonts (configurable). Requires writable storage permissions.php artisan google-fonts:clear command or add a last_updated check to the config..htaccess restrictions).filesystem.disks config to restrict access.@googlefonts interfere with existing @stack usage?<style> tags without bundling..woff2 files).storage/app/public/google-fonts.config/google-fonts.php with desired fonts.'fonts' => [
'default' => 'https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap',
'monospace' => 'https://fonts.googleapis.com/css2?family=Fira+Code&display=swap',
],
@googlefonts directives:
@googlefonts('default') <!-- Default font -->
@googlefonts('monospace', ['class' => 'font-mono']) <!-- Named font with classes -->
php artisan serve).php artisan google-fonts:clear to pre-fetch fonts in staging/production.@googlefonts directive.use Spatie\GoogleFonts\Facades\GoogleFonts;
echo GoogleFonts::render('default');
font-family: 'Inter', sans-serif; (ensure CSS is loaded after @googlefonts).GoogleFontsServiceProvider.config/google-fonts.php; use Laravel’s config caching (php artisan config:cache) in production.storage/app/public/google-fonts. Implement a cleanup script for unused fonts (e.g., via php artisan storage:link --remove).du -sh storage/app/public/google-fonts).php artisan google-fonts:clear) or config cache.font-display: swap in the generated CSS to avoid FOIT.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Google CSS structure changes | Fonts fail to load | Monitor GitHub issues; implement fallback CDN. |
How can I help you explore Laravel packages today?