boldenamsterdam / htmlcache

Cache pages to HTML and boost website performance on Craft CMS 3
MIT License
31 stars 21 forks source link

fixed HeadersAlreadySentException #5

Closed bragle closed 5 years ago

bragle commented 5 years ago

When using devmode, im getting a HeadersAlreadySent exception from Yii. This seems to fix it.

Alternatively, setting \Yii::$app->response->isSent to true before echoing the output will also work.

bragle commented 5 years ago

I'm actually not quite sure why I'm getting that error, but I suspect it might have something with running craft on windows. I'm using the lastest default LAMP stack on win 10 with xampp.

This is the the error message (printed below the page content):

`An Error occurred while handling another error: yii\web\HeadersAlreadySentException: Headers already sent in C:\xampp\htdocs\craft\vendor\bolden\htmlcache\src\services\HtmlcacheService.php on line 256. in C:\xampp\htdocs\craft\vendor\yiisoft\yii2\web\Response.php:366 Stack trace:

0 C:\xampp\htdocs\craft\vendor\yiisoft\yii2\web\Response.php(339): yii\web\Response->sendHeaders()

1 C:\xampp\htdocs\craft\vendor\yiisoft\yii2\web\ErrorHandler.php(135): yii\web\Response->send()

2 C:\xampp\htdocs\craft\vendor\craftcms\cms\src\web\ErrorHandler.php(132): yii\web\ErrorHandler->renderException(Object(yii\web\HeadersAlreadySentException))

3 C:\xampp\htdocs\craft\vendor\yiisoft\yii2\base\ErrorHandler.php(111): craft\web\ErrorHandler->renderException(Object(yii\web\HeadersAlreadySentException))

4 C:\xampp\htdocs\craft\vendor\craftcms\cms\src\web\ErrorHandler.php(63): yii\base\ErrorHandler->handleException(Object(yii\web\HeadersAlreadySentException))

5 [internal function]: craft\web\ErrorHandler->handleException(Object(yii\web\HeadersAlreadySentException))

6 {main}

Previous exception: yii\web\HeadersAlreadySentException: Headers already sent in C:\xampp\htdocs\craft\vendor\bolden\htmlcache\src\services\HtmlcacheService.php on line 256. in C:\xampp\htdocs\craft\vendor\yiisoft\yii2\web\Response.php:366 Stack trace:

0 C:\xampp\htdocs\craft\vendor\yiisoft\yii2\web\Response.php(339): yii\web\Response->sendHeaders()

1 C:\xampp\htdocs\craft\vendor\yiisoft\yii2\base\Application.php(656): yii\web\Response->send()

2 C:\xampp\htdocs\craft\vendor\bolden\htmlcache\src\services\HtmlcacheService.php(61): yii\base\Application->end()

3 C:\xampp\htdocs\craft\vendor\bolden\htmlcache\src\HtmlCache.php(126): bolden\htmlcache\services\HtmlcacheService->checkForCacheFile()

4 C:\xampp\htdocs\craft\vendor\yiisoft\yii2\base\BaseObject.php(109): bolden\htmlcache\HtmlCache->init()

5 C:\xampp\htdocs\craft\vendor\yiisoft\yii2\base\Module.php(158): yii\base\BaseObject->__construct(Array)

6 C:\xampp\htdocs\craft\vendor\craftcms\cms\src\base\Plugin.php(113): yii\base\Module->__construct('html-cache', Object(craft\web\Application), Array)

7 [internal function]: craft\base\Plugin->__construct('html-cache', Object(craft\web\Application), Array)

8 C:\xampp\htdocs\craft\vendor\yiisoft\yii2\di\Container.php(383): ReflectionClass->newInstanceArgs(Array)

9 C:\xampp\htdocs\craft\vendor\yiisoft\yii2\di\Container.php(156): yii\di\Container->build('bolden\htmlcach...', Array, Array)

10 C:\xampp\htdocs\craft\vendor\yiisoft\yii2\BaseYii.php(349): yii\di\Container->get('bolden\htmlcach...', Array, Array)

11 C:\xampp\htdocs\craft\vendor\craftcms\cms\src\services\Plugins.php(788): yii\BaseYii::createObject(Array, Array)

12 C:\xampp\htdocs\craft\vendor\craftcms\cms\src\services\Plugins.php(199): craft\services\Plugins->createPlugin('html-cache', Array)

13 C:\xampp\htdocs\craft\vendor\craftcms\cms\src\base\ApplicationTrait.php(1148): craft\services\Plugins->loadPlugins()

14 C:\xampp\htdocs\craft\vendor\craftcms\cms\src\web\Application.php(109): craft\web\Application->_postInit()

15 C:\xampp\htdocs\craft\vendor\yiisoft\yii2\base\BaseObject.php(109): craft\web\Application->init()

16 C:\xampp\htdocs\craft\vendor\yiisoft\yii2\base\Application.php(206): yii\base\BaseObject->__construct(Array)

17 C:\xampp\htdocs\craft\vendor\craftcms\cms\src\web\Application.php(97): yii\base\Application->__construct(Array)

18 [internal function]: craft\web\Application->__construct(Array)

19 C:\xampp\htdocs\craft\vendor\yiisoft\yii2\di\Container.php(383): ReflectionClass->newInstanceArgs(Array)

20 C:\xampp\htdocs\craft\vendor\yiisoft\yii2\di\Container.php(156): yii\di\Container->build('craft\web\Appli...', Array, Array)

21 C:\xampp\htdocs\craft\vendor\yiisoft\yii2\BaseYii.php(349): yii\di\Container->get('craft\web\Appli...', Array, Array)

22 C:\xampp\htdocs\craft\vendor\craftcms\cms\bootstrap\bootstrap.php(252): yii\BaseYii::createObject(Array)

23 C:\xampp\htdocs\craft\vendor\craftcms\cms\bootstrap\web.php(42): require('C:\xampp\htdocs...')

24 C:\xampp\htdocs\craft\web\index.php(20): require('C:\xampp\htdocs...')

25 {main}`

boldenamsterdam commented 5 years ago

Not sure either. I suspect that there is an error happening but it cannot be displayed (cannot send headers) because the html cache plugin already sent content. Maybe you can figure out the error that is happening in your application.

You suggestion seems legit and it did not create any issues when I tested it. I will merge the code and release a new version.

bragle commented 5 years ago

BTW, you might want to change \Yii::$app to \Craft::$app. I don't believe it'll make any difference, but it might be nice for consistency.

boldenamsterdam commented 5 years ago

Good point thanks. Although it should not matter as Craft extends Yii.