问题
Google Pagespeed test doesn't recognize preload when it is used together with stylesheet. I tried
<link rel="preload stylesheet" href="http://www.example.com/style.css" />
and it still shows "Consider using to prioritize fetching resources that are currently requested later in page load.". If I remove stylesheet from rel attribute, it recognizes the preload.
I was thinking to try this:
<link rel="preload" rel="stylesheet" href="http://www.example.com/style.css" />
but I am not sure if you can have 2 the same attributes for the same link tag. Would this work?
回答1:
For loading CSS styles asynchronously, you should indicate it, as a preload stylesheet. Whenever you use rel="preload"
alone, it won't transform to stylesheet on page load and it will remain as preload
, so to indicate it as stylesheet you should use another attribute such as as
which indicate the type of element and in your case, it should be style
. Then you need to tell the browser whenever the loading process got finished you need to consider this as a stylesheet, so you have to define another attribute like onload
and then define its real relation.
so you have to import it like this:
<link rel="preload" as="style" onload="this.rel='stylesheet'" href="http://www.example.com/style.css">
NOTE: You can read more about it here in google developers.
UPDATE
Since preload
is not supported in firefox until now (according to this) browsers the only way to do it is to declare it twice in the one rel
tag or two separate tags.
<link rel="preload" as="style" href="http://www.example.com/style.css">
<link rel="stylesheet" href="http://www.example.com/style.css">
Or
<link rel="stylesheet" rel="preload" as="style" href="http://www.example.com/style.css">
NOTE: As @JohnyFree tested the second one (one with a line through) in the Google page speed, it won't be recognized as valid preload
style, whilst the format is valid according to W3.org.
来源:https://stackoverflow.com/questions/61889044/how-to-use-both-rel-preload-and-rel-stylesheet-for-the-same-tag