What was your expectation, and what is actually happening instead?
Defer function is not working
I created a brand new project with no starter kit and put this code in route file:
<?php
use Illuminate\Support\Facades\Route;
use function Illuminate\Support\defer;
Route::get('/', function () {
defer(fn() => sleep(10));
return view('welcome');
});
but defer not working as expected.
@tykus it won't return the view instantly, it waits 10 seconds then it will return the view.
@Armani are you serving the application using php artisan serve or do you use a "proper webserver"?
@tykus I'm using Herd and I run npm run dev. it is in development stage on my local computer.
@Armani it should be working; what versions of PHP, Laravel etc. are you using?
@Armani What url are you visiting the site on? If its localhost:8080 you are stilling using php artisan serve and not herd. The url should be something like yoursite.test
@Sinnbeck I'm using PHP v8.3.16 and the Laravel v11.41.3. the url is project.test.
I just installed a brand new project and didn't touch anything except the route.
I tested the defer() helper as well as Concurrency::defer() on Ubuntu 20.04 / PHP 8.4, both on Nginx and artisan serve. On Nginx, both worked correctly. With artisan serve, the facade method worked but defer() did what @armani described. I don't know where the difference comes from.
I searched the web for the answer but It is really strange that no one talks about this.
@Armani maybe no-one else is having this issue? I have tried to reproduce without success except when using php artisan serve
I'm using Laragon with NGINX on Windows and the defer() helper function is not working but the facade works!
Add the \Illuminate\Foundation\Http\Middleware\InvokeDeferredCallbacks::class middleware, either to specific routes/middleware groups, or as global middleware
In bootstrap/app.php:
use Illuminate\Foundation\Http\Middleware\InvokeDeferredCallbacks;
return Application::configure(basePath: dirname(__DIR__))
->withMiddleware(function (Middleware $middleware) {
// global
$middleware->use([InvokeDeferredCallbacks::class]);
// or a route group
$middleware->appendToGroup('group-name', [
InvokeDeferredCallbacks::class,
]);
})
or on a specific route/route group in your routes file:
use Illuminate\Foundation\Http\Middleware\InvokeDeferredCallbacks;
Route::middleware([InvokeDeferredCallbacks::class])->group(function () {
Route::get('foo', FooController::class);
});
I'm still seeing this. Running Laravel 12.28.1, on Windows 11 through Herd (Nginx). Has this been addressed? Is it just not supported on Windows environments?
The following defer function is never called:
Concurrency::defer(function () {
sleep(5);
Log::info('deferred');
});
Log::info('middle');
return 123;
The following code is called, but the browser is sat spinning the 5 seconds of sleep time:
defer(function () {
sleep(5);
Log::info('deferred');
});
Log::info('middle');
return 123;
I don't have problems with defer(). Windows 10, PHP 8.3.10, Laravel 12.25, php artisan serve.
"123" is returned immediately (yes, browser shows loading all 5 seconds, I thought it is normal on Windows; I didn't check but I think this is due to lack of pcntl php extension on Wondows), log records appear in right order and in right time:
[2025-09-09 16:41:53] local.INFO: middle
[2025-09-09 16:41:58] local.INFO: deferred
Code:
Route::get('/defer', function () {
defer(function () {
sleep(5);
Log::info('deferred');
});
Log::info('middle');
return 123;
});
Please or to participate in this conversation.