mirror of
https://github.com/filamentphp/filament.git
synced 2026-09-24 15:42:09 +08:00
Merge pull request #13634 from dissto/feature/optimize-command
Proposal: Add `filament:optimize` and `filament:optimize-clear` commands
This commit is contained in:
@@ -42,18 +42,34 @@ The Filament Panel Builder pre-installs the [Form Builder](/docs/forms), [Table
|
||||
|
||||
## Improving Filament panel performance
|
||||
|
||||
### Caching Blade Icons
|
||||
### Optimizing Filament for production
|
||||
|
||||
You may wish to consider running `php artisan icons:cache` locally, and also in your deployment script. This is because Filament uses the [Blade Icons](https://blade-ui-kit.com/blade-icons) package, which can be much more performant when cached.
|
||||
To optimize Filament for production, you should run the following command in your deployment script:
|
||||
|
||||
### Caching Filament components
|
||||
```bash
|
||||
php artisan filament:optimize
|
||||
```
|
||||
|
||||
You may also wish to consider running `php artisan filament:cache-components` in your deployment script, especially if you have large numbers of components (resources, pages, widgets, relation managers, custom Livewire components, etc.). This will create cache files in the `bootstrap/cache/filament` directory of your application, which contain indexes for each type of component. This can significantly improve the performance of Filament in some apps, as it reduces the number of files that need to be scanned and auto-discovered for components.
|
||||
This command will [cache the Filament components](#caching-filament-components) and additionally the [Blade icons](#caching-blade-icons), which can significantly improve the performance of your Filament panels. This command is a shorthand for the commands `php artisan filament:cache-components` and `php artisan icons:cache`.
|
||||
|
||||
To clear the caches at once, you can run:
|
||||
|
||||
```bash
|
||||
php artisan filament:optimize-clear
|
||||
```
|
||||
|
||||
#### Caching Filament components
|
||||
|
||||
If you're not using the [`filament:optimize` command](#optimizing-filament-for-production), you may wish to consider running `php artisan filament:cache-components` in your deployment script, especially if you have large numbers of components (resources, pages, widgets, relation managers, custom Livewire components, etc.). This will create cache files in the `bootstrap/cache/filament` directory of your application, which contain indexes for each type of component. This can significantly improve the performance of Filament in some apps, as it reduces the number of files that need to be scanned and auto-discovered for components.
|
||||
|
||||
However, if you are actively developing your app locally, you should avoid using this command, as it will prevent any new components from being discovered until the cache is cleared or rebuilt.
|
||||
|
||||
You can clear the cache at any time without rebuilding it by running `php artisan filament:clear-cached-components`.
|
||||
|
||||
#### Caching Blade Icons
|
||||
|
||||
If you're not using the [`filament:optimize` command](#optimizing-filament-for-production), you may wish to consider running `php artisan icons:cache` locally, and also in your deployment script. This is because Filament uses the [Blade Icons](https://blade-ui-kit.com/blade-icons) package, which can be much more performant when cached.
|
||||
|
||||
### Enabling OPcache on your server
|
||||
|
||||
From the [Laravel Forge documentation](https://forge.laravel.com/docs/servers/php.html#opcache):
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Filament\Support\Commands\Concerns;
|
||||
|
||||
use Filament\Commands\CacheComponentsCommand;
|
||||
|
||||
trait CanCachePanelComponents
|
||||
{
|
||||
protected function canCachePanelComponents(): bool
|
||||
{
|
||||
return class_exists(CacheComponentsCommand::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace Filament\Support\Commands;
|
||||
|
||||
use Closure;
|
||||
use Filament\Support\Commands\Concerns\CanCachePanelComponents;
|
||||
use Illuminate\Console\Command;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
|
||||
#[AsCommand(name: 'filament:optimize-clear')]
|
||||
class OptimizeClearCommand extends Command
|
||||
{
|
||||
use CanCachePanelComponents;
|
||||
|
||||
protected $signature = 'filament:optimize-clear';
|
||||
|
||||
protected $description = 'Remove cached components and Blade icons.';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$this->components->info('Clearing cached components and Blade icons.');
|
||||
|
||||
$tasks = collect();
|
||||
|
||||
if ($this->canCachePanelComponents()) {
|
||||
$tasks->put(
|
||||
'Clearing cached components',
|
||||
fn (): bool => $this->callSilent('filament:clear-cached-components') === static::SUCCESS
|
||||
);
|
||||
}
|
||||
|
||||
$tasks->put(
|
||||
'Clearing cached Blade icons',
|
||||
fn (): bool => $this->callSilent('icons:clear') === static::SUCCESS
|
||||
);
|
||||
|
||||
$tasks->each(fn (Closure $task, string $description) => $this->components->task($description, $task));
|
||||
|
||||
$this->newLine();
|
||||
|
||||
return static::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
namespace Filament\Support\Commands;
|
||||
|
||||
use Closure;
|
||||
use Filament\Support\Commands\Concerns\CanCachePanelComponents;
|
||||
use Illuminate\Console\Command;
|
||||
use Symfony\Component\Console\Attribute\AsCommand;
|
||||
|
||||
#[AsCommand(name: 'filament:optimize')]
|
||||
class OptimizeCommand extends Command
|
||||
{
|
||||
use CanCachePanelComponents;
|
||||
|
||||
protected $signature = 'filament:optimize';
|
||||
|
||||
protected $description = 'Cache components and Blade icons to improve performance.';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$this->components->info('Caching components and Blade icons.');
|
||||
|
||||
$tasks = collect();
|
||||
|
||||
if ($this->canCachePanelComponents()) {
|
||||
$tasks->put(
|
||||
'Caching components',
|
||||
fn (): bool => $this->callSilent('filament:cache-components') === static::SUCCESS
|
||||
);
|
||||
}
|
||||
|
||||
$tasks->put('Caching Blade icons', fn (): bool => $this->callSilent('icons:cache') === static::SUCCESS);
|
||||
|
||||
$tasks->each(fn (Closure $task, string $description) => $this->components->task($description, $task));
|
||||
|
||||
$this->newLine();
|
||||
|
||||
return static::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,8 @@ use Filament\Support\Commands\AssetsCommand;
|
||||
use Filament\Support\Commands\CheckTranslationsCommand;
|
||||
use Filament\Support\Commands\InstallCommand;
|
||||
use Filament\Support\Commands\MakeIssueCommand;
|
||||
use Filament\Support\Commands\OptimizeClearCommand;
|
||||
use Filament\Support\Commands\OptimizeCommand;
|
||||
use Filament\Support\Commands\UpgradeCommand;
|
||||
use Filament\Support\Components\ComponentManager;
|
||||
use Filament\Support\Components\Contracts\ScopedComponentManager;
|
||||
@@ -40,10 +42,11 @@ class SupportServiceProvider extends PackageServiceProvider
|
||||
AssetsCommand::class,
|
||||
CheckTranslationsCommand::class,
|
||||
InstallCommand::class,
|
||||
UpgradeCommand::class,
|
||||
|
||||
MakeIssueCommand::class,
|
||||
MakeIssueCommandAlias::class,
|
||||
OptimizeClearCommand::class,
|
||||
OptimizeCommand::class,
|
||||
UpgradeCommand::class,
|
||||
])
|
||||
->hasConfigFile()
|
||||
->hasTranslations()
|
||||
|
||||
Reference in New Issue
Block a user