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.
config/google-fonts.php, allowing granular control over disk storage, path prefixes, inlining, preloading, and fallback behavior. Supports CSP (Content Security Policy) integration via nonces.php artisan vendor:publish). No database migrations or complex setup required.@googlefonts directive, which integrates seamlessly with existing Blade templates. Minimal code changes needed for adoption.storage:link for public disk access (default). Fonts are stored in storage/app/public/fonts/, which must be included in Git if version-controlled.user_agent but trades off performance for modern browsers).php artisan google-fonts:fetch or lazy fetching. No built-in versioning for font updates.Performance vs. Privacy Tradeoff:
google-fonts:fetch) critical?Legacy Browser Requirements:
user_agent configuration thoroughly.Storage and Deployment:
public disk suitable, or do you need a CDN (e.g., S3, Cloudflare R2)?Maintenance Overhead:
google-fonts:fetch or rely on lazy fetching?CSP and Security:
spatie/laravel-csp), ensure nonces are properly integrated to avoid inline script/style violations.Scaling:
@googlefonts(['nonce' => csp_nonce()]), making it compatible with strict CSP policies.composer require spatie/laravel-google-fonts.php artisan vendor:publish --provider="Spatie\GoogleFonts\GoogleFontsServiceProvider" --tag="google-fonts-config".<link> tags with @googlefonts directives in Blade templates.fallback=false in config to ensure no runtime errors (fallback to Google URLs is default in production).user_agent).php artisan storage:link to ensure font files are publicly accessible.php artisan google-fonts:fetch.@googlefonts is unlikely to clash with custom directives).public disk but configurable.storage/app/public is writable and linked to public/storage (or adjust storage:link path).config/google-fonts.php (copy embed URLs from Google Fonts).disk, path, inline, preload, and fallback settings.<link> tags with @googlefonts directives.<!-- Before -->
<link href="https://fonts.googleapis.com/css2?family=Roboto" rel="stylesheet">
<!-- After -->
@googlefonts('roboto')
fallback=false to catch integration issues.preload for critical fonts.user_agent for legacy browser support if needed.php artisan google-fonts:fetch to update fonts (e.g., via cron or CI/CD).lazy mode (v1.6.0+) to auto-fetch only when accessed (reduces maintenance but may delay first-load performance).git add and commit.config/google-fonts.php for changes (e.g., new font families, weights).php artisan config:cache) if enabled.storage/app/public/fonts/).storage/logs/laravel.log).user_agent strings if WOFF2 fails.du -sh storage/app/public/fonts.'disk' => 's3',
Ensure CDN caching is enabled for font files.preload: true to prioritize font loading (reduces render-blocking).inline: true reduces HTTP requestsHow can I help you explore Laravel packages today?