Description
Symfony version(s) affected
7.3.0
Description
Hi, I'm trying to use Livewire streams in my Laravel app, but it keep on triggering "Cannot modify header information" error such as livewire/livewire#9357
After digging deeper into the codes, I found this PR #60377 that triggered above issue.
How to reproduce
You could check out my repo below. I installed a fresh new Laravel app with just Livewire installed to make sure nothing else was affecting the test results.
- Execute below commands
git clone [email protected]:sarukomine/livewire-stream-for-report.git
cd livewire-stream-for-report
git checkout 12.x
composer install
cp .env.example .env
php artisan key:generate
php artisan migrate
php artisan serve
-
Open
http://127.0.0.1:8000/stream
page -
Click "Start count-down" button
-
Open
storage/logs/laravel.log
file, you could see the error message
laravel/laravel => v10.3.3
laravel/framework => v10.48.29
livewire/livewire => v3.6.3
symfony/http-foundation => v6.4.22
laravel/laravel => v11.6.1
laravel/framework => v11.0.0
livewire/livewire => v3.6.3
symfony/http-foundation => v7.3.0
laravel/laravel => v12.0.9
laravel/framework => v12.16.0
livewire/livewire => v3.6.3
symfony/http-foundation => v7.3.0
Possible Solution
Just add the fix before trigger header() method, stream response will be works fine.
if (!empty(preg_grep('/^Content-Type:.*\btext\/event-stream\b.*/i', headers_list()))) {
return $this;
}
like that
// headers have already been sent by the developer
if (headers_sent()) {
// Add a fix here
if (!empty(preg_grep('/^Content-Type:.*\btext\/event-stream\b.*/i', headers_list()))) {
return $this;
}
if (!\in_array(\PHP_SAPI, ['cli', 'phpdbg', 'embed'], true)) {
// Or add a fix here
if (!empty(preg_grep('/^Content-Type:.*\btext\/event-stream\b.*/i', headers_list()))) {
return $this;
}
$statusCode ??= $this->statusCode;
header(\sprintf('HTTP/%s %s %s', $this->version, $statusCode, $this->statusText), true, $statusCode);
}
return $this;
}
Or, we may delete these changes from #60377, let this function keep simple~
Additional Context
[2025-05-30 13:11:31] local.ERROR: Cannot modify header information - headers already sent by (output started at /Users/whoami/Herd/livewire-stream-11/vendor/livewire/livewire/src/Features/SupportStreaming/SupportStreaming.php:37) {"exception":"[object] (ErrorException(code: 0): Cannot modify header information - headers already sent by (output started at /Users/whoami/Herd/livewire-stream-11/vendor/livewire/livewire/src/Features/SupportStreaming/SupportStreaming.php:37) at /Users/whoami/Herd/livewire-stream-11/vendor/symfony/http-foundation/Response.php:322)
[stacktrace]
#0 /Users/whoami/Herd/livewire-stream-11/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(256): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(2, 'Cannot modify h...', '/Users/whoami...', 322)
#1 [internal function]: Illuminate\\Foundation\\Bootstrap\\HandleExceptions->Illuminate\\Foundation\\Bootstrap\\{closure}(2, 'Cannot modify h...', '/Users/whoami...', 322)
#2 /Users/whoami/Herd/livewire-stream-11/vendor/symfony/http-foundation/Response.php(322): header('HTTP/1.1 200 OK', true, 200)
#3 /Users/whoami/Herd/livewire-stream-11/vendor/symfony/http-foundation/Response.php(401): Symfony\\Component\\HttpFoundation\\Response->sendHeaders()
#4 /Users/whoami/Herd/livewire-stream-11/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(1168): Symfony\\Component\\HttpFoundation\\Response->send()
#5 /Users/whoami/Herd/livewire-stream-11/public/index.php(17): Illuminate\\Foundation\\Application->handleRequest(Object(Illuminate\\Http\\Request))
#6 /Applications/Herd.app/Contents/Resources/valet/server.php(167): require('/Users/whoami...')
#7 {main}
"}
[2025-05-30 13:11:31] local.ERROR: Uncaught ErrorException: Cannot modify header information - headers already sent by (output started at /Users/whoami/Herd/livewire-stream-11/vendor/livewire/livewire/src/Features/SupportStreaming/SupportStreaming.php:37) in /Users/whoami/Herd/livewire-stream-11/vendor/symfony/http-foundation/Response.php:322
Stack trace:
#0 /Users/whoami/Herd/livewire-stream-11/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(256): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'Cannot modify h...', '/Users/whoami...', 322)
#1 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->Illuminate\Foundation\Bootstrap\{closure}(2, 'Cannot modify h...', '/Users/whoami...', 322)
#2 /Users/whoami/Herd/livewire-stream-11/vendor/symfony/http-foundation/Response.php(322): header('HTTP/1.1 500 In...', true, 500)
#3 /Users/whoami/Herd/livewire-stream-11/vendor/symfony/http-foundation/Response.php(401): Symfony\Component\HttpFoundation\Response->sendHeaders()
#4 /Users/whoami/Herd/livewire-stream-11/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(219): Symfony\Component\HttpFoundation\Response->send()
#5 /Users/whoami/Herd/livewire-stream-11/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(196): Illuminate\Foundation\Bootstrap\HandleExceptions->renderHttpResponse(Object(ErrorException))
#6 /Users/whoami/Herd/livewire-stream-11/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(256): Illuminate\Foundation\Bootstrap\HandleExceptions->handleException(Object(ErrorException))
#7 [internal function]: Illuminate\Foundation\Bootstrap\HandleExceptions->Illuminate\Foundation\Bootstrap\{closure}(Object(ErrorException))
#8 {main}
thrown {"exception":"[object] (Symfony\\Component\\ErrorHandler\\Error\\FatalError(code: 0): Uncaught ErrorException: Cannot modify header information - headers already sent by (output started at /Users/whoami/Herd/livewire-stream-11/vendor/livewire/livewire/src/Features/SupportStreaming/SupportStreaming.php:37) in /Users/whoami/Herd/livewire-stream-11/vendor/symfony/http-foundation/Response.php:322
Stack trace:
#0 /Users/whoami/Herd/livewire-stream-11/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(256): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError(2, 'Cannot modify h...', '/Users/whoami...', 322)
#1 [internal function]: Illuminate\\Foundation\\Bootstrap\\HandleExceptions->Illuminate\\Foundation\\Bootstrap\\{closure}(2, 'Cannot modify h...', '/Users/whoami...', 322)
#2 /Users/whoami/Herd/livewire-stream-11/vendor/symfony/http-foundation/Response.php(322): header('HTTP/1.1 500 In...', true, 500)
#3 /Users/whoami/Herd/livewire-stream-11/vendor/symfony/http-foundation/Response.php(401): Symfony\\Component\\HttpFoundation\\Response->sendHeaders()
#4 /Users/whoami/Herd/livewire-stream-11/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(219): Symfony\\Component\\HttpFoundation\\Response->send()
#5 /Users/whoami/Herd/livewire-stream-11/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(196): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->renderHttpResponse(Object(ErrorException))
#6 /Users/whoami/Herd/livewire-stream-11/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(256): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleException(Object(ErrorException))
#7 [internal function]: Illuminate\\Foundation\\Bootstrap\\HandleExceptions->Illuminate\\Foundation\\Bootstrap\\{closure}(Object(ErrorException))
#8 {main}
thrown at /Users/whoami/Herd/livewire-stream-11/vendor/symfony/http-foundation/Response.php:322)
[stacktrace]
#0 {main}
"}