Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 新增ServiceWorker #16

Merged
merged 6 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ firework:
# Experimental, may have a lot of bugs, open with caution!
pjax:
enable: false

serviceworker:
enable: true
# Dependent cdn links
vendor:
js:
Expand Down
3 changes: 3 additions & 0 deletions layout/_partial/head.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,7 @@
}).init();
</script>
<% } %>
<% if (theme.serviceworker.enable){ %>
<script src="/sw.js"></script>
<% } %>
</head>
12 changes: 12 additions & 0 deletions scripts/generator/servicework.js
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,
}
})
69 changes: 69 additions & 0 deletions source/js/sw.js
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);
});
}