#97652f:asset.css and f:asset.script should be able to render CSS and JS from referenced files as block inline
Description
One essential aspect of professional SEO for high availability and high-performance online solutions is a fine-grained CSS and JS devilery tailored to the respective situation and persona of every single page view. The inline integration of critical CSS and JS is an indispensable measure for the optimal coordination of payload, round trips, render time etc. Offloading this optimization to HTTP/2 only does not even come close to the optimal solution. f:asset.css and f:asset.script contribute to this scaleability to a large extend by adding assets to the source only when the corresponding component is rendered! thumbs up
But it is currently not possible to integrate files with the ViewHelpers f:asset.css and f:asset.js and have their content output inline. Since both ViewHelpers are set to replace the integration of CSS and JS with TypoScript at some point (at least that is what I’ve read so far), this option must be created.
I know that it is possible to place CSS and JS as content directly within the ViewHelpers enclosing tags. But that is not an option for the development and maintenance of seriously meant projects. The strength of TYPO3 is its granularity and therefore the flexibility to compose dev teams of specialist for each aspect of the project. To keep this strength the freedom to leverage modern FE development concepts is essential. To copy and paste e.g. CSS resulting from a development pipeline into to a fluid template to have it rendered inline is not a modern development concept.
I therefore suggest giving the ViewHelpers f:asset.css and f:asset.js additional attributes to allow the following:
- Include CSS files to render them inline as individual style blocks: critical=“true”
- Include CSS files to render them inline and concatenated as a single combined style block: critical=“true” concatenate=“true”
- Include JS files to render them inline as individual script block: inline=“true”
- Include JS files to render them inline and concatenated as a single combined script block: inline=“true” concatenate=“true”
Since these features are essential to contemporary, professional FE development they should be part of the core rather than community extensions.
Reviews 1
Files 6
History 8
-
Jan Loderhose commented and changed attached f_asset-css-inline-and-external-poc.html attached f_asset-css-single-inline-only-poc.html attached f_asset-css-concatenated-and-single-inline-only-poc.html attached f_asset-js-concatenated-and-single-inline-only-poc.html attached f_asset-js-single-inline-poc.html attached f_asset-js-inplace.html <span data-processor="relativetime" data-value="2022-05-20T10:09:30+00:00" > 05/20/22, 10:09 </span>
Hi Georg,
thank you for your quick reply! Sorry, my late. ;-)
I’ve prepared some poc files and attached them to this comment. That was a lucky request on your side because creating these examples revealed it least one further implication. Maybe it’s best to have a look at the files first and afterwards continue with the
Priority conondrum
In file f_asset-css-concatenated-and-single-inline-only-poc.html I show how 4 CSS files are included. Three of them are concatenated. One of them has the priority attribute set to true. The result is that the three files are ordered by the order of their ViewHelper instances within the fluid template and the priority attribute and then their contents are concatenated and rendered in a single style block in the resulting order. A fourth file is not concatenated and therefore is simply rendered in a separate style block which is placed after the concatenated block.
What if the fourth file was included with priority=“true”, too?
We have a style block with the content of concatenated CSS files, one with priority=“true”. The priority brings the files rendered within one style block into the right order.
And we have a style block with the content of a single CSS file. Priority=“true” currently means that it must be rendered first. But the concatenated style block contains prioritized CSS that should be first, too.Well I did not find a plausible solution to that. In the comments within in the files I write that one goal should be to allow for the maximum possible flexibility. But to be true, I don’t see a reason why multiple inline style blocks should be needed other than someone is not able to handle CSS properly. So maybe concatenation of critical (and therefore inline) CSS should simply be the default. Sort all critical CSS files according to their order and priority and put their contents in a single style blick. Voila. If one needs a separate inline style block, let this be done by an explicit concatenate=“false” and let the developer see how he/she handles avoidable conflicts then. (My examples use explizit concatenate=“true” but this topic is still subject to discussion so I think that’s fine for now.)
A second aspect to have in mind is that people like to include external libraries just as they are delivered. Usually libraries are included first. This seems to make perfect sense but we are talking about inlining CSS because it is critical and is therefore needed asap to allow the browser to start the render process without the need to wait for additional requests for assets to complete. I show this in file f_asset-css-inline-and-external-poc.html. An external library that is needed to start the render process also needs to be inlined. Otherwise the whole concept does not make sense. But, what if an external CSS src is not available when it is requested. Waiting for seconds for a reply is no real option. After all, we are talking about speed an efficiency here. Maybe the critical attribute should not be available for the inclusion of external libraries.
So, always have the inline (cricital) CSS block render first. Every additional CSS inclusion comes after that. Libraries and what ever linked CSS.
Regarding JS: The thoughts concerning the priority and the need to have multiple script blocks are essentially the same. That’s why I suggest to- collect all JS for the header and for the end of the body tag separately
- per collection: order it by the order of inclusion and priority
- per collection: render all inlined js in a single script block and all references to files afterwards.
- included external files,
- inline JS which is written inline and
- inlined content of js files.
If such an attribute will be added it should not be possible to combine it with the concatenate attribute (or it should implicitly set concatenate=“false” if it will be true by default).
I suppose, if f:asset.css and f:asset.script would take all the above annotations into account they would provide an even better support for asset handling as TypoScript ever did. ;-)
What do you think? Does this make sense or did I miss something?
-
Georg Ringer commented <span data-processor="relativetime" data-value="2022-05-21T06:23:17+00:00" > 05/21/22, 06:23 </span>
thanks for the very detailed feedback! I am really not too much into the frontend process tbh but forge is not the right place to discuss things. what about using slack for that?
we can change everything, so just as an idea: if this is currently allowed to have external resources inlined we can throw an exception there because it doesn’t make sense
-
Jan Loderhose commented <span data-processor="relativetime" data-value="2022-05-23T07:57:58+00:00" > 05/23/22, 07:57 </span>
yeah, let’s switch to slack. which channel? We can also use DMs. My name on slack is the same as here.
-
Frank Nägler commented <span data-processor="relativetime" data-value="2022-07-11T15:01:22+00:00" > 07/11/22, 15:01 </span>
I think the purpose of this ViewHelper is different from your use case.
The VH does not produce direct output, for this reason the VH does not need to be wrapped in an escaping node.
Also, the $escapeChildren is set to false, a possible security issue if the ViewHelper works in the described way.The purpose is to collect inline code to get the body free of style and script tags.
Both are relevant scenarios for using the “content security policy” header in a helpfull way.Changing the behavior of this ViewHelper is maybe not the best idea.
I can’t see the benefits, but I would opt for a dedicated ViewHelper if this feature will be accepted. -
Benni Mack commented and changed status
NewNeeds Feedback <span data-processor="relativetime" data-value="2025-01-21T15:42:54+00:00" > 01/21/25, 15:42 </span>Hey,
doesn’t this one solve the actual issue? https://review.typo3.org/c/Packages/TYPO3.CMS/+/84424
-
Riccardo De Contardi commented <span data-processor="relativetime" data-value="2025-01-30T15:36:39+00:00" > 01/30/25, 15:36 </span>
f:asset.scriptandf:asset.cssdon’t cover all the feature expressed here; This is what I found on the latest main (but on 13.x the results are the same)test 1
File Reference:
f_asset-css-inline-and-external-poc.htmltried with
<f:asset.css identifier="normalize" href="https://cdn.jsdelivr.net/npm/normalize.css@8.0.1/normalize.css" inline="1" /> <f:asset.css identifier="example" href="fileadmin/Assets/example.css" priority="1" inline="1" /> <f:asset.css identifier="example2" href="fileadmin/Assets/example2.css" priority="1" inline="1" />
Conclusion: adding
inline="1"to a CSS file with an external URL leads to an exceptionso just
<f:asset.css identifier="normalize" href="https://cdn.jsdelivr.net/npm/normalize.css@8.0.1/normalize.css" /> <f:asset.css identifier="example" href="fileadmin/Assets/example.css" priority="1" inline="1" /> <f:asset.css identifier="example2" href="fileadmin/Assets/example2.css" priority="1" inline="1" />
works, i.e. with
inline=1 priority=1both files are rendered inline and in the <head> section of the page</head>test 2
File reference
f_asset-css-single-inline-only-poc.htmltried with
<f:asset.css identifier="example" href="fileadmin/Assets/example.css" priority="1" inline="1" /> <f:asset.css identifier="example2" href="fileadmin/Assets/example2.css" priority="1" inline="1" />
Conclusion:
seems to work as per the example, i.e. withinline=1 priority=1both files are rendered inline and in the<head>section of the pagetest 3
File reference
f_asset-css-concatenated-and-single-inline-only-poc.html1) There is no
concatenate="true"attribute, so this feature is absent. thus all inline<style>are always separate2) the
priority=1attribute as far as I can see renders the<style>tags inside the<head>section of HTML; without it the<style>tags are rendered just before the closing</body>tag
Thus to obtain the same order of the example you have to write:<f:asset.css identifier="example3" href="fileadmin/Assets/example3.css" priority="1" inline="1" /> <f:asset.css identifier="example" href="fileadmin/Assets/example.css" priority="1" inline="1" /> <f:asset.css identifier="example2" href="fileadmin/Assets/example2.css" priority="1" inline="1" /> <f:asset.css identifier="example4" href="fileadmin/Assets/example4.css" priority="1" inline="1" />
test 4
File reference
f_asset-js-concatenated-and-single-inline-only-poc.html1) There is no
concatenate="true"attribute, so this feature is absent. thus all inline<script>tags are always separate
thus<f:asset.script identifier="examplejs" src="fileadmin/Assets/example.js" inline="1"/> <f:asset.script identifier="examplejs2" src="fileadmin/Assets/example2.js" inline="1" />
writes inline
<scripts>just before the closing</body>tagtest 5
File reference
f_asset-js-single-inline-poc.html<f:asset.script identifier="examplejs" src="fileadmin/Assets/example.js" inline="1"/> <f:asset.script identifier="examplejs2" src="fileadmin/Assets/example2.js" inline="1" />
works as per the example
test 6
File reference
f_asset-js-inplace.html1) There is no
inplace="true"attribute, thus the<script>tags are always rendered just before the closing</body>tag, even if put for example inside a partial.
Addingpriority=1they will placed in the<head>tag -
Georg Ringer commented and changed status
Needs FeedbackClosed <span data-processor="relativetime" data-value="2026-10-15T20:22:54+00:00" > 10/15/26, 20:22 </span>Comment draft for #97652
Hi,
the core of this request is available since TYPO3 13.3:
f:asset.cssandf:asset.scripthave aninlineargument which embeds the content of the referenced file (see Feature-99510, change 84424). Together withprioritythe inlined files end up in the<head>, as shown by the tests in note 7. Inlining an external URL throws an exception, as proposed here.Not available are
concatenateandinplace. I would not add them:- The inlined blocks are collected by the AssetCollector and rendered at a defined place on purpose. This keeps the body free of
<style>and<script>and works with a Content Security Policy and nonces.inplacecontradicts that. - There was no further interest in
concatenatesince the discussion in 2022, and the open question how it should work withprioritywas never answered.
I suggest to close this issue as resolved by #99510. If somebody needs
concatenateorinplace, please open a new, focused issue with a concrete use case. - The inlined blocks are collected by the AssetCollector and rendered at a defined place on purpose. This keeps the body free of
thanks for creating the issue. Would it be possible for you to provide a proof of concept HTML output?