Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

typoscript - TYPO3 Extension Pagerenderer disable cache for addJsInlineCode

I have an cache issue, in my ext_localconf.php from my own extension I call my function

$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['tslib/class.tslib_fe.php']['checkDataSubmission'][$_EXTKEY] = 'TesterTestHooksHook';

where I add via pageRenderer

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

some inline Javascript.

The Javascript is changed on every reload from the page but with config.no_cache = 0 the Javascript is all time the same.

How can I tell the pagerenderer that this Javascript is new and must be updated? no_cache = 1 is not a option ;)

My installed version is 9.5.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The hook you used is deprecated (https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/9.5/Deprecation-86279-VariousHooksAndPSR-15Middlewares.html).

Via a middleware

You might want to migrate to a middleware where your PageRenderer code will be run non-cached (https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/RequestHandling/Index.html).

Or: via USER_INT

Depending on your use case, you can also call your code from a USER_INT TypoScript object. (https://docs.typo3.org/m/typo3/reference-typoscript/master/en-us/ContentObjects/UserAndUserInt/Index.html). This will also give you non-cached control over the dynamically added JS. Performance-wise the middleware should be a bit lighter (especially if this would be the first USER_INT on your page). The middleware will be called for all requests, the USER_INT only for your page (type=0). So probably its just a matter of taste.

TypoScript setup:

page.23789 = USER_INT
page.23789 {
  userFunc = VendorExtensionNameAddAssets->jsInline
}

PHP:

namespace VendorExtensionName;
class AddAssets {
  public function jsInline(string $content, array $conf): string
  {
    TYPO3CMSCoreUtilityGeneralUtility::makeInstance(
      TYPO3CMSCorePagePageRenderer::class
    )
      ->addJsInlineCode(
          'extensionname-mytest',
          'alert("Hello there! 
Generated@' . time() . ')"'
      );
    return '';
  }
}

You could also return the HTML (<script>...) from the function but for maintainability I suggest registering assets via the PageRenderer API (or AssetCollector in TYPO3 v10+ https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/10.3/Feature-90522-IntroduceAssetCollector.html).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...