-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from MliKiowa/main
feat: 新增ServiceWorker
- Loading branch information
Showing
4 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const fs = require('hexo-fs') | ||
let timeVersion = Date.now() | ||
|
||
hexo.extend.generator.register('ServiceWoker', function(locals){ | ||
let filePath = 'themes/reimu/source/js/sw.js' | ||
let content = fs.readFileSync(filePath) | ||
content = 'const VERSION = "'+ timeVersion +'";\n' + content | ||
return { | ||
path: "sw.js", | ||
data: content, | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const PreCache = [ | ||
'/images/taichi.png', | ||
'/images/banner.jpg', | ||
'/images/taichi-fill.png', | ||
'/css/loader.css', | ||
'/css/style.css', | ||
'/js/script.js' | ||
]; | ||
|
||
// 安装时预加载必要内容 | ||
self.addEventListener('install', (event) => { | ||
console.log(`Service Worker ${VERSION} installing.`); | ||
event.waitUntil( | ||
caches.open(VERSION).then((cache) => { | ||
return cache.addAll(PreCache); | ||
}) | ||
); | ||
}); | ||
|
||
self.addEventListener('fetch', function(event) { | ||
// 检查请求是否为 POST 或带有查询参数的 GET 这样可用避免错误缓存 | ||
if (event.request.method === 'POST' || (event.request.method === 'GET' && event.request.url.indexOf('?') !== -1)) { | ||
event.respondWith(fetch(event.request)); | ||
} else { | ||
event.respondWith( | ||
caches.match(event.request) | ||
.then(function(response) { | ||
if (response) { | ||
return response; | ||
} | ||
return fetch(event.request).then(function(responseToCache) { | ||
var responseToCacheClone = responseToCache.clone(); | ||
caches.open(VERSION).then(function(cache) { | ||
cache.put(event.request, responseToCacheClone); | ||
}); | ||
return responseToCache; | ||
}); | ||
} | ||
) | ||
); | ||
} | ||
}); | ||
|
||
|
||
self.addEventListener('activate', (event) => { | ||
event.waitUntil( | ||
caches.keys().then((cacheNames) => { | ||
return Promise.all( | ||
cacheNames.map((cacheName) => { | ||
if (VERSION !== cacheName) { | ||
console.log(`Service Worker: deleting old cache ${cacheName}`); | ||
return caches.delete(cacheName); | ||
} | ||
}) | ||
); | ||
}) | ||
); | ||
console.log(`Service Worker ${VERSION} activated.`); | ||
}); | ||
|
||
if ('serviceWorker' in navigator) { | ||
navigator.serviceWorker.register('/sw.js') | ||
.then((registration) => { | ||
console.log('Service Worker 注册成功: ', registration); | ||
}) | ||
.catch((error) => { | ||
console.log('Service Worker 注册失败: ', error); | ||
}); | ||
} |