html-minifier icon indicating copy to clipboard operation
html-minifier copied to clipboard

Feature Request: Support to convert css link tags to async/preload tags

Open naveedahmed1 opened this issue 5 years ago • 13 comments

This plugin does a great job.

Here's a feature request to please add support to convert css link tags to async/preload tags

for example convert

<link rel="stylesheet" href="styles.117bbb465e9c23da0dcf.css">

to

<link rel="stylesheet" href="styles.117bbb465e9c23da0dcf.css" media="print" onload='this.media="all",this.onload=null'>

naveedahmed1 avatar May 25 '20 21:05 naveedahmed1

I think this is out of scope of this library which does only and should only do compression.

DanielRuf avatar Jun 06 '20 10:06 DanielRuf

onload='this.media="all",this.onload=null'

This is not really how async / preload natively works. I guess you mean the async attribute and rel="preload".

DanielRuf avatar Jun 06 '20 10:06 DanielRuf

Thank you so much @DanielRuf fro your response.

I am actually referring to a solution which is used to eliminate render blocking stylesheets. As mentioned here https://stackoverflow.com/a/46750893/2755616 .

I know its primarily a html minification plugin, but this could be an added feature, maybe available behind some flag, so that users can enable by passing a parameter e.g asyncCSS=true when desired.

naveedahmed1 avatar Jun 06 '20 12:06 naveedahmed1

I think this is out of scope of this library which does only and should only do compression.

I still think it's nothing that should be done by this library. There are probably already other solutions (plugins for gulp, webpack, ...) which do this.

DanielRuf avatar Jun 06 '20 12:06 DanielRuf

The library is about saving bytes / compressing your input, not performance improvements by adding code.

DanielRuf avatar Jun 06 '20 12:06 DanielRuf

Ok, can you please point out any which can be run through command line? Thanks

naveedahmed1 avatar Jun 06 '20 12:06 naveedahmed1

And also see https://caniuse.com/#feat=link-rel-preload and https://caniuse.com/#feat=script-async

https://www.npmjs.com/search?q=async%20stylesheet https://github.com/search?q=async+stylesheet https://github.com/devpreview/async-stylesheet-webpack-plugin

But personally loading styles asynchronically makes not much sense in most cases (FOUT, FOIC, ...).

I guess you can use any regex / search&replace solution.

DanielRuf avatar Jun 06 '20 12:06 DanielRuf

Thank you so much @DanielRuf for sharing these, really appreciate :)

But personally loading styles asynchronically makes not much sense in most cases (FOUT, FOIC, ...).

Yes I agree, but it could be useful when the css for the critical path and above the fold item is already inlined in the html file.

In my case its an Angular app, and during the build process the Angular CLI automatically generates the filename for style sheet and adds it to the index.html. What I want is after the Angular CLI has generated the output, I run some command line npm plugin which converts stylesheet tag to async tag as mentioned above.

naveedahmed1 avatar Jun 06 '20 12:06 naveedahmed1

https://www.npmjs.com/package/inline-critical has a CLI that might cover your usecase.

DanielRuf avatar Jun 06 '20 12:06 DanielRuf

Thanks again @DanielRuf .

I just tried this, but it has two issues:

  1. It requires css file name in advance
  2. It prints html output on console instead of updating the html file.
  3. I tried it with hardcoded files(html and css) like inline-critical dist/index.html dist/styles.3818ec537453af2e8d93.css -p, but the html printed on console contains empty <head> tag.

I will explore it further, and search for other plugins as well.

I know its not appropriate to do these conversation on github issues .

Thank you once again for your time :)

naveedahmed1 avatar Jun 06 '20 13:06 naveedahmed1

It prints html output on console instead of updating the html file.

See the readme. You want to pipe it to a file using > file.html.

DanielRuf avatar Jun 06 '20 13:06 DanielRuf

I think it is better if you mention the issues at https://github.com/bezoerb/inline-critical/issues

DanielRuf avatar Jun 06 '20 13:06 DanielRuf

Thanks, I'll explore it again, if the issue persists I will report the issue.

naveedahmed1 avatar Jun 06 '20 14:06 naveedahmed1