Skip to content
Permalink
Browse files

增加service Worker

  • Loading branch information...
firedent committed Mar 30, 2017
1 parent e5b050f commit 5847fa827c05f90d2da8671d89ea7bbc9f2ff9be
@@ -3,6 +3,7 @@
menu:
主页: /
日常: /tags/日常/
2017年计划: /2017/01/26/f03473fc/
BiLiBiLi弹幕解密器: https://bilibili.zscself.com/
网站地图: /sitemap.xml

@@ -30,11 +31,10 @@ subnav:
#whatsapp:
#instagram:
telegram: "https://telegram.me/DuDuLuDuDaDaDa"
rss: /feed.xml
mail: "/mail.png"
#gitlab:

rss: /atom.xml

# 是否需要修改 root 路径
# 如果您的网站存放在子目录中,例如 http://yoursite.com/blog,
# 请将您的 url 设为 http://yoursite.com/blog 并把 root 设为 /blog/。
@@ -58,13 +58,13 @@ reward_type: 1
# 打赏wording
reward_wording: '谢谢你请我吃糖果'
# 支付宝二维码图片地址,跟你设置头像的方式一样。比如:/assets/img/alipay.jpg
alipay: /images/donateMeByAlipay.jpg
alipay: /images/donateMeByAlipay.gif
# 微信二维码图片地址
# weixin:

# Miscellaneous
baidu_analytics: '1a20d4b1772a9c03105309df5ceef184'
google_analytics: ''
baidu_analytics: '10d5995ba60a44948e3bf579c6065e10'
google_analytics: 'UA-92400662-1'
favicon: /frog.png

#你的头像url
@@ -75,7 +75,7 @@ share_jia: false

#是否开启多说评论,填写你在多说申请的项目名称 duoshuo: duoshuo-key
#若使用disqus,请在博客config文件中填写disqus_shortname,并关闭多说评论
duoshuo: fir3d3nt
#duoshuo: fir3d3nt

# 样式定制 - 一般不需要修改,除非有很强的定制欲望…
style:
@@ -20,6 +20,8 @@
}
%>
<meta name="renderer" content="webkit">
<meta name="google-site-verification" content="QdBWEkIUbrn71b6RNgymst3D_GyjPoq0WKwFw5TDvXo" />
<meta name="msvalidate.01" content="D5A6C14046DE1F576A46B902A10E36D8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" >
<link rel="dns-prefetch" href="<%= config.url %>">
<title><% if (title){ %><%= title %> | <% } %><%= config.title %></title>

Large diffs are not rendered by default.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,171 @@
'use strict';

const version = 'v20160443';
const __DEVELOPMENT__ = false;
const __DEBUG__ = false;
const offlineResources = [
'/',
'/2017/03/30/3e16aa00/index.html',
'/favicon.ico'
];

const ignoreFetch = [

];


//////////
// Install
//////////
function onInstall(event) {
log('install event in progress.');

event.waitUntil(updateStaticCache());
}

function updateStaticCache() {
return caches
.open(cacheKey('offline'))
.then((cache) => {
return cache.addAll(offlineResources);
})
.then(() => {
log('installation complete!');
});
}

////////
// Fetch
////////
function onFetch(event) {
const request = event.request;

if (shouldAlwaysFetch(request)) {
event.respondWith(networkedOrOffline(request));
return;
}

if (shouldFetchAndCache(request)) {
event.respondWith(networkedOrCached(request));
return;
}

event.respondWith(cachedOrNetworked(request));
}

function networkedOrCached(request) {
return networkedAndCache(request)
.catch(() => { return cachedOrOffline(request) });
}

// Stash response in cache as side-effect of network request
function networkedAndCache(request) {
return fetch(request)
.then((response) => {
var copy = response.clone();
caches.open(cacheKey('resources'))
.then((cache) => {
cache.put(request, copy);
});

log("(network: cache write)", request.method, request.url);
return response;
});
}

function cachedOrNetworked(request) {
return caches.match(request)
.then((response) => {
log(response ? '(cached)' : '(network: cache miss)', request.method, request.url);
return response ||
networkedAndCache(request)
.catch(() => { return offlineResponse(request) });
});
}

function networkedOrOffline(request) {
return fetch(request)
.then((response) => {
log('(network)', request.method, request.url);
return response;
})
.catch(() => {
return offlineResponse(request);
});
}

function cachedOrOffline(request) {
return caches
.match(request)
.then((response) => {
return response || offlineResponse(request);
});
}

function offlineResponse(request) {
log('(offline)', request.method, request.url);
if (request.url.match(/\.(jpg|png|gif|svg|jpeg)(\?.*)?$/)) {
return caches.match('/favicon.ico');
} else {
return caches.match('/offline.html');
}
}

///////////
// Activate
///////////
function onActivate(event) {
log('activate event in progress.');
event.waitUntil(removeOldCache());
}

function removeOldCache() {
return caches
.keys()
.then((keys) => {
return Promise.all( // We return a promise that settles when all outdated caches are deleted.
keys
.filter((key) => {
return !key.startsWith(version); // Filter by keys that don't start with the latest version prefix.
})
.map((key) => {
return caches.delete(key); // Return a promise that's fulfilled when each outdated cache is deleted.
})
);
})
.then(() => {
log('removeOldCache completed.');
});
}

function cacheKey() {
return [version, ...arguments].join(':');
}

function log() {
if (developmentMode()) {
console.log("SW:", ...arguments);
}
}

function shouldAlwaysFetch(request) {
return __DEVELOPMENT__ ||
request.method !== 'GET' ||
ignoreFetch.some(regex => request.url.match(regex));
}

function shouldFetchAndCache(request) {
return ~request.headers.get('Accept').indexOf('text/html');
}

function developmentMode() {
return __DEVELOPMENT__ || __DEBUG__;
}

log("Hello from ServiceWorker land!", version);

self.addEventListener('install', onInstall);

self.addEventListener('fetch', onFetch);

self.addEventListener("activate", onActivate);
@@ -1,3 +1,4 @@

<script>
<% var left = '<%=' %>
<% var right = '%' %>
@@ -13,7 +14,12 @@
document.getElementsByTagName('body')[0].appendChild($script)
$script.setAttribute('src', path)
}
<% for (var asset in compilation.assets) { %><% if (asset.indexOf('slider') >= 0) { %><% var slider = asset %><% } %><% } %>loadScript("<%= left %>config.root<%= right %><%= right2 %><%= slider %>")
<% for (var asset in compilation.assets) { %><% if (asset.indexOf('slider') >= 0) { %><% var slider = asset %><% } %><% if (asset.indexOf('serviceworker') >= 0) { %><% var serviceworker = asset %><% } %><% } %>
loadScript("<%= left %>config.root<%= right %><%= right2 %><%= slider %>")
if ('serviceWorker' in navigator) {
var $script = document.createElement('script')
document.getElementsByTagName('body')[0].appendChild($script)
$script.innerHTML = 'navigator.serviceWorker.register("<%= left %>config.root<%= right %><%= right2 %><%= serviceworker %>");'
}
})();
</script>

Oops, something went wrong.

0 comments on commit 5847fa8

Please sign in to comment.
You can’t perform that action at this time.