0
0
Laravelframework~8 mins

Terminable middleware in Laravel - Performance & Optimization

Choose your learning style9 modes available
Performance: Terminable middleware
MEDIUM IMPACT
Terminable middleware affects the response lifecycle by running code after the response is sent, impacting server response time and user experience.
Running cleanup or logging tasks after sending the HTTP response
Laravel
<?php
namespace App\Http\Middleware;
use Closure;
class LogMiddleware {
    public function handle($request, Closure $next) {
        return $next($request);
    }
    public function terminate($request, $response) {
        // Logging happens after response sent
        \Log::info('Request logged');
    }
}
Terminable middleware runs logging after response, so user gets response faster and logging does not delay interaction.
📈 Performance GainResponse sent immediately; logging runs after response, improving INP and perceived speed.
Running cleanup or logging tasks after sending the HTTP response
Laravel
<?php
namespace App\Http\Middleware;
use Closure;
class LogMiddleware {
    public function handle($request, Closure $next) {
        $response = $next($request);
        // Logging happens here before response is sent
        \Log::info('Request logged');
        return $response;
    }
}
Logging blocks the response until finished, increasing server response time and delaying user interaction.
📉 Performance CostBlocks response for logging duration, increasing Time to Interactive (TTI) and INP.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Logging inside handle()0 (server-side)00[X] Bad - blocks response, delays user interaction
Logging inside terminate()0 (server-side)00[OK] Good - response sent immediately, no delay
Rendering Pipeline
Terminable middleware runs after the HTTP response is sent to the browser, so it does not block rendering or painting stages.
Server Response
Post-Response Processing
⚠️ BottleneckServer Response blocking due to synchronous tasks in middleware handle method
Core Web Vital Affected
INP
Terminable middleware affects the response lifecycle by running code after the response is sent, impacting server response time and user experience.
Optimization Tips
1Put long-running tasks like logging or cleanup in the terminate() method.
2Avoid blocking the response in the handle() method to improve user experience.
3Use terminable middleware to keep server response fast and interaction smooth.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of using terminable middleware in Laravel?
AIt runs tasks after the response is sent, reducing user wait time.
BIt caches the entire response to speed up future requests.
CIt compresses the response to reduce download size.
DIt delays the response until all tasks finish.
DevTools: Network
How to check: Open DevTools, go to Network tab, reload page, and check Time to First Byte (TTFB) and response time for requests.
What to look for: Long TTFB or delayed response indicates blocking middleware; fast response with background tasks after indicates terminable middleware use.