fix: a bug that files can't be updated
This commit is contained in:
parent
fd634988af
commit
c7983adfc2
|
@ -305,6 +305,7 @@ async function genCode() {
|
||||||
var HASH = '${hashStr}'
|
var HASH = '${hashStr}'
|
||||||
var URLS = ['${urlsStr}']
|
var URLS = ['${urlsStr}']
|
||||||
var PRIVACY = ${privacy}
|
var PRIVACY = ${privacy}
|
||||||
|
var UPDATE_TIMER = 120
|
||||||
|
|
||||||
${tmplCode}
|
${tmplCode}
|
||||||
`
|
`
|
||||||
|
|
|
@ -2,17 +2,13 @@ function pageEnv() {
|
||||||
var container = document.documentElement
|
var container = document.documentElement
|
||||||
|
|
||||||
function fallback(html) {
|
function fallback(html) {
|
||||||
var noscript = document.getElementsByTagName('noscript')
|
var noscripts = document.getElementsByTagName('noscript')
|
||||||
if (noscript.length > 0) {
|
if (noscripts.length > 0) {
|
||||||
html = noscript[0].innerHTML
|
html = noscripts[0].innerHTML
|
||||||
}
|
}
|
||||||
container.innerHTML = html
|
container.innerHTML = html
|
||||||
}
|
}
|
||||||
|
|
||||||
function reload() {
|
|
||||||
location.reload()
|
|
||||||
}
|
|
||||||
|
|
||||||
var currentScript = document.currentScript
|
var currentScript = document.currentScript
|
||||||
var jsUrl = currentScript.src
|
var jsUrl = currentScript.src
|
||||||
var rootPath
|
var rootPath
|
||||||
|
@ -31,6 +27,29 @@ function pageEnv() {
|
||||||
rootPath = currentScript.dataset.root
|
rootPath = currentScript.dataset.root
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function unpackToCache(bytes, cache) {
|
||||||
|
var info = JSON.stringify({
|
||||||
|
hash: HASH,
|
||||||
|
time: Date.now()
|
||||||
|
})
|
||||||
|
var res = new Response(info)
|
||||||
|
var pendings = [
|
||||||
|
cache.put(rootPath + '.cache-info', res),
|
||||||
|
swPending,
|
||||||
|
]
|
||||||
|
var pathResMap = unpack(bytes)
|
||||||
|
|
||||||
|
for (var path in pathResMap) {
|
||||||
|
res = pathResMap[path]
|
||||||
|
pendings.push(
|
||||||
|
cache.put(rootPath + path, res)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Promise.all(pendings).then(function() {
|
||||||
|
location.reload()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
function parseImgBuf(buf) {
|
function parseImgBuf(buf) {
|
||||||
if (!buf) {
|
if (!buf) {
|
||||||
loadNextUrl()
|
loadNextUrl()
|
||||||
|
@ -44,16 +63,9 @@ function pageEnv() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var bytes = decode1Px3Bytes(buf)
|
var bytes = decode1Px3Bytes(buf)
|
||||||
|
|
||||||
caches.delete('.web2img').then(function() {
|
caches.delete('.web2img').then(function() {
|
||||||
caches.open('.web2img').then(function(cache) {
|
caches.open('.web2img').then(function(cache) {
|
||||||
unpack(bytes, cache).then(function() {
|
unpackToCache(bytes, cache)
|
||||||
if (swPending) {
|
|
||||||
swPending.then(reload)
|
|
||||||
} else {
|
|
||||||
reload()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -97,13 +109,13 @@ function pageEnv() {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (PRIVACY === 2) {
|
if (PRIVACY === 2) {
|
||||||
// hide origin header
|
// hide `origin` header
|
||||||
var iframe = document.createElement('iframe')
|
var iframe = document.createElement('iframe')
|
||||||
|
|
||||||
if (typeof RELEASE !== 'undefined') {
|
if (typeof RELEASE !== 'undefined') {
|
||||||
iframe.src = 'data:text/html,<script>onmessage=' + loadImg + '</script>'
|
iframe.src = 'data:text/html,<script>onmessage=' + loadImg + '<\/script>'
|
||||||
} else {
|
} else {
|
||||||
iframe.src = 'data:text/html;base64,' + btoa('<script>onmessage=' + loadImg + '</script>')
|
iframe.src = 'data:text/html;base64,' + btoa('<script>onmessage=' + loadImg + '<\/script>')
|
||||||
}
|
}
|
||||||
iframe.style.display = 'none'
|
iframe.style.display = 'none'
|
||||||
iframe.onload = loadNextUrl
|
iframe.onload = loadNextUrl
|
||||||
|
@ -145,60 +157,74 @@ function pageEnv() {
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
function unpack(bytes, cache) {
|
function unpack(bytes) {
|
||||||
var confEnd = bytes.indexOf(13) // '\r'
|
var confEnd = bytes.indexOf(13) // '\r'
|
||||||
var confBin = bytes.subarray(0, confEnd)
|
var confBin = bytes.subarray(0, confEnd)
|
||||||
var confStr = new TextDecoder().decode(confBin)
|
var confStr = new TextDecoder().decode(confBin)
|
||||||
var confObj = JSON.parse(confStr)
|
var confObj = JSON.parse(confStr)
|
||||||
|
|
||||||
var offset = confEnd + 1
|
var offset = confEnd + 1
|
||||||
var pendings = []
|
|
||||||
|
|
||||||
for (var file in confObj) {
|
for (var path in confObj) {
|
||||||
var headers = confObj[file]
|
var headers = confObj[path]
|
||||||
headers['cache-control'] = 'max-age=60'
|
var expires = /\.html$/.test(path) ? 5 : UPDATE_TIMER
|
||||||
|
headers['cache-control'] = 'max-age=' + expires
|
||||||
|
|
||||||
var len = headers['content-length']
|
var len = headers['content-length']
|
||||||
var bin = bytes.subarray(offset, offset + len)
|
var bin = bytes.subarray(offset, offset + len)
|
||||||
var req = new Request(rootPath + file)
|
|
||||||
var res = new Response(bin, {
|
confObj[path] = new Response(bin, {
|
||||||
headers: headers
|
headers: headers
|
||||||
})
|
})
|
||||||
pendings.push(
|
|
||||||
cache.put(req, res)
|
|
||||||
)
|
|
||||||
offset += len
|
offset += len
|
||||||
}
|
}
|
||||||
return Promise.all(pendings)
|
return confObj
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function swEnv() {
|
function swEnv() {
|
||||||
var jsUrl = location.href
|
var jsUrl = location.href
|
||||||
var rootPath = getRootPath(jsUrl)
|
var rootPath = getRootPath(jsUrl)
|
||||||
var hasUpdate
|
var isFirst = 1
|
||||||
var currJs
|
var newJs
|
||||||
|
|
||||||
// check update
|
function openFile(path) {
|
||||||
setInterval(function() {
|
return caches.open('.web2img').then(function(cache) {
|
||||||
|
return cache.match(path)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkUpdate() {
|
||||||
|
openFile(rootPath + '.cache-info').then(function(res) {
|
||||||
|
if (!res) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
res.json().then(function(info) {
|
||||||
|
if (Date.now() - info.time < 1000 * UPDATE_TIMER) {
|
||||||
|
return
|
||||||
|
}
|
||||||
var p = 'cache' in Request.prototype
|
var p = 'cache' in Request.prototype
|
||||||
? fetch(jsUrl, {cache: 'no-cache'})
|
? fetch(jsUrl, {cache: 'no-cache'})
|
||||||
: fetch(jsUrl + '?t=' + Date.now())
|
: fetch(jsUrl + '?t=' + Date.now())
|
||||||
|
|
||||||
p.then(function(res) {
|
p.then(function(res) {
|
||||||
res.text().then(function(js) {
|
res.text().then(function(js) {
|
||||||
if (currJs !== js) {
|
if (js.indexOf(info.hash) === -1) {
|
||||||
if (currJs) {
|
newJs = js
|
||||||
hasUpdate = 1
|
console.log('[web2img] new version found')
|
||||||
console.log('update')
|
|
||||||
}
|
|
||||||
currJs = js
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}, 1000 * 120)
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
setInterval(checkUpdate, 1000 * UPDATE_TIMER)
|
||||||
|
|
||||||
onfetch = function(e) {
|
onfetch = function(e) {
|
||||||
|
if (isFirst) {
|
||||||
|
isFirst = 0
|
||||||
|
checkUpdate()
|
||||||
|
}
|
||||||
var req = e.request
|
var req = e.request
|
||||||
if (req.url.indexOf(rootPath)) {
|
if (req.url.indexOf(rootPath)) {
|
||||||
// url not starts with rootPath
|
// url not starts with rootPath
|
||||||
|
@ -206,31 +232,34 @@ function swEnv() {
|
||||||
}
|
}
|
||||||
var res
|
var res
|
||||||
|
|
||||||
if (hasUpdate && req.mode === 'navigate') {
|
if (newJs && req.mode === 'navigate') {
|
||||||
var html = '<script data-root="' + rootPath + '">' + currJs + '</script>'
|
var html = '<script data-root="' + rootPath + '">' + newJs + '<\/script>'
|
||||||
res = new Response(html, {
|
res = new Response(html, {
|
||||||
headers: {
|
headers: {
|
||||||
'content-type': 'text/html'
|
'content-type': 'text/html'
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
hasUpdate = 0
|
newJs = ''
|
||||||
|
console.log('[web2img] updating')
|
||||||
} else {
|
} else {
|
||||||
var path = new URL(req.url).pathname
|
var path = new URL(req.url).pathname
|
||||||
.replace(/\/{2,}/g, '/')
|
.replace(/\/{2,}/g, '/')
|
||||||
.replace(/\/$/, '/index.html')
|
.replace(/\/$/, '/index.html')
|
||||||
|
|
||||||
res = caches.open('.web2img').then(function(cache) {
|
res = openFile(path).then(function(r1) {
|
||||||
return cache.match(path).then(function(res) {
|
return r1 || openFile(rootPath + '404.html').then(function(r2) {
|
||||||
return res || cache.match(rootPath + '404.html').then(function(res) {
|
return r2 || new Response('file not found: ' + path, {
|
||||||
return res || new Response('file not found: ' + path, {
|
|
||||||
status: 404
|
status: 404
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
|
||||||
}
|
}
|
||||||
e.respondWith(res)
|
e.respondWith(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
oninstall = function() {
|
||||||
|
skipWaiting()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRootPath(url) {
|
function getRootPath(url) {
|
||||||
|
|
Loading…
Reference in a new issue