2525class TypoScriptRenderingMiddleware implements MiddlewareInterface
2626{
2727 private const argumentNamespace = 'tx_typoscriptrendering ' ;
28+ private const defaultContentType = 'text/html ' ;
2829
2930 /**
3031 * Dispatches the request to the corresponding typoscript_rendering configuration
@@ -37,20 +38,47 @@ class TypoScriptRenderingMiddleware implements MiddlewareInterface
3738 public function process (ServerRequestInterface $ request , RequestHandlerInterface $ handler ): ResponseInterface
3839 {
3940 $ frontendController = $ GLOBALS ['TSFE ' ];
41+ $ requestedContentType = $ frontendController ->config ['config ' ]['contentType ' ] ?? self ::defaultContentType;
4042 if (!$ frontendController ->isGeneratePage () || !isset ($ request ->getQueryParams ()[self ::argumentNamespace])) {
41- return $ handler ->handle ($ request );
43+ return $ this -> amendContentType ( $ handler ->handle ($ request), $ requestedContentType );
4244 }
4345 $ this ->ensureRequiredEnvironment ();
4446
47+ $ frontendController ->config ['config ' ]['debug ' ] = 0 ;
4548 $ frontendController ->config ['config ' ]['disableAllHeaderCode ' ] = 1 ;
49+ $ frontendController ->config ['config ' ]['disableCharsetHeader ' ] = 0 ;
4650 $ frontendController ->pSetup = [
4751 '10 ' => 'TYPOSCRIPT_RENDERING ' ,
4852 '10. ' => [
4953 'request ' => $ request ->getQueryParams ()[self ::argumentNamespace],
5054 ],
5155 ];
5256
53- return $ handler ->handle ($ request );
57+ return $ this ->amendContentType ($ handler ->handle ($ request ), $ requestedContentType );
58+ }
59+
60+ /**
61+ * TYPO3's frontend rendering allows to influence the content type,
62+ * but does not store this information in cache, which leads to wrong content type
63+ * to be sent when content if pulled from cache.
64+ * We add a tiny workaround, that allows plugins to set the content type, but also
65+ * store the content type in cache:
66+ *
67+ * $GLOBALS['TSFE']->setContentType('application/json');
68+ * $GLOBALS['TSFE']->config['config']['contentType'] = 'application/json';
69+ *
70+ * @param ResponseInterface $response
71+ * @param string $requestedContentType
72+ * @return ResponseInterface
73+ */
74+ private function amendContentType (ResponseInterface $ response , string $ requestedContentType ): ResponseInterface
75+ {
76+ $ originalContentTypeHeader = $ response ->getHeader ('Content-Type ' )[0 ];
77+ if (strpos ($ originalContentTypeHeader , self ::defaultContentType) === 0 && strpos ($ originalContentTypeHeader , $ requestedContentType ) === false ) {
78+ $ response = $ response ->withHeader ('Content-Type ' , \str_replace (self ::defaultContentType, $ requestedContentType , $ originalContentTypeHeader ));
79+ }
80+
81+ return $ response ;
5482 }
5583
5684 /**
0 commit comments