-
Notifications
You must be signed in to change notification settings - Fork 12k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for Automatically Minimize Render-Blocking CSS #17764
Comments
Actually there could be two approaches,
Apps using There's also an official webpack plugin (https://github.com/numical/script-ext-html-webpack-plugin) which could be helpful. I tried adding below to my
|
Here's the similar plugin for |
With a default Angular application, pre-loading the styles won't do any good. The whole application is rendered using JavaScript, so unless you are using Angular Universal, this extra configuration is of no real help. And to avoid having huge initial styles, you can minimize your global styles (defined in angular.json) by using lazy modules and defining the module specific styles inside components for that module; You could have something like a wrapping "shell" component with
Because of the dynamic nature of Angular application and the complexity behind the routing system that supports lazy modules, modules like that won't be of any use. Your best bet is to use lazy-modules with a shell component that deals with more generic styles and in general: just be careful when splitting the application into lazy-modules. |
Thank you so much @SchnWalter. I agree to your comment, my suggestion is mainly for the Universal apps and those apps that are using appshell model. What if we could add an option of |
Yes, but that's not for "Render-Blocking CSS", that's for all the global CSS, and for most angular projects, that won't give a noticeable performance boost for such a feature to be added to the core Angular tooling; this is something for a 3rd party builder. |
Ok, from your comment #17764 (comment) I am trying another approach, adding critical path css to the |
In general injected global css should be render blocking as otherwise it will cause flickering when there is the transition between the server and client. My recommendation would be to use both injected and non injected global styles and load the non injected global styles eagerly. Again this might cause flickering when used with SSR. This issue is also related to #11395 which I think it make sense to continue tracking over there. |
Closing in favour of #11395. If there is a reason why this should be tracked separately please let me know. |
Thanks @alan-agius4 . I thinks its a different feature request but I wont mind if this could be merged and tracked with #11395 since the objective in both cases is almost same. Originally my request was that Angular CLI should automatically detect the css for the
If this is too complex to implement and you think it wont add much value, at least there should be an option for user to specify how a particular style file should be added to production output. For example, it would be great to add
With above settings, Angular CLI should inline the contents of |
Unless I am misunderstanding something. This plugin will not work with JS frameworks such as Angular, because the plugin takes an HTML to detect which CSS classes are critical. With regards to preload and inlining the entire contents, this actually can already be done by extending the default CLI builder. That said inlining CSS, opens up another discussion around CSP (Content security policy). |
I had some idea, and it probably could work only in case of universal/prerendered app or apps using app-shell.
Can you please share some more details on this? some docs or example may be?
I think Google also suggest inlining critical styles and asynchronously loading rest of the styles https://web.dev/render-blocking-resources/ , btw when we're using SSR it also inlines the styles in head of the document. |
This will potentially slow down universal, because each response will need to get parsed and generate critical stylesheet on the fly because each page will have different critical css, with regards to pre-render, you might end up with different stylesheets for each page.
There are no docs about this, because while we allow customising your build we don't support it. You can take a look at https://www.npmjs.com/package/@angular-builders/custom-webpack#index-transform
Yeah indeed, that doesn't that's it's not a problem though. |
@naveedahmed1 we're looking into this with Chrome. Discussing pros and cons of different approaches. |
That's great, Thank you so much @mgechev for the update :) |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
🚀 Feature request
Command (mark with an
x
)Description
The size of CSS for a normal website could be from 100kb to few hundred KBs.
For better performance we should inline the critical css in the head of the html document and lazy load rest of the css through an external file.
Currently Angular CLI adds the css file to the head of the index.html, which makes it
render blocking
.Ideally we should have a solution like the one mentioned here https://dzone.com/articles/critical-css-and-webpack-automatically-minimize-re , so that during the build process Angular CLI automatically identify the css of the critical path, inline it in head of the
index.html
, and lazy load rest of the css usingpreload
e.g.:<link href="/style.96106fab.css" rel="preload" as="style" onload="this.rel='stylesheet'">
I am not sure how practical the solution described in this post is, but I think the least we could do is allow lazy loading of css e.g through an option in
angular.json
file that allow us to specify that this particular css file should be lazy loaded and when that option is enabled, Angular CLI should create below tag:<link href="/style.hash.css" rel="preload" as="style" onload="this.rel='stylesheet'">
instead of
<link rel="stylesheet" href="styles.hash.css">
assuming we have already inline the critical css in index.html.
This Webpack plugin might be relevant and helpful https://www.npmjs.com/package/preload-webpack-plugin
The text was updated successfully, but these errors were encountered: