filament/spatie-laravel-google-fonts-plugin
Adds a Spatie Google Fonts provider to Filament panels, letting you use locally fetched and cached Google Fonts via spatie/laravel-google-fonts. Configure with Panel::font('Inter', provider: SpatieGoogleFontProvider::class) to avoid CDNs.
Install Dependencies Run:
composer require spatie/laravel-google-fonts:"^4.0" filament/spatie-laravel-google-fonts-plugin:"^5.0" -W
Follow Spatie’s setup guide to configure the base package (e.g., .env settings, caching).
Register the Plugin
In your Panel configuration (e.g., app/Providers/Filament/AdminPanelProvider.php), add:
use Filament\FontProviders\SpatieGoogleFontProvider;
public function panel(Panel $panel): Panel {
return $panel
->font('Inter', provider: SpatieGoogleFontProvider::class);
}
Replace 'Inter' with your desired Google Font (must be pre-configured in Spatie’s package).
Verify Setup Clear caches and restart your dev server:
php artisan optimize:clear
php artisan cache:clear
Check the Filament panel—your font should now render locally (no CDN calls).
Replace CDN-based fonts with self-hosted alternatives in Filament’s admin panel.
Example: Replace default Roboto with Inter (or Poppins, Roboto Mono, etc.) for a modern look without external requests.
Font Selection & Registration
Dynamic Font Switching: Use a config file or database to toggle fonts per panel/tenant:
// app/Config/filament.php
'fonts' => [
'default' => 'Inter',
'fallback' => 'Roboto',
],
Then reference in panel():
->font(config('filament.fonts.default'), provider: SpatieGoogleFontProvider::class)
Multi-Panel Support: Extend SpatieGoogleFontProvider to support panel-specific font sets:
class CustomFontProvider extends SpatieGoogleFontProvider {
public function getFont(): string {
return request()->routeIs('admin*') ? 'Inter' : 'Lato';
}
}
Integration with Filament Themes
->theme(Theme::make('dark')
->font('Inter', provider: SpatieGoogleFontProvider::class)
)
Asset Optimization
app/Providers/AppServiceProvider.php:
public function boot() {
if (app()->environment('production')) {
FontManager::preload('Inter');
}
}
php artisan google-fonts:clear-cache
Custom Font Families Extend Spatie’s config to include custom subsets/variants:
// config/google-fonts.php
'fonts' => [
'Inter' => [
'weights' => ['100', '400', '700'],
'styles' => ['italic'],
],
],
Then use in Filament:
->font('Inter', weights: ['400', '700'], styles: ['italic'])
Fallback Chains Define fallback fonts in Spatie’s config and pass them to Filament:
// config/google-fonts.php
'fallbacks' => [
'Inter' => ['Roboto', 'Arial'],
],
Filament will automatically handle the chain via the provider.
Dynamic Font Loading Lazy-load fonts for non-critical pages using Filament’s resource hooks:
use Filament\Resources\Resource;
public function getPages(): array {
return [
Resource::make(Page::class)
->pages([
ListPages::make()
->font('Inter', provider: SpatieGoogleFontProvider::class),
]),
];
}
Font Not Loading?
php artisan google-fonts:download to ensure files exist in storage/app/google-fonts.php artisan view:clear.Mixed CDN/Local Sources
@googlefonts directive isn’t processed.AppServiceProvider:
public function boot() {
$this->loadGoogleFonts();
}
Performance Overhead
Roboto with 10+ weights) bloat asset size.->font('Inter', weights: ['400', '500'], styles: [])
Panel-Specific Conflicts
filament/admin vs. filament/tenant).Check Downloaded Fonts
Inspect storage/app/google-fonts for missing files. Run:
php artisan google-fonts:download --font=Inter
Verify Blade Directives
Ensure @googlefonts directives render in Filament’s views. Add this to a test blade file:
@googlefonts(['Inter'])
If missing, Spatie’s package isn’t booting early enough.
Log Provider Calls
Temporarily add logging to SpatieGoogleFontProvider to debug font resolution:
public function getFontUrl(): string {
\Log::debug('Resolving font URL for:', $this->font);
return parent::getFontUrl();
}
Custom Provider Logic
Extend SpatieGoogleFontProvider to add logic like:
class UserFontProvider extends SpatieGoogleFontProvider {
public function getFont(): string {
return auth()->user()->preferred_font ?? 'Inter';
}
}
Hybrid CDN/Local Fallback Modify the provider to fall back to CDN if local files are missing:
public function getFontUrl(): string {
$localUrl = parent::getFontUrl();
return file_exists(public_path($localUrl)) ? $localUrl : "https://fonts.googleapis.com/...";
}
Font Analytics Track font usage with a middleware or observer:
// app/Providers/FontObserverServiceProvider.php
public function boot() {
FontManager::resolving(function ($font) {
\Log::info("Font resolved: {$font->getFontName()}");
});
}
Caching Behavior
.env:
GOOGLE_FONTS_CACHE_TTL=minutes
Font Name Validation
Storage Permissions
Ensure storage/app/google-fonts is writable:
chmod -R 755 storage/app/google-fonts
How can I help you explore Laravel packages today?