Skip to content

Commit 408591d

Browse files
authored
Merge pull request #75 from helhum/stability-improvements
2 parents e9ede26 + 489c4a8 commit 408591d

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

Classes/Middleware/TypoScriptRenderingMiddleware.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
class 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
/**

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"license": "GPL-2.0-or-later",
2323
"require": {
2424
"php": "^7.2",
25-
"typo3/cms-core": "^9.5 || ^10.4"
25+
"typo3/cms-core": "^9.5.17 || ^10.4.2"
2626
},
2727
"require-dev": {
2828
"nimut/testing-framework": "^4.0 || ^5.0",

0 commit comments

Comments
 (0)