我想缓存来自使用策略 token 作为 URL 参数的安全 CDN 的资源。
例如:www.cdn.com/image.png?Policy=AAAAA&Key-Pair-Id=BBBBB
如果我重新访问该站点,即使我有不同的策略 token 和 key 对 ID,我也想从缓存中获取 Assets 。
例如:www.cdn.com/image.png?Policy=CCCCC&Key-Pair-Id=DDDDD
如果我在服务工作人员中使用此代码:
workbox.routing.registerRoute(
/^(http(s)?:)?\/\/www\.cdn\.com.*/,
workbox.strategies.staleWhileRevalidate()
);
它在缓存中找不到响应并转到网络。
我希望它通过不带 URL 参数的 URL 进行匹配(即使 Policy=CCCCC&Key-Pair-Id=DDDDD 实际上不是有效的策略)。只需查看 www.cdn.com/image.png
是否存在并检索它。
最佳答案
我通过使用自定义处理程序找到了解决方案:
workbox.routing.registerRoute(
/^(http(s)?:)?\/\/www\.cdn\.com.*/,
({url, event}) => {
return caches.open(`${prefix}-${runtime}-${suffix}`).then((cache) => {
const customRequest = `${url.origin}${url.pathname}`;
return cache.match(customRequest).then((cacheRes) => {
if (cacheRes) {
return cacheRes;
}
return fetch(event.request).then((fetchRes) => {
cache.put(customRequest, fetchRes.clone());
return fetchRes;
});
});
});
}
);
关于javascript - workbox 3 - 忽略运行时缓存的 URL 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47962341/