在JavaScript中,缓存(Cache)是一种存储机制,用于临时存储数据,以便在未来请求时能够更快地获取这些数据,而不是重新从原始来源(如服务器或本地文件)获取。缓存可以显著提高应用的性能和响应速度。
Cache-Control
、Expires
、ETag
等。Cache-Control: public, max-age=3600
Expires: Thu, 01 Dec 2023 16:00:00 GMT
ETag: "33a64df551425fcc55e4d42a148795d9f25f89d4"
self.addEventListener('install', event => {
event.waitUntil(
caches.open('my-cache').then(cache => {
return cache.addAll([
'/',
'/index.html',
'/styles.css',
'/app.js'
]);
})
);
});
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request).then(response => {
return response || fetch(event.request);
})
);
});
通过合理使用缓存,可以显著提升Web应用的性能和用户体验。
领取专属 10元无门槛券
手把手带您无忧上云