diff --git a/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php b/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
index 54ebe583480eb1941f0f4c2d40cc8fbf815f8065..8a6c5c0dcaabee417e8048115b8e503c83f1b128 100644
--- a/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
+++ b/typo3/sysext/frontend/Classes/Controller/TypoScriptFrontendController.php
@@ -3539,17 +3539,6 @@ class TypoScriptFrontendController implements LoggerAwareInterface
                 GeneralUtility::callUserFunction($_funcRef, $_params, $this);
             }
         }
-        // Send content-length header.
-        // Notice that all HTML content outside the length of the content-length header will be cut off!
-        // Therefore content of unknown length from included PHP-scripts and if admin users are logged
-        // in (admin panel might show...) or if debug mode is turned on, we disable it!
-        if (
-            (!isset($this->config['config']['enableContentLengthHeader']) || $this->config['config']['enableContentLengthHeader'])
-            && !$this->beUserLogin && !$GLOBALS['TYPO3_CONF_VARS']['FE']['debug']
-            && !$this->config['config']['debug'] && !$this->doWorkspacePreview()
-        ) {
-            header('Content-Length: ' . strlen($this->content));
-        }
     }
 
     /**
diff --git a/typo3/sysext/frontend/Classes/Http/RequestHandler.php b/typo3/sysext/frontend/Classes/Http/RequestHandler.php
index f5ea643fc2a235a624ab69672e0bd69f217dd1cc..47e93efc7301268725dbe23b0320888d20145840 100644
--- a/typo3/sysext/frontend/Classes/Http/RequestHandler.php
+++ b/typo3/sysext/frontend/Classes/Http/RequestHandler.php
@@ -263,6 +263,17 @@ class RequestHandler implements RequestHandlerInterface
         }
 
         if ($sendTSFEContent) {
+            // Send content-length header.
+            // Notice that all HTML content outside the length of the content-length header will be cut off!
+            // Therefore content of unknown length from included PHP-scripts and if admin users are logged
+            // in (admin panel might show...) or if debug mode is turned on, we disable it!
+            if (
+                (!isset($this->controller->config['config']['enableContentLengthHeader']) || $this->controller->config['config']['enableContentLengthHeader'])
+                && !$this->controller->beUserLogin && !$GLOBALS['TYPO3_CONF_VARS']['FE']['debug']
+                && !$this->controller->config['config']['debug'] && !$this->controller->doWorkspacePreview()
+            ) {
+                header('Content-Length: ' . strlen($this->controller->content));
+            }
             $response->getBody()->write($this->controller->content);
         }
         GeneralUtility::makeInstance(LogManager::class)