laravel / framework

The Laravel Framework.
https://laravel.com
MIT License
32.21k stars 10.89k forks source link

Issue with using the new defer helper #52861

Closed MohamedElzoghby99 closed 5 hours ago

MohamedElzoghby99 commented 6 hours ago

Laravel Version

^11.9

PHP Version

^8.2

Database Driver & Version

MySQL 8.0.39 for Linux Debian 12

Description

i installed a fresh laravel application i wanted to test the new helper defer

Route::get('/', function () {
    defer(function () {
        sleep(5);
        dump("Running defered code");
    });

    return response('success', 200);
});

for something reason when i visit: http://localhost:8000, it does show the response but the browser is still loading until the code inside defer has executed. Screenshot from 2024-09-20 00-24-14

Steps To Reproduce

just created a new laravel application

added this code in routes.php file:

Route::get('/', function () {
    defer(function () {
        sleep(5);
        dump("Running defered code");
    });

    return response('success', 200);
});

then visited localhost:8000

timacdonald commented 5 hours ago

It seems like your web server does not support the defer functionality.

The web server will need to be using PHP-FPM or Lightspeed.

If you are doing this via php artisan server, for example, the web server cannot hang up on the request until it has finished executing.