Armani's avatar
Level 17

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.

0 likes
14 replies
tykus's avatar

What was your expectation, and what is actually happening instead?

Armani's avatar
Level 17

@tykus it won't return the view instantly, it waits 10 seconds then it will return the view.

tykus's avatar

@Armani are you serving the application using php artisan serve or do you use a "proper webserver"?

Armani's avatar
Level 17

@tykus I'm using Herd and I run npm run dev. it is in development stage on my local computer.

tykus's avatar

@Armani it should be working; what versions of PHP, Laravel etc. are you using?

Sinnbeck's avatar

@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

Armani's avatar
Level 17

@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.

JussiMannisto's avatar

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.

1 like
Armani's avatar
Level 17

I searched the web for the answer but It is really strange that no one talks about this.

tykus's avatar

@Armani maybe no-one else is having this issue? I have tried to reproduce without success except when using php artisan serve

jasperfernandez's avatar

I'm using Laragon with NGINX on Windows and the defer() helper function is not working but the facade works!

willvincent's avatar

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);
});
1 like
VaguelyOnline's avatar

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;
1 like
Glukinho's avatar

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;
});
1 like

Please or to participate in this conversation.