简体   繁体   English

webp/jpg - 仅在 html 文件中预加载所需的类型

[英]webp/jpg - Preloading only the needed type in a html file

i have a website and i am using webp and jpg as a fallback.我有一个网站,我使用 webp 和 jpg 作为后备。 in the header, i have a bis image and smaller image for mobile users.在 header 中,我为移动用户提供了 bis 图像和较小的图像。

So i have 4 files:所以我有4个文件:

header-big.webp
header-small.webp
header-big.jpg
header-small.jpg

Because it is in the header, i want wo preload the image, but only the image i need.因为它在 header 中,所以我想要预加载图像,但只需要我需要的图像。 for the small and big ones i can preload it with the media-attribute.对于小型和大型的,我可以使用媒体属性预加载它。

<link rel="preload" href="header-small.jpg" as="image" type="image/jpg" media="(max-width: 575px)">
<link rel="preload" href="header-small.webp" as="image" type="image/webp" media="(max-width: 575px)">
<link rel="preload" href="header-big.jpg" as="image" type="image/jpg" media="(min-width: 576px)">
<link rel="preload" href="header-big.webp" as="image" type="image/webp" media="(min-width: 576px)">

In this case, the browser always preloads two files, depending on its width, but still just one of them will be used.在这种情况下,浏览器总是根据文件的宽度预加载两个文件,但仍然只会使用其中一个。

and jeah, it makes sense, because the jpg and webp can be both implemented.天哪,这是有道理的,因为 jpg 和 webp 都可以实现。 so of course the browser preload both.所以当然浏览器预加载两者。

but can i say "if you support webp, than preload the webp and do not preload the jpg"?但我可以说“如果你支持 webp,那么预加载 webp 而不要预加载 jpg”?

Thanks, Florian谢谢,弗洛里安

The solution I implemented involves a small script taken from https://avif.io/blog/tutorials/use-avif-in-css/ to detect AVIF and WEBP, because I already needed it afterwards to add CSS support for both formats, but for your use case can be a bit simplified.我实施的解决方案涉及一个小脚本,取自https://avif.io/blog/tutorials/use-avif-in-css/来检测 AVIF 和 WEBP,因为我之后已经需要它来添加 CSS 对这两种格式的支持,但是对于您的用例可以稍微简化一些。 Once I know it is supported I add a preload link in the head.一旦我知道它受支持,我就会在头部添加一个preload链接。

I placed the script at the very end of the head, coz in my particular case I don't need to have image earlier.我将脚本放在头部的最后,因为在我的特殊情况下我不需要更早的图像。

<script type="text/javascript">
  function addPreloadLink(img, type) {
    var fileref = document.createElement('link');
    fileref.setAttribute('rel', 'preload');
    fileref.setAttribute('as', 'image');
    fileref.setAttribute('href', img);
    fileref.setAttribute('type', type);

    document.head.appendChild(fileref);
  }

  function AddClass(c) {
    document.documentElement.classList.add(c);
  }

  var webp = new Image();
  webp.src =
    'data:image/webp;base64,UklGRhoAAABXRUJQVlA4TA0AAAAvAAAAEAcQERGIiP4HAA==';
  webp.onload = function() {
    AddClass('webp');
    addPreloadLink('header-small.webp', 'image/webp');
    addPreloadLink('header-big.webp', 'image/webp');
  };
  webp.onerror = function() {
    //load JPG
    addPreloadLink('header-small.jpg', 'image/jpg');
    addPreloadLink('header-big.jpg', 'image/jpg');
  };
</script>

So, I came to this question because I'm trying to improve LCP for the latest google search signal update.所以,我来这个问题是因为我正在尝试改进 LCP 以适应最新的谷歌搜索信号更新。 I too wanted to preload webp when supported and preload jpg when not.我也想在支持时预加载 webp,在不支持时预加载 jpg。

For the case of preloading jpg when webp is not supported, this could only occur when the browser supports preload and not webp.对于不支持 webp 时预加载 jpg 的情况,只有在浏览器支持预加载而不支持 webp 时才会出现这种情况。 When I looked at https://caniuse.com/webp and compared it to https://caniuse.com/link-rel-preload to determine how big that area of intersection is, I noticed there aren't a whole lot of browser versions that support preload and not webp, mostly safari.当我查看https://caniuse.com/webp并将其与https://caniuse.com/link-rel-preload进行比较以确定该交叉区域有多大时,我注意到没有很多支持 preload 而不是 webp 的浏览器版本,主要是 safari。 I've summarized that intersection in the table below.我在下表中总结了那个交叉点。 The webp and preload columns show the first version with sufficient support for webp and for the link element and preload respectively. webp 和 preload 列显示了第一个版本,分别对 webp 和链接元素和预加载有足够的支持。 The version gap column shows which versions fall into that 'supports preload but not webp' category and the % users in gap, shows what percentage of global users fall into that category and would benefit from preloaded jpegs.版本差距列显示了哪些版本属于“支持预加载但不支持 webp”类别和差距中的用户百分比,显示了属于该类别的全球用户的百分比,并将从预加载的 jpeg 中受益。 The % were aggregated from https://caniuse.com/usage-table % 来自https://caniuse.com/usage-table

Browser浏览器 WebP WebP Preload预载 Version gap版本差距 % users in gap差距中的用户百分比
IE IE NA不适用 NA不适用
Edge边缘 18 18 17 17 [17-18) [17-18) 0.03% 0.03%
FireFox FireFox 65 65 85 (56*, 57-84 disabled by default) 85 (56*, 57-84 默认禁用) 56 56 0.01% 0.01%
Chrome铬合金 32 32 50 50
Safari Safari 14* 14* 11.1 11.1 [11.1-14) [11.1-14) 0.65% 0.65%
Opera歌剧 19 19 37 37
Safari(iOS) Safari(iOS) 14.4 14.4 11.3 11.3 [11.3-14.4) [11.3-14.4) 1.24% 1.24%
Opera Mini歌剧迷你 all全部 NA不适用
Android Browser Android浏览器 4.2 4.2 91 91
Opera Mobile歌剧移动 62 62 NA不适用
Chrome for Android镀铬 Android 91 91 91 91
Firefox for Android Firefox 用于 Android 89 89 89 89
UC for Android UC 用于 Android 12.12 12.12 NA不适用
Samsung Internet三星互联网 4 4 5 5
QQ Browser QQ浏览器 10.4 10.4 NA不适用
Baidu Browser百度浏览器 7.12 7.12 NA不适用
KaiOS凯iOS NA不适用 NA不适用
TOTAL全部的 1.93% 1.93%

So, approximately 1.93% globally and my hunch is that if your audience is mostly US based, it's probably less than that, under the assumption that more people in the 'developed' world are on the latest hardware and browsers than elsewhere.因此,全球大约有 1.93%,我的直觉是,如果您的受众主要是美国,那么可能会低于这个数字,假设“发达”世界中使用最新硬件和浏览器的人比其他地方多。 I also expect that number to be declining asymptotically towards zero as time marches forward and that users who are on those older browsers are more likely to be on slower connections as well.我还预计随着时间的推移,这个数字会逐渐下降到零,并且使用这些旧浏览器的用户也更有可能使用较慢的连接。

My assessment from this analysis is that it's probably not worth it to try and do any preload of jpegs if you have webp assets available, because the users who would benefit are a) a teeeny sliver of the total and b) probably harder to optimize for in general, with diminishing returns as the users sunset those browser versions.我对这个分析的评估是,如果你有可用的 webp 资产,尝试做任何 jpeg 预加载可能不值得,因为受益的用户是 a) 总数中的一小部分 b) 可能更难优化一般来说,随着用户停用这些浏览器版本,收益会递减。

If you're like me and your goal is to move enough users from 'bad' and 'needs improving' scores on Core Web Vitals into the 'good' range to meet the 75% of users threshold that google has indicated will get you some kind of special indication of 'fast site' in the search results, then you can probably not worry about this cohort.如果您像我一样,并且您的目标是将足够多的用户从核心 Web Vitals 上的“差”和“需要改进”分数转移到“好”范围内,以达到 75% 的用户阈值,谷歌表示这将为您带来一些在搜索结果中有一种“快速站点”的特殊指示,那么您可能不必担心这个队列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM