【问题标题】:TYPO3 Extension Pagerenderer disable cache for addJsInlineCodeTYPO3 Extension Pagerenderer 禁用 addJsInlineCode 的缓存
【发布时间】:2021-01-06 11:44:55
【问题描述】:

我有一个缓存问题,在我自己的扩展程序的 ext_localconf.php 中我调用了我的函数

$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['checkDataSubmission'][$_EXTKEY] = 'Tester\Test\Hooks\Hook';

我通过 pageRenderer 添加的位置

$pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
$pageRenderer->addJsInlineCode('Test','myvar= "'.$acckey.'";');

一些内联 Javascript。

每次从页面重新加载时都会更改 Javascript,但使用 config.no_cache = 0 时,Javascript 始终相同。

我如何告诉页面渲染器这个 Javascript 是新的并且必须更新? no_cache = 1 不是一个选项 ;)

我安装的版本是 9.5。

【问题讨论】:

    标签: typo3 typoscript typo3-9.x typo3-extensions


    【解决方案1】:

    您使用的钩子已弃用 (https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/9.5/Deprecation-86279-VariousHooksAndPSR-15Middlewares.html)。

    通过中间件

    您可能希望迁移到您的PageRenderer 代码将以非缓存方式运行的中间件 (https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/RequestHandling/Index.html)。

    或者:通过 USER_INT

    根据您的用例,您还可以从USER_INT TypoScript 对象调用您的代码。 (https://docs.typo3.org/m/typo3/reference-typoscript/master/en-us/ContentObjects/UserAndUserInt/Index.html)。这也将使您对动态添加的 JS 进行非缓存控制。性能方面,中间件应该更轻一些(特别是如果这将是您页面上的第一个 USER_INT)。将为所有请求调用中间件,USER_INT 仅用于您的page (type=0)。所以可能只是口味问题。

    TypoScript 设置:

    page.23789 = USER_INT
    page.23789 {
      userFunc = Vendor\ExtensionName\AddAssets->jsInline
    }
    

    PHP:

    namespace Vendor\ExtensionName;
    class AddAssets {
      public function jsInline(string $content, array $conf): string
      {
        \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
          \TYPO3\CMS\Core\Page\PageRenderer::class
        )
          ->addJsInlineCode(
              'extensionname-mytest',
              'alert("Hello there! \nGenerated@' . time() . ')"'
          );
        return '';
      }
    }
    

    您也可以从函数中返回 HTML (<script>...),但为了可维护性,我建议通过 PageRenderer API(或 TYPO3 v10 中的 AssetCollector + https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/10.3/Feature-90522-IntroduceAssetCollector.html)注册资产。

    【讨论】:

      猜你喜欢
      • 2011-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多