gpt4 book ai didi

javascript - 服务人员 : How to cache images that do not have extensions using Workbox

转载 作者:行者123 更新时间:2023-12-02 23:48:51 28 4
gpt4 key购买 nike

我是 Service Worker 的新手,在我的页面中,我有没有扩展名的图像[*.jpg、*.png 等],如下所示“www.domain.com/api/media/a2b93f21-1acf-4e5e-9b19-6d7c68aaadc2”,我从 API 获取它们。

以下代码可以正常工作,但不适用于此类图像

workbox.routing.registerRoute(
// Cache image files.
/\.(?:png|jpg|jpeg|svg|gif)$/,
// Use the cache if it's available.
new workbox.strategies.CacheFirst({
// Use a custom cache name.
cacheName: 'image-cache',
plugins: [
new workbox.expiration.Plugin({
// Cache only 20 images.
maxEntries: 20,
// Cache for a maximum of a week.
maxAgeSeconds: 7 * 24 * 60 * 60,
})
],
})
);

有什么建议吗?

最佳答案

带有工作箱,来自manual -

You can use the RequestDestination enumerate type of the destination of the request to determine a strategy. For example, when the target is data

:

workbox.routing.registerRoute(
// Custom `matchCallback` function
({event}) => event.request.destination === 'image',
new workbox.strategies.CacheFirst({
cacheName: 'image',
plugins: [
new workbox.expiration.Plugin({
maxEntries: 20,
maxAgeSeconds: 7 * 24 * 60 * 60, // 1 week
}),
],
})
);

在普通的 Service Worker 中,您可以检查 request accept header

if (request.headers.get('Accept').indexOf('image') !== -1) { 
... stash in cache ...
}

关于javascript - 服务人员 : How to cache images that do not have extensions using Workbox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55726483/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com