当前位置:网站首页>Service Workers让网站动态加载Webp图片
Service Workers让网站动态加载Webp图片
2022-07-28 11:08:00 【安语未】
Service Workers加载webp图片
每个图片加载请求可以通过accept获取是否支持webp格式,例如如下图

利用这一点,我们可以判断支持webp图片,就使用webp图片。我们需要注册一个Service Worker。Service Worker的一大特性就是,它们能够拦截网络请求,这样子,我们就能够完全控制响应内容。使用这个特性,我们能够监听HTTP头部,并决定如何做。
首先我们注册service worker
<script>
// Register the service worker
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/service-worker.js').then(function(registration) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}).catch(function(err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
}
</script>在上面的代码中,我们做了一个简单的检查,判断浏览器
边栏推荐
- ripro9.0修正升级版+WP两款美化包+稀有插件
- 万字详解 Google Play 上架应用标准包格式 AAB
- [极客大挑战 2019]BabySQL-1|SQL注入
- WinForm generates random verification code
- Software testing and quality learning notes 1 --- black box testing
- Cvpr2020 best paper: unsupervised learning of symmetric deformable 3D objects
- Minikube initial experience environment construction
- LabVIEW AI视觉工具包(非NI Vision)下载与安装教程
- Database advanced learning notes - storage structure
- 融云 IM & RTC 能力上新盘点
猜你喜欢

zotero文献管理器及其使用姿势(不定时更新)

Introduction to the usage of SAP ui5 image display control avatar trial version

What kind of knowledge payment system functions are more conducive to the development of the platform and lecturers?

PKG packaging node project

ripro9.0修正升级版+WP两款美化包+稀有插件

Byte side: how to realize reliable transmission with UDP?

万字详解 Google Play 上架应用标准包格式 AAB

Xiaoshuidi 2.0 website navigation network template
![[极客大挑战 2019]BabySQL-1|SQL注入](/img/21/b5b4727178a585e610d743e92248f7.png)
[极客大挑战 2019]BabySQL-1|SQL注入

天狼星网络验证源码/官方正版/内附搭建教程
随机推荐
Summary of common RSA related problems in CTF: basic RSA encryption and decryption
Flash point list of cross platform efficiency software
Several reincarnation stories
[cesium] entity property and timing binding: the sampledproperty method is simple to use
Why does acid food hurt teeth + early periodontitis
Cvpr2021 pedestrian re identification /person re identification paper + summary of open source code
[极客大挑战 2019]BabySQL-1|SQL注入
R language uses LM function to build regression model and regression model for transformed data (for example, it is necessary to build regression model for X and y, but they have no linear relationshi
Router firmware decryption idea
一文看懂设备指纹如何防篡改、防劫持
Postgres overview
Unity鼠标带动物体运动的三种方法
echo -ne(echo line)
【一知半解】零值拷贝
Introduction to web security RADIUS protocol application
MySQL离线同步到odps的时候 可以配置动态分区吗
[general database integrated development environment] Shanghai daoning provides you with Aqua Data Studio downloads, tutorials, and trials
A hundred flowers bloom in data analysis engines. Why invest heavily in Clickhouse?
Autumn recruit offer harvesters, and take the offers of major manufacturers at will
B2 sub theme / blog b2child sub theme / open source code