Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wushizhenking
JavaGuide
提交
36b025c6
J
JavaGuide
项目概览
wushizhenking
/
JavaGuide
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
JavaGuide
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
36b025c6
编写于
3月 08, 2020
作者:
S
shuang.kou
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[feat]add pwa(离线模式)支持
上级
a4d4c13c
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
90 addition
and
0 deletion
+90
-0
index.html
index.html
+7
-0
sw.js
sw.js
+83
-0
未找到文件。
index.html
浏览文件 @
36b025c6
...
...
@@ -17,6 +17,11 @@
<!-- docsify-edit-on-github -->
<script
src=
"//unpkg.com/docsify-edit-on-github/index.js"
></script>
<script>
// 离线支持
if
(
typeof
navigator
.
serviceWorker
!==
'
undefined
'
)
{
navigator
.
serviceWorker
.
register
(
'
sw.js
'
)
}
window
.
$docsify
=
{
name
:
'
JavaGuide
'
,
repo
:
'
https://github.com/Snailclimb/JavaGuide
'
,
...
...
@@ -50,6 +55,8 @@
-->
<!-- 复制到剪贴板 -->
<script
src=
"//unpkg.com/docsify-copy-code"
></script>
<!-- 图片缩放 -->
<script
src=
"//unpkg.com/docsify/lib/plugins/zoom-image.js"
></script>
</body>
</html>
\ No newline at end of file
sw.js
0 → 100644
浏览文件 @
36b025c6
/* ===========================================================
* 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 */
})
)
}
})
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录