Installation:
composer require dotninth/laravel-tachyon
Publish the config file:
php artisan vendor:publish --provider="Dotninth\Tachyon\TachyonServiceProvider"
First Use Case:
Enable Tachyon globally in config/tachyon.php:
'enabled' => env('TACHYON_ENABLED', true),
Add to your .env:
TACHYON_ENABLED=true
Verify Integration:
Run a test request and inspect the response headers for X-Tachyon-Optimized: true.
config/tachyon.php (optimization rules, exclusions, and middleware settings).app/Http/Middleware/TachyonMiddleware.php (customize logic if needed).@tachyon and @tachyonoff for granular control in views.Global Optimization:
Enable middleware in app/Http/Kernel.php:
protected $middleware = [
\Dotninth\Tachyon\Http\Middleware\TachyonMiddleware::class,
];
Conditional Optimization: Use Blade directives to exclude specific sections:
@tachyonoff
<!-- Unoptimized content (e.g., dynamic ads, user-generated HTML) -->
@endtachyonoff
Dynamic Rules:
Configure rules in config/tachyon.php:
'rules' => [
'remove_comments' => true,
'collapse_whitespace' => true,
'shorten_doctype' => true,
'remove_processing_instructions' => true,
'remove_cdata_sections' => true,
'remove_xml_prolog' => true,
],
Livewire Integration: Exclude Livewire components from optimization by wrapping them:
@tachyonoff
<livewire:component />
@endtachyonoff
Or configure Livewire-specific exclusions in config/tachyon.php:
'excluded_routes' => [
'livewire/*',
],
API vs. Web Routes:
Disable Tachyon for API routes in config/tachyon.php:
'excluded_routes' => [
'api/*',
],
php artisan tachyon:test to validate optimizations on a specific route.'debug' => env('TACHYON_DEBUG', false),
'custom_rules' => [
\App\Rules\CustomHtmlRule::class,
],
Over-Optimization:
Livewire/Alpine Conflicts:
@tachyonoff or exclude their routes.Caching Headaches:
php artisan cache:clear or configure cache tags for granular control.Middleware Order:
TachyonMiddleware is placed correctly in app/Http/Kernel.php:
// Correct order (example):
\App\Http\Middleware\TrimStrings::class,
\Dotninth\Tachyon\Http\Middleware\TachyonMiddleware::class,
\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class,
Enable Debug Logs:
Set 'debug' => true in config/tachyon.php and check storage/logs/laravel.log for optimization steps.
Compare Outputs:
Use browser dev tools to compare optimized vs. unoptimized HTML (disable Tachyon temporarily with @tachyonoff around suspect sections).
Route Exclusions:
If a route isn’t optimizing, verify it’s not in excluded_routes or blocked by middleware priority.
Custom Rules:
Implement Dotninth\Tachyon\Contracts\HtmlRule to add bespoke optimizations:
namespace App\Rules;
use Dotninth\Tachyon\Contracts\HtmlRule;
class CustomHtmlRule implements HtmlRule {
public function process(string $html): string {
// Custom logic (e.g., replace placeholders)
return str_replace('{{placeholder}}', 'value', $html);
}
}
Register in config/tachyon.php:
'custom_rules' => [
\App\Rules\CustomHtmlRule::class,
],
Event Listeners:
Listen for tachyon.optimized and tachyon.failed events to log or modify behavior:
// In EventServiceProvider
protected $listen = [
'Dotninth\Tachyon\Events\HtmlOptimized' => [
\App\Listeners\LogOptimization::class,
],
];
Conditional Middleware: Dynamically enable/disable Tachyon based on request attributes (e.g., user role):
// In TachyonMiddleware
public function handle($request, Closure $next) {
if (auth()->check() && auth()->user()->is_admin) {
return $next($request);
}
// Proceed with optimization
}
Environment Variables:
Override settings via .env:
TACHYON_RULE_REMOVE_COMMENTS=false
TACHYON_DEBUG=true
Performance Impact:
php artisan tachyon:benchmark.Blade Directives:
@tachyon and @tachyonoff must be at the root level of a Blade file (not nested in other directives like @if).How can I help you explore Laravel packages today?