Skip to content

"HeadersAlreadySentException" with a POST call to a Commerce action using runAction #29

@luke-dp

Description

@luke-dp

Bug Report

What is happening

After updating the plugin recently, I'm getting a HeadersAlreadySentException when I try to use Datastar's runAction to update my Craft Commerce cart:

{# templates/test.twig #}
<!DOCTYPE html>
<html>
	<body>
		<button type="button" data-on:click="{{ datastar.post('_test-action') }}">
			Add to Cart
		</button>
	</body>
</html>
{# templates/_test-action.twig #}
{# purchasableId and qty parameters left out for simplicity since they are not needed to see this error occur #}
{% set response = datastar.runAction('commerce/cart/update-cart') %}
Click to view HeadersAlreadySentException log

yii\web\HeadersAlreadySentException: Headers already sent in  on line 0. in /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/yiisoft/yii2/web/Response.php:368
Stack trace:
#0 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/yiisoft/yii2/web/Response.php(341): yii\web\Response->sendHeaders()
#1 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/putyourlightson/craft-datastar-module/src/services/SseService.php(315): yii\web\Response->send()
#2 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/putyourlightson/craft-datastar-module/src/services/SseService.php(247): putyourlightson\datastar\services\SseService->throwException()
#3 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/putyourlightson/craft-datastar-module/src/controllers/DefaultController.php(60): putyourlightson\datastar\services\SseService->renderTemplate()
#4 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/putyourlightson/craft-datastar-module/src/services/SseService.php(89): putyourlightson\datastar\controllers\DefaultController->{closure:putyourlightson\datastar\controllers\DefaultController::actionIndex():59}()
#5 [internal function]: putyourlightson\datastar\services\SseService->{closure:putyourlightson\datastar\services\SseService::getEventStream():77}()
#6 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/yiisoft/yii2/web/Response.php(453): call_user_func()
#7 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/yiisoft/yii2/web/Response.php(342): yii\web\Response->sendContent()
#8 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/yiisoft/yii2/base/Application.php(397): yii\web\Response->send()
#9 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/web/index.php(12): yii\base\Application->run()
#10 {main}

After adding custom logging, I got this as the specific error being triggered on runAction():

Click to view session cookie parameters log

==CUSTOMLOG== $exceptionyii\base\ErrorException: session_set_cookie_params(): Session cookie parameters cannot be changed after headers have already been sent in /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/yiisoft/yii2/web/Session.php:424
Stack trace:
#0 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/craftcms/cms/src/web/ErrorHandler.php(115): yii\base\ErrorHandler->handleError()
#1 [internal function]: craft\web\ErrorHandler->handleError()
#2 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/yiisoft/yii2/web/Session.php(424): session_set_cookie_params()
#3 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/yiisoft/yii2/web/Session.php(152): yii\web\Session->setCookieParamsInternal()
#4 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/yiisoft/yii2/web/Session.php(733): yii\web\Session->open()
#5 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/craftcms/commerce/src/controllers/CartController.php(286): yii\web\Session->remove()
#6 [internal function]: craft\commerce\controllers\CartController->actionUpdateCart()
#7 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/yiisoft/yii2/base/InlineAction.php(66): call_user_func_array()
#8 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/yiisoft/yii2/base/Controller.php(197): yii\base\InlineAction->runWithParams()
#9 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/yiisoft/yii2/base/Module.php(554): yii\base\Controller->runAction()
#10 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/craftcms/cms/src/web/Application.php(370): yii\base\Module->runAction()
#11 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/putyourlightson/craft-datastar-module/src/helpers/Request.php(38): craft\web\Application->runAction()
#12 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/putyourlightson/craft-datastar-module/src/variables/DatastarVariable.php(70): putyourlightson\datastar\helpers\Request::runAction()
#13 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/twig/twig/src/Extension/CoreExtension.php(1909): putyourlightson\datastar\variables\DatastarVariable->runAction()
#14 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/craftcms/cms/src/helpers/Template.php(154): Twig\Extension\CoreExtension::getAttribute()
#15 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/storage/runtime/compiled_templates/ef/efd549536b357fb18ebb97f7f512f84b.php(46): craft\helpers\Template::attribute()
#16 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/twig/twig/src/Template.php(402): __TwigTemplate_e971ae49bb7b7cbae5e05282d195fd46->doDisplay()
#17 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/twig/twig/src/Template.php(358): Twig\Template->yield()
#18 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/twig/twig/src/Template.php(373): Twig\Template->display()
#19 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/twig/twig/src/TemplateWrapper.php(51): Twig\Template->render()
#20 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/twig/twig/src/Environment.php(333): Twig\TemplateWrapper->render()
#21 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/craftcms/cms/src/web/View.php(590): Twig\Environment->render()
#22 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/putyourlightson/craft-datastar-module/src/services/SseService.php(241): craft\web\View->renderTemplate()
#23 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/putyourlightson/craft-datastar-module/src/controllers/DefaultController.php(60): putyourlightson\datastar\services\SseService->renderTemplate()
#24 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/putyourlightson/craft-datastar-module/src/services/SseService.php(89): putyourlightson\datastar\controllers\DefaultController->{closure:putyourlightson\datastar\controllers\DefaultController::actionIndex():59}()
#25 [internal function]: putyourlightson\datastar\services\SseService->{closure:putyourlightson\datastar\services\SseService::getEventStream():77}()
#26 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/yiisoft/yii2/web/Response.php(453): call_user_func()
#27 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/yiisoft/yii2/web/Response.php(342): yii\web\Response->sendContent()
#28 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/yiisoft/yii2/base/Application.php(397): yii\web\Response->send()
#29 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/web/index.php(12): yii\base\Application->run()
#30 {main}

Next Twig\Error\RuntimeError: An exception has been thrown during the rendering of a template ("session_set_cookie_params(): Session cookie parameters cannot be changed after headers have already been sent") in "_test-action" at line 2. in /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/templates/_test-action.twig:2
Stack trace:
#0 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/twig/twig/src/Template.php(358): Twig\Template->yield()
#1 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/twig/twig/src/Template.php(373): Twig\Template->display()
#2 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/twig/twig/src/TemplateWrapper.php(51): Twig\Template->render()
#3 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/twig/twig/src/Environment.php(333): Twig\TemplateWrapper->render()
#4 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/craftcms/cms/src/web/View.php(590): Twig\Environment->render()
#5 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/putyourlightson/craft-datastar-module/src/services/SseService.php(241): craft\web\View->renderTemplate()
#6 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/putyourlightson/craft-datastar-module/src/controllers/DefaultController.php(60): putyourlightson\datastar\services\SseService->renderTemplate()
#7 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/putyourlightson/craft-datastar-module/src/services/SseService.php(89): putyourlightson\datastar\controllers\DefaultController->{closure:putyourlightson\datastar\controllers\DefaultController::actionIndex():59}()
#8 [internal function]: putyourlightson\datastar\services\SseService->{closure:putyourlightson\datastar\services\SseService::getEventStream():77}()
#9 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/yiisoft/yii2/web/Response.php(453): call_user_func()
#10 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/yiisoft/yii2/web/Response.php(342): yii\web\Response->sendContent()
#11 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/vendor/yiisoft/yii2/base/Application.php(397): yii\web\Response->send()
#12 /mnt/BLOCKSTORAGE/home/1277938.cloudwaysapps.com/kxmjvasumx/public_html/web/index.php(12): yii\base\Application->run()
#13 {main}

It appears that setting the shouldCloseSession config setting to false fixes this issue.

Just a note of clarification: If I do a GET request to commerce/cart/get-cart instead, that works fine.

What I expected to happen

Cart gets updated error-free. Will need to add products and include the purchasableId and qty parameters in the commerce/cart/update-cart runAction to actually have the cart update.

Steps to reproduce the issue

On a Craft CMS install with Craft Commerce and Datastar plugins installed, create the test.twig and _test-action.twig files referenced above and paste in the code. Create a Single entry called Test. Go to the /test URL and click Add To Cart. Open the Craft debug toolbar, click AJAX, and click on the entry in the list. The logs should show the HeadersAlreadySentException. To see the session cookie parameters error, insert Craft::warning('==CUSTOMLOG== $exception' . $exception); after line 245 in the vendor/putyourlightson/craft-datastar-module/src/services/SseService.php file.

Image

Plugin Version

1.0.0-RC.11

Craft CMS Version

5.9.14

PHP Version

8.4.18

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions