如何在TYPO3中移除JSON视图的缓存注释(不关闭缓存且不修改核心文件)
解决TYPO3 JSON页面缓存注释破坏格式的问题
我来帮你搞定这个头疼的问题,完全符合你的要求——不用修改核心文件、不关闭整体缓存,通过自定义扩展就能解决。这里有两种可靠的实现方案:
方案1:利用AfterGeneratePageEvent事件监听器
这个方案是在页面内容生成后、缓存存储前直接处理内容,移除缓存注释。
步骤1:创建事件监听器类
在你的自定义扩展中新建Classes/EventListener/RemoveCacheCommentForJsonListener.php文件,内容如下:
<?php declare(strict_types=1); namespace YourVendor\YourExtension\EventListener; use TYPO3\CMS\Frontend\Event\AfterGeneratePageEvent; class RemoveCacheCommentForJsonListener { public function __invoke(AfterGeneratePageEvent $event): void { $tsfe = $event->getController(); // 检查当前页面是否设置了JSON类型的Content-Type $contentType = $tsfe->additionalHeaders['Content-Type'] ?? ''; if (str_contains($contentType, 'application/json')) { // 移除末尾的缓存注释 $content = $tsfe->content; $content = preg_replace('/<!-- Cached page generated.*?-->$/s', '', $content); $tsfe->content = trim($content); } } }
步骤2:注册监听器
在扩展的Configuration/Services.yaml中添加监听器注册配置:
services: YourVendor\YourExtension\EventListener\RemoveCacheCommentForJsonListener: tags: - name: event.listener identifier: 'yourextension-remove-json-cache-comment' event: TYPO3\CMS\Frontend\Event\AfterGeneratePageEvent
方案2:使用HTTP中间件(更通用)
如果事件监听器没达到预期效果,中间件可以在响应发送的最后阶段处理内容,确保注释被过滤掉。
步骤1:创建中间件类
在扩展中新建Classes/Middleware/RemoveJsonCacheCommentMiddleware.php文件:
<?php declare(strict_types=1); namespace YourVendor\YourExtension\Middleware; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; class RemoveJsonCacheCommentMiddleware implements MiddlewareInterface { public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $response = $handler->handle($request); // 检查响应头是否为JSON类型 $contentType = $response->getHeaderLine('Content-Type'); if (str_contains($contentType, 'application/json')) { $bodyContent = $response->getBody()->getContents(); // 过滤掉末尾的缓存注释 $bodyContent = preg_replace('/<!-- Cached page generated.*?-->$/s', '', $bodyContent); $bodyContent = trim($bodyContent); // 重新构建响应体 $newBody = new \GuzzleHttp\Psr7\Stream(fopen('php://temp', 'rwb+')); $newBody->write($bodyContent); $newBody->rewind(); $response = $response->withBody($newBody); } return $response; } }
步骤2:注册中间件
在扩展的Configuration/RequestMiddlewares.php中注册中间件:
<?php return [ 'frontend' => [ 'yourvendor/yourextension/remove-json-cache-comment' => [ 'target' => \YourVendor\YourExtension\Middleware\RemoveJsonCacheCommentMiddleware::class, 'after' => [ 'typo3/cms-frontend/prepare-tsfe-rendering', ], 'before' => [ 'typo3/cms-frontend/output-compression', ], ], ], ];
验证与注意事项
- 确保你的TypoScript配置正确(示例):
page { typeNum = 123 disableAllHeaderCode = 1 additionalHeaders { 10 { header = Content-Type: application/json; charset=utf-8 } } # 这里替换成你的JSON内容生成逻辑 10 = USER 10 { userFunc = YourVendor\YourExtension\Controller\JsonGenerator->generateContent } }
- 替换代码中的
YourVendor\YourExtension为你实际的供应商名称和扩展名称 - 安装激活扩展后清除缓存,测试首次和后续请求,应该都能输出纯净的JSON格式内容
内容的提问来源于stack exchange,提问作者kledo-34
相关产品推荐
相关产品推荐

