Closed nagyt234 closed 4 years ago
I confirm the issue. It is about the order of things done. PageCache takes already rendered page with dynamic placeholders such as <![CDATA[YII-DYNAMIC-1]]>
and evaluates corresponding dynamic expressions and replaces placeholders with evaluation results. Layout rendering doesn't happen at all so registerJs
has no effect.
Potentially it could be solved in web\View::renderBodyEndHtml()
and alike methods by returning through renderDynamic()
instead of directly. That would result in massive increase of evaluated expressions though making page cache not so effective.
Overall I suspect it's not a good idea to try solving it in Yii 2 but I could be wrong.
@yiisoft/reviewers what do you think?
I'd not change the default behavior for Yii 2.x (as always :smiley:) - if available, it should be optional.
Overall it looks like a massive change in behavior so I agree it's not a good idea to solve it like that.
@nagyt234 I'm closing the issue as "won't fix" but if there's a way I can't see please let me know.
OK, I agree, that to solve this issue generally is very difficult and would decrease the effectiveness of the cache. But than at least the documentation of renderDynamic should mention, that the functions register...
have no effect.
What could be quite easy, if we would have a 2nd set of functions, like register...Dynamic
which could work also with placeholders and with the limitation that assets already on the cached page are not analyzed.
Where would you document that? Actually too many methods that indirectly modify layout do not work with dynamic rendering. Basically everything except meta-tags.
@samdark I would write exactly what you wrote here in [the description of renderDynamic](https://www.yiiframework.com/doc/api/2.0/yii-base-view#renderDynamic()-detail) and probably in The Definitive Guide to Yii 2.0:
Methods that indirectly modify layout (registerJS, registerJSFile, etc.) do not work with dynamic rendering. Basically everything except meta-tags.
JS or CSS registered with registerJS, registerJSFile etc will not work in renderDynamic. This problem causes, that it is not possible to use any widgets in
renderDynamic
on a cached page which has JavaScript or CSS assets. This issue is basically #16051 re-opened with simple step-step reproducing.What steps will reproduce the problem?
views/site/index.php
:behaviors
incontrollers/SiteController.php
:What is the expected result?
The registered JavaScript should appear in the HTML page.
What do you get instead?
All Javascript registered in renderDynamic is missing.
Additional info