Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
嗯菜
NeteaseCloudMusicApi
提交
b3ac7e9e
N
NeteaseCloudMusicApi
项目概览
嗯菜
/
NeteaseCloudMusicApi
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
N
NeteaseCloudMusicApi
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
b3ac7e9e
编写于
4月 01, 2017
作者:
B
binaryify
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add serviceWorker
上级
a2e4d264
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
88 addition
and
0 deletion
+88
-0
docs/index.html
docs/index.html
+5
-0
docs/sw.js
docs/sw.js
+83
-0
未找到文件。
docs/index.html
浏览文件 @
b3ac7e9e
...
...
@@ -17,4 +17,9 @@
}
</script>
<script
src=
"//unpkg.com/docsify/lib/docsify.min.js"
></script>
<script>
if
(
typeof
navigator
.
serviceWorker
!==
'
undefined
'
)
{
navigator
.
serviceWorker
.
register
(
'
sw.js
'
)
}
</script>
</html>
docs/sw.js
0 → 100644
浏览文件 @
b3ac7e9e
/* ===========================================================
* docsify sw.js
* ===========================================================
* Copyright 2016 @huxpro
* Licensed under Apache 2.0
* Register service worker.
* ========================================================== */
const
RUNTIME
=
'
docsify
'
const
HOSTNAME_WHITELIST
=
[
self
.
location
.
hostname
,
'
fonts.gstatic.com
'
,
'
fonts.googleapis.com
'
,
'
unpkg.com
'
]
// The Util Function to hack URLs of intercepted requests
const
getFixedUrl
=
(
req
)
=>
{
var
now
=
Date
.
now
()
var
url
=
new
URL
(
req
.
url
)
// 1. fixed http URL
// Just keep syncing with location.protocol
// fetch(httpURL) belongs to active mixed content.
// And fetch(httpRequest) is not supported yet.
url
.
protocol
=
self
.
location
.
protocol
// 2. add query for caching-busting.
// Github Pages served with Cache-Control: max-age=600
// max-age on mutable content is error-prone, with SW life of bugs can even extend.
// Until cache mode of Fetch API landed, we have to workaround cache-busting with query string.
// Cache-Control-Bug: https://bugs.chromium.org/p/chromium/issues/detail?id=453190
if
(
url
.
hostname
===
self
.
location
.
hostname
)
{
url
.
search
+=
(
url
.
search
?
'
&
'
:
'
?
'
)
+
'
cache-bust=
'
+
now
}
return
url
.
href
}
/**
* @Lifecycle Activate
* New one activated when old isnt being used.
*
* waitUntil(): activating ====> activated
*/
self
.
addEventListener
(
'
activate
'
,
event
=>
{
event
.
waitUntil
(
self
.
clients
.
claim
())
})
/**
* @Functional Fetch
* All network requests are being intercepted here.
*
* void respondWith(Promise<Response> r)
*/
self
.
addEventListener
(
'
fetch
'
,
event
=>
{
// Skip some of cross-origin requests, like those for Google Analytics.
if
(
HOSTNAME_WHITELIST
.
indexOf
(
new
URL
(
event
.
request
.
url
).
hostname
)
>
-
1
)
{
// Stale-while-revalidate
// similar to HTTP's stale-while-revalidate: https://www.mnot.net/blog/2007/12/12/stale
// Upgrade from Jake's to Surma's: https://gist.github.com/surma/eb441223daaedf880801ad80006389f1
const
cached
=
caches
.
match
(
event
.
request
)
const
fixedUrl
=
getFixedUrl
(
event
.
request
)
const
fetched
=
fetch
(
fixedUrl
,
{
cache
:
'
no-store
'
})
const
fetchedCopy
=
fetched
.
then
(
resp
=>
resp
.
clone
())
// Call respondWith() with whatever we get first.
// If the fetch fails (e.g disconnected), wait for the cache.
// If there’s nothing in cache, wait for the fetch.
// If neither yields a response, return offline pages.
event
.
respondWith
(
Promise
.
race
([
fetched
.
catch
(
_
=>
cached
),
cached
])
.
then
(
resp
=>
resp
||
fetched
)
.
catch
(
_
=>
{
/* eat any errors */
})
)
// Update the cache with the version we fetched (only for ok status)
event
.
waitUntil
(
Promise
.
all
([
fetchedCopy
,
caches
.
open
(
RUNTIME
)])
.
then
(([
response
,
cache
])
=>
response
.
ok
&&
cache
.
put
(
event
.
request
,
response
))
.
catch
(
_
=>
{
/* eat any errors */
})
)
}
})
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录