gpt4 book ai didi

javascript - 当缓存优先策略中的内容发生变化时,网站不会更新

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:43:32 25 4
gpt4 key购买 nike

我在我想要支持离线浏览的渐进式网络应用程序中使用缓存优先策略。我注意到离线浏览工作正常但是当我更新网站上的内容时,它仍然显示旧的东西。我不确定我的代码有什么问题因为我希望它在加载离线之前检查是否有更新内容。我有 ma​​nifest.jsonService-worker.jsOfflinepage.jsma​​in.js

这是我使用的service-worker.js代码:

      //service worker configuration
'use strict';

const
version = '1.0.0',
CACHE = version + '::PWA',
offlineURL = '/offline/',
installFilesEssential = [
'/',
'/manifest.json',
'/theme/pizza/css/style.css',
'/theme/pizza/css/font-awesome/font-awesome.css',
'/theme/pizza/javascript/script.js',
'/theme/pizza/javascript/offlinepage.js',
'/theme/pizza/logo.png',
'/theme/pizza/icon.png'
].concat(offlineURL),
installFilesDesirable = [
'/favicon.ico',
'/theme/pizza/logo.png',
'/theme/pizza/icon.png'
];

// install static assets
function installStaticFiles() {

return caches.open(CACHE)
.then(cache => {

// cache desirable files
cache.addAll(installFilesDesirable);

// cache essential files
return cache.addAll(installFilesEssential);

});

}
// clear old caches
function clearOldCaches() {

return caches.keys()
.then(keylist => {

return Promise.all(
keylist
.filter(key => key !== CACHE)
.map(key => caches.delete(key))
);

});

}

// application installation
self.addEventListener('install', event => {

console.log('service worker: install');

// cache core files
event.waitUntil(
installStaticFiles()
.then(() => self.skipWaiting())
);

});

// application activated
self.addEventListener('activate', event => {

console.log('service worker: activate');

// delete old caches
event.waitUntil(
clearOldCaches()
.then(() => self.clients.claim())
);

});

// is image URL?
let iExt = ['png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp'].map(f => '.' + f);
function isImage(url) {

return iExt.reduce((ret, ext) => ret || url.endsWith(ext), false);

}


// return offline asset
function offlineAsset(url) {

if (isImage(url)) {

// return image
return new Response(
'<svg role="img" viewBox="0 0 400 300" xmlns="http://www.w3.org/2000/svg"><title>offline</title><path d="M0 0h400v300H0z" fill="#eee" /><text x="200" y="150" text-anchor="middle" dominant-baseline="middle" font-family="sans-serif" font-size="50" fill="#ccc">offline</text></svg>',
{ headers: {
'Content-Type': 'image/svg+xml',
'Cache-Control': 'no-store'
}}
);

}
else {

// return page
return caches.match(offlineURL);

}

}

// application fetch network data
self.addEventListener('fetch', event => {

// abandon non-GET requests
if (event.request.method !== 'GET') return;

let url = event.request.url;

event.respondWith(

caches.open(CACHE)
.then(cache => {

return cache.match(event.request)
.then(response => {

if (response) {
// return cached file
console.log('cache fetch: ' + url);
return response;
}

// make network request
return fetch(event.request)
.then(newreq => {

console.log('network fetch: ' + url);
if (newreq.ok) cache.put(event.request, newreq.clone());
return newreq;

})
// app is offline
.catch(() => offlineAsset(url));

});

})

);

});

最佳答案

将 ?[VERSION] 添加到链接的 src。

例如:

<script type="text/javascript" src="your_file.js?1500"></script>

每次更新代码时,只需在版本中添加一个数字即可。

实际上这是重复的问题 look here for other解决方案。

关于javascript - 当缓存优先策略中的内容发生变化时,网站不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44546556/

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