mirror of
https://github.com/hugo-fixit/FixIt.git
synced 2026-08-25 07:48:54 +00:00
🎉 Feat: add PWA support
This commit is contained in:
+2
-1
@@ -2,14 +2,15 @@
|
||||
|
||||
## v0.2.12
|
||||
|
||||
- :tada: Feat: add PWA support
|
||||
- :bug: Fix: add scroll-margin to headerlink
|
||||
- :bug: Fix: Check if content is empty beforing passing
|
||||
- :fire: Feat: remove lib smooth-scroll ([#1](https://github.com/Lruihao/FixIt/issues/1))
|
||||
- :lipstick: Style: change scroll-behavior to smooth
|
||||
- :lipstick: Style: page image margin
|
||||
- :arrow_up: Chore: update all third-party libraries
|
||||
- :wrench: Chore: minify card-link background image
|
||||
- :wrench: Chore(i18n): change some translations
|
||||
- :bug: Fix: Check if content is empty beforing passing
|
||||
|
||||
## v0.2.11
|
||||
|
||||
|
||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+15
-6
@@ -104,12 +104,14 @@ enableEmoji = true
|
||||
noFavicon = false
|
||||
# modern SVG favicon to use in place of older style .png and .ico files
|
||||
svgFavicon = ""
|
||||
# Android browser theme color
|
||||
themeColor = "#ffffff"
|
||||
# Safari mask icon color
|
||||
iconColor = "#5bbad5"
|
||||
# Windows v8-10 tile color
|
||||
tileColor = "#da532c"
|
||||
# Android browser theme color
|
||||
[languages.en.params.app.themeColor]
|
||||
light = "#ffffff"
|
||||
dark = "#252627"
|
||||
# Search config
|
||||
[languages.en.params.search]
|
||||
enable = true
|
||||
@@ -304,12 +306,14 @@ enableEmoji = true
|
||||
noFavicon = false
|
||||
# 更现代的 SVG 网站图标, 可替代旧的 .png 和 .ico 文件
|
||||
svgFavicon = ""
|
||||
# Android 浏览器主题色
|
||||
themeColor = "#ffffff"
|
||||
# Safari 图标颜色
|
||||
iconColor = "#5bbad5"
|
||||
# Windows v8-10 磁贴颜色
|
||||
tileColor = "#da532c"
|
||||
# Android 浏览器主题色
|
||||
[languages.zh-cn.params.app.themeColor]
|
||||
light = "#ffffff"
|
||||
dark = "#252627"
|
||||
# 搜索配置
|
||||
[languages.zh-cn.params.search]
|
||||
enable = true
|
||||
@@ -500,12 +504,14 @@ enableEmoji = true
|
||||
noFavicon = false
|
||||
# modern SVG favicon to use in place of older style .png and .ico files
|
||||
svgFavicon = ""
|
||||
# Android browser theme color
|
||||
themeColor = "#ffffff"
|
||||
# Safari mask icon color
|
||||
iconColor = "#5bbad5"
|
||||
# Windows v8-10 tile color
|
||||
tileColor = "#da532c"
|
||||
# Android browser theme color
|
||||
[languages.fr.params.app.themeColor]
|
||||
light = "#ffffff"
|
||||
dark = "#252627"
|
||||
# Search config
|
||||
[languages.fr.params.search]
|
||||
enable = true
|
||||
@@ -639,6 +645,9 @@ enableEmoji = true
|
||||
# website images for Open Graph and Twitter Cards
|
||||
# 网站图片, 用于 Open Graph 和 Twitter Cards
|
||||
images = ["/logo.png"]
|
||||
# enable PWA
|
||||
# 开启 PWA 支持
|
||||
enablePWA = true
|
||||
|
||||
# Header config
|
||||
# 页面头部导航栏配置
|
||||
|
||||
@@ -2,50 +2,71 @@
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html lang="{{ .Site.LanguageCode }}">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="robots" content="noodp" />
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
|
||||
<title>
|
||||
{{- block "title" . }}{{ .Site.Title }}{{ end -}}
|
||||
</title>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="robots" content="noodp" />
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
|
||||
<title>
|
||||
{{- block "title" . }}{{ .Site.Title }}{{ end -}}
|
||||
</title>
|
||||
|
||||
{{- partial "head/meta.html" . -}}
|
||||
{{- partial "head/link.html" . -}}
|
||||
{{- partial "head/seo.html" . -}}
|
||||
</head>
|
||||
<body header-desktop="{{ .Site.Params.header.desktopMode }}" header-mobile="{{ .Site.Params.header.mobileMode }}">
|
||||
{{- /* Check theme isDark before body rendering */ -}}
|
||||
{{- $theme := .Site.Params.defaulttheme -}}
|
||||
<script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('{{ $theme }}' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : '{{ $theme }}' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>
|
||||
{{- partial "head/meta.html" . -}}
|
||||
{{- partial "head/link.html" . -}}
|
||||
{{- partial "head/seo.html" . -}}
|
||||
</head>
|
||||
<body header-desktop="{{ .Site.Params.header.desktopMode }}" header-mobile="{{ .Site.Params.header.mobileMode }}">
|
||||
{{- /* Check theme isDark before body rendering */ -}}
|
||||
{{- $theme := .Site.Params.defaulttheme -}}
|
||||
<script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('{{ $theme }}' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : '{{ $theme }}' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>
|
||||
|
||||
<div id="mask"></div>
|
||||
<div id="mask"></div>
|
||||
|
||||
{{- /* Body wrapper */ -}}
|
||||
<div class="wrapper">
|
||||
{{- partial "header.html" . -}}
|
||||
<main class="main">
|
||||
<div class="container">
|
||||
{{- block "content" . }}{{ end -}}
|
||||
</div>
|
||||
</main>
|
||||
{{- partial "footer.html" . -}}
|
||||
{{- /* Body wrapper */ -}}
|
||||
<div class="wrapper">
|
||||
{{- partial "header.html" . -}}
|
||||
<main class="main">
|
||||
<div class="container">
|
||||
{{- block "content" . }}{{ end -}}
|
||||
</div>
|
||||
</main>
|
||||
{{- partial "footer.html" . -}}
|
||||
</div>
|
||||
|
||||
<div id="fixed-buttons">
|
||||
{{- /* top button */ -}}
|
||||
<a href="#" id="back-to-top" class="fixed-button" title="{{ T `backToTop` }}">
|
||||
<i class="fas fa-arrow-up fa-fw"></i>
|
||||
</a>
|
||||
<div id="fixed-buttons">
|
||||
{{- /* top button */ -}}
|
||||
<a href="#" id="back-to-top" class="fixed-button" title="{{ T `backToTop` }}">
|
||||
<i class="fas fa-arrow-up fa-fw"></i>
|
||||
</a>
|
||||
|
||||
{{- /* comment button */ -}}
|
||||
<a href="#" id="view-comments" class="fixed-button" title="{{ T `viewComments` }}">
|
||||
<i class="fas fa-comment fa-fw"></i>
|
||||
</a>
|
||||
</div>
|
||||
{{- /* comment button */ -}}
|
||||
<a href="#" id="view-comments" class="fixed-button" title="{{ T `viewComments` }}">
|
||||
<i class="fas fa-comment fa-fw"></i>
|
||||
</a>
|
||||
</div>
|
||||
|
||||
{{- /* Load JavaScript scripts and CSS */ -}}
|
||||
{{- partial "assets.html" . -}}
|
||||
</body>
|
||||
{{- /* PWA */ -}}
|
||||
{{- if eq .Site.Params.enablePWA true -}}
|
||||
{{- if eq hugo.Environment "production" -}}
|
||||
<script type="text/javascript">
|
||||
if ('serviceWorker' in navigator) {
|
||||
navigator.serviceWorker
|
||||
.register('/service-worker.min.js', { scope: '/' })
|
||||
.then(function (registration) {
|
||||
// console.log('Service Worker Registered');
|
||||
})
|
||||
.catch(function (error) {
|
||||
console.error('error: ', error);
|
||||
});
|
||||
navigator.serviceWorker.ready.then(function (registration) {
|
||||
// console.log('Service Worker Ready');
|
||||
});
|
||||
}
|
||||
</script>
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- /* Load JavaScript scripts and CSS */ -}}
|
||||
{{- partial "assets.html" . -}}
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,58 +1,57 @@
|
||||
{{- if ne .Site.Params.footer.enable false -}}
|
||||
<footer class="footer">
|
||||
<div class="footer-container">
|
||||
{{- /* Custom Content */ -}}
|
||||
{{- with .Site.Params.footer.custom -}}
|
||||
<div class="footer-line">
|
||||
{{- safeHTML . -}}
|
||||
</div>
|
||||
{{- end -}}
|
||||
<!-- <i class="fas fa-bug fa-fw"></i> -->
|
||||
{{- /* Hugo and FixIt */ -}}
|
||||
{{- if ne .Site.Params.footer.hugo false -}}
|
||||
<div class="footer-line">
|
||||
{{- $hugo := printf `<a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo %v">Hugo</a>` hugo.Version -}}
|
||||
{{- $theme := .Scratch.Get "version" | printf
|
||||
`<a href="https://github.com/Lruihao/FixIt" target="_blank" rel="noopener noreffer" title="FixIt %v">
|
||||
<i class="fa-stack fa-xs">
|
||||
<i class="fas fa-bug fa-stack-1x"></i>
|
||||
<i class="fas fa-ban fa-stack-2x"></i>
|
||||
</i>
|
||||
FixIt
|
||||
</a>` -}}
|
||||
{{- dict "Hugo" $hugo "Theme" $theme | T "poweredBySome" | safeHTML }}
|
||||
</div>
|
||||
{{- end -}}
|
||||
|
||||
<div class="footer-line">
|
||||
{{- /* Copyright year */ -}}
|
||||
{{- if ne .Site.Params.footer.copyright false -}}
|
||||
<i class="far fa-copyright fa-fw"></i>
|
||||
{{- with .Site.Params.footer.since -}}
|
||||
<span itemprop="copyrightYear">
|
||||
{{- if lt . now.Year }}{{ . }} - {{ end }}{{ now.Year -}}
|
||||
</span>
|
||||
{{- else -}}
|
||||
<span itemprop="copyrightYear">{{ now.Year }}</span>
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- /* Author */ -}}
|
||||
{{- if ne .Site.Params.footer.author false -}}
|
||||
<span class="author" itemprop="copyrightHolder"> <a href="{{ $.Site.Author.link | default .Site.Home.RelPermalink }}" target="_blank">{{ .Site.Author.name }}</a></span>
|
||||
{{- end -}}
|
||||
|
||||
{{- /* License */ -}}
|
||||
{{- with .Site.Params.footer.license -}}
|
||||
| <span class="license">{{ . | safeHTML }}</span>
|
||||
{{- end -}}
|
||||
|
||||
{{- /* ICP */ -}}
|
||||
{{- with .Site.Params.footer.icp -}}
|
||||
<span class="icp-splitter"> | </span><br class="icp-br"/>
|
||||
<span class="icp">{{ . | safeHTML }}</span>
|
||||
{{- end -}}
|
||||
</div>
|
||||
<footer class="footer">
|
||||
<div class="footer-container">
|
||||
{{- /* Custom Content */ -}}
|
||||
{{- with .Site.Params.footer.custom -}}
|
||||
<div class="footer-line">
|
||||
{{- safeHTML . -}}
|
||||
</div>
|
||||
</footer>
|
||||
{{- end -}}
|
||||
{{- /* Hugo and FixIt */ -}}
|
||||
{{- if ne .Site.Params.footer.hugo false -}}
|
||||
<div class="footer-line">
|
||||
{{- $hugo := printf `<a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo %v">Hugo</a>` hugo.Version -}}
|
||||
{{- $theme := .Scratch.Get "version" | printf
|
||||
`<a href="https://github.com/Lruihao/FixIt" target="_blank" rel="noopener noreffer" title="FixIt %v">
|
||||
<i class="fa-stack fa-xs">
|
||||
<i class="fas fa-bug fa-stack-1x"></i>
|
||||
<i class="fas fa-ban fa-stack-2x"></i>
|
||||
</i>
|
||||
FixIt
|
||||
</a>` -}}
|
||||
{{- dict "Hugo" $hugo "Theme" $theme | T "poweredBySome" | safeHTML }}
|
||||
</div>
|
||||
{{- end -}}
|
||||
|
||||
<div class="footer-line">
|
||||
{{- /* Copyright year */ -}}
|
||||
{{- if ne .Site.Params.footer.copyright false -}}
|
||||
<i class="far fa-copyright fa-fw"></i>
|
||||
{{- with .Site.Params.footer.since -}}
|
||||
<span itemprop="copyrightYear">
|
||||
{{- if lt . now.Year }}{{ . }} - {{ end }}{{ now.Year -}}
|
||||
</span>
|
||||
{{- else -}}
|
||||
<span itemprop="copyrightYear">{{ now.Year }}</span>
|
||||
{{- end -}}
|
||||
{{- end -}}
|
||||
|
||||
{{- /* Author */ -}}
|
||||
{{- if ne .Site.Params.footer.author false -}}
|
||||
<span class="author" itemprop="copyrightHolder"> <a href="{{ $.Site.Author.link | default .Site.Home.RelPermalink }}" target="_blank">{{ .Site.Author.name }}</a></span>
|
||||
{{- end -}}
|
||||
|
||||
{{- /* License */ -}}
|
||||
{{- with .Site.Params.footer.license -}}
|
||||
| <span class="license">{{ . | safeHTML }}</span>
|
||||
{{- end -}}
|
||||
|
||||
{{- /* ICP */ -}}
|
||||
{{- with .Site.Params.footer.icp -}}
|
||||
<span class="icp-splitter"> | </span><br class="icp-br"/>
|
||||
<span class="icp">{{ . | safeHTML }}</span>
|
||||
{{- end -}}
|
||||
</div>
|
||||
</div>
|
||||
</footer>
|
||||
{{- end -}}
|
||||
|
||||
@@ -8,10 +8,14 @@
|
||||
<meta name="application-name" content="{{ .Site.Params.app.title | default .Site.Title }}">
|
||||
<meta name="apple-mobile-web-app-title" content="{{ .Site.Params.app.title | default .Site.Title }}">
|
||||
|
||||
{{- with .Site.Params.app.themeColor -}}
|
||||
<meta name="theme-color" content="{{ . }}">
|
||||
{{- with .Site.Params.app.themeColor.light -}}
|
||||
<meta name="theme-color" media="(prefers-color-scheme: light)" content="{{ . }}">
|
||||
{{- end -}}
|
||||
|
||||
{{- with .Site.Params.app.themeColor.dark -}}
|
||||
<meta name="theme-color" media="(prefers-color-scheme: dark)" content="{{ . }}">
|
||||
{{- end -}}
|
||||
|
||||
{{- with .Site.Params.app.tileColor -}}
|
||||
<meta name="msapplication-TileColor" content="{{ . }}">
|
||||
<meta name="msapplication-TileColor" content="{{ . }}">
|
||||
{{- end -}}
|
||||
|
||||
Generated
+7
-7
@@ -3972,7 +3972,7 @@
|
||||
"dependencies": {
|
||||
"minimist": {
|
||||
"version": "1.2.5",
|
||||
"resolved": "https://registry.nlark.com/minimist/download/minimist-1.2.5.tgz",
|
||||
"resolved": "https://registry.nlark.com/minimist/download/minimist-1.2.5.tgz?cache=0&sync_timestamp=1618847017774&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fminimist%2Fdownload%2Fminimist-1.2.5.tgz",
|
||||
"integrity": "sha1-Z9ZgFLZqaoqqDAg8X9WN9OTpdgI=",
|
||||
"dev": true
|
||||
}
|
||||
@@ -4620,7 +4620,7 @@
|
||||
"dependencies": {
|
||||
"minimist": {
|
||||
"version": "1.2.5",
|
||||
"resolved": "https://registry.nlark.com/minimist/download/minimist-1.2.5.tgz",
|
||||
"resolved": "https://registry.nlark.com/minimist/download/minimist-1.2.5.tgz?cache=0&sync_timestamp=1618847017774&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fminimist%2Fdownload%2Fminimist-1.2.5.tgz",
|
||||
"integrity": "sha1-Z9ZgFLZqaoqqDAg8X9WN9OTpdgI=",
|
||||
"dev": true
|
||||
}
|
||||
@@ -4728,7 +4728,7 @@
|
||||
},
|
||||
"minimist": {
|
||||
"version": "1.2.5",
|
||||
"resolved": "https://registry.nlark.com/minimist/download/minimist-1.2.5.tgz",
|
||||
"resolved": "https://registry.nlark.com/minimist/download/minimist-1.2.5.tgz?cache=0&sync_timestamp=1618847017774&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fminimist%2Fdownload%2Fminimist-1.2.5.tgz",
|
||||
"integrity": "sha1-Z9ZgFLZqaoqqDAg8X9WN9OTpdgI=",
|
||||
"dev": true
|
||||
},
|
||||
@@ -5272,9 +5272,9 @@
|
||||
"integrity": "sha512-BViHy7LKeTz4oNnkcLJ+lVSL6vpiFeX6/d3oSH8zCW7UxP2onchk+vTGB143xuFjHS3deTgkKoXXymXqymiIdA==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"inherits": "^2.0.3",
|
||||
"string_decoder": "^1.1.1",
|
||||
"util-deprecate": "^1.0.1"
|
||||
"inherits": "2.0.4",
|
||||
"string_decoder": "1.1.1",
|
||||
"util-deprecate": "1.0.2"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5329,7 +5329,7 @@
|
||||
"dependencies": {
|
||||
"minimist": {
|
||||
"version": "1.2.5",
|
||||
"resolved": "https://registry.nlark.com/minimist/download/minimist-1.2.5.tgz",
|
||||
"resolved": "https://registry.nlark.com/minimist/download/minimist-1.2.5.tgz?cache=0&sync_timestamp=1618847017774&other_urls=https%3A%2F%2Fregistry.nlark.com%2Fminimist%2Fdownload%2Fminimist-1.2.5.tgz",
|
||||
"integrity": "sha1-Z9ZgFLZqaoqqDAg8X9WN9OTpdgI=",
|
||||
"dev": true
|
||||
}
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@
|
||||
},
|
||||
"scripts": {
|
||||
"preinstall": "npx npm-force-resolutions",
|
||||
"babel": "npx babel src/js --out-file assets/js/theme.min.js --source-maps",
|
||||
"babel": "npx babel src/js/theme.js --out-file assets/js/theme.min.js --source-maps --no-comments && npx babel src/js/service-worker.js --out-file static/service-worker.min.js --source-maps --no-comments",
|
||||
"build": "npm run babel && hugo -v --source=exampleSite --themesDir ../.. --gc",
|
||||
"build-lunr-segmentit": "browserify src/lib/lunr.segmentit.js -o assets/lib/lunr/lunr.segmentit.js -t babelify --presets @babel/preset-env --presets minify",
|
||||
"start": "npm run babel && hugo server --source=exampleSite --themesDir ../.. -D --disableFastRender",
|
||||
|
||||
@@ -0,0 +1,262 @@
|
||||
const CACHE_VERSION = 1;
|
||||
|
||||
const BASE_CACHE_FILES = [
|
||||
'/css/style.min.css',
|
||||
'/js/theme.min.js',
|
||||
'/site.webmanifest'
|
||||
];
|
||||
|
||||
const OFFLINE_CACHE_FILES = [
|
||||
'/css/style.min.css',
|
||||
'/js/theme.min.js',
|
||||
'/site.webmanifest',
|
||||
'/offline'
|
||||
];
|
||||
|
||||
const NOT_FOUND_CACHE_FILES = [
|
||||
'/css/style.min.css',
|
||||
'/js/theme.min.js',
|
||||
'/site.webmanifest',
|
||||
'/404.html'
|
||||
];
|
||||
|
||||
const OFFLINE_PAGE = '/offline';
|
||||
const NOT_FOUND_PAGE = '/404.html';
|
||||
|
||||
const CACHE_VERSIONS = {
|
||||
assets: 'assets-v' + CACHE_VERSION,
|
||||
content: 'content-v' + CACHE_VERSION,
|
||||
offline: 'offline-v' + CACHE_VERSION,
|
||||
notFound: '404-v' + CACHE_VERSION
|
||||
};
|
||||
|
||||
// Define MAX_TTL's in SECONDS for specific file extensions
|
||||
const MAX_TTL = {
|
||||
'/': 3600,
|
||||
html: 3600,
|
||||
json: 86400,
|
||||
js: 86400,
|
||||
css: 86400
|
||||
};
|
||||
|
||||
const CACHE_BLACKLIST = [
|
||||
(str) => {
|
||||
return !str.startsWith('http://localhost');
|
||||
}
|
||||
];
|
||||
|
||||
const SUPPORTED_METHODS = ['GET'];
|
||||
|
||||
/**
|
||||
* isBlackListed
|
||||
* @param {string} url
|
||||
* @returns {boolean}
|
||||
*/
|
||||
function isBlacklisted(url) {
|
||||
return CACHE_BLACKLIST.length > 0
|
||||
? !CACHE_BLACKLIST.filter((rule) => {
|
||||
if (typeof rule === 'function') {
|
||||
return !rule(url);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}).length
|
||||
: false;
|
||||
}
|
||||
|
||||
/**
|
||||
* getFileExtension
|
||||
* @param {string} url
|
||||
* @returns {string}
|
||||
*/
|
||||
function getFileExtension(url) {
|
||||
const extension = url.split('.').reverse()[0].split('?')[0];
|
||||
return extension.endsWith('/') ? '/' : extension;
|
||||
}
|
||||
|
||||
/**
|
||||
* getTTL
|
||||
* @param {string} url
|
||||
*/
|
||||
function getTTL(url) {
|
||||
if (typeof url === 'string') {
|
||||
const extension = getFileExtension(url);
|
||||
if (typeof MAX_TTL[extension] === 'number') {
|
||||
return MAX_TTL[extension];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* installServiceWorker
|
||||
* @returns {Promise}
|
||||
*/
|
||||
function installServiceWorker() {
|
||||
return Promise.all([
|
||||
caches.open(CACHE_VERSIONS.assets).then((cache) => {
|
||||
return cache.addAll(BASE_CACHE_FILES);
|
||||
}),
|
||||
caches.open(CACHE_VERSIONS.offline).then((cache) => {
|
||||
return cache.addAll(OFFLINE_CACHE_FILES);
|
||||
}),
|
||||
caches.open(CACHE_VERSIONS.notFound).then((cache) => {
|
||||
return cache.addAll(NOT_FOUND_CACHE_FILES);
|
||||
})
|
||||
]).then(() => {
|
||||
return self.skipWaiting();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* cleanupLegacyCache
|
||||
* @returns {Promise}
|
||||
*/
|
||||
function cleanupLegacyCache() {
|
||||
const currentCaches = Object.keys(CACHE_VERSIONS).map((key) => {
|
||||
return CACHE_VERSIONS[key];
|
||||
});
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
caches
|
||||
.keys()
|
||||
.then((keys) => {
|
||||
return keys.filter((key) => {
|
||||
return !~currentCaches.indexOf(key);
|
||||
});
|
||||
})
|
||||
.then((legacy) => {
|
||||
if (legacy.length) {
|
||||
Promise.all(
|
||||
legacy.map((legacyKey) => {
|
||||
return caches.delete(legacyKey);
|
||||
})
|
||||
)
|
||||
.then(() => {
|
||||
resolve();
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
self.addEventListener('install', (event) => {
|
||||
event.waitUntil(Promise.all([installServiceWorker(), self.skipWaiting()]));
|
||||
});
|
||||
|
||||
// The activate handler takes care of cleaning up old caches.
|
||||
self.addEventListener('activate', (event) => {
|
||||
event.waitUntil(
|
||||
Promise.all([
|
||||
cleanupLegacyCache(),
|
||||
self.clients.claim(),
|
||||
self.skipWaiting()
|
||||
]).catch((err) => {
|
||||
console.log(err);
|
||||
self.skipWaiting();
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
self.addEventListener('fetch', (event) => {
|
||||
event.respondWith(
|
||||
caches.open(CACHE_VERSIONS.content).then((cache) => {
|
||||
return cache
|
||||
.match(event.request)
|
||||
.then((response) => {
|
||||
if (response) {
|
||||
const headers = response.headers.entries();
|
||||
let date = null;
|
||||
|
||||
for (const pair of headers) {
|
||||
if (pair[0] === 'date') {
|
||||
date = new Date(pair[1]);
|
||||
}
|
||||
}
|
||||
if (date) {
|
||||
const age = parseInt(
|
||||
(new Date().getTime() - date.getTime()) / 1000
|
||||
);
|
||||
const ttl = getTTL(event.request.url);
|
||||
|
||||
if (ttl && age > ttl) {
|
||||
return new Promise((resolve) => {
|
||||
return fetch(event.request.clone())
|
||||
.then((updatedResponse) => {
|
||||
if (updatedResponse) {
|
||||
cache.put(event.request, updatedResponse.clone());
|
||||
resolve(updatedResponse);
|
||||
} else {
|
||||
resolve(response);
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
resolve(response);
|
||||
});
|
||||
}).catch((err) => {
|
||||
console.log(err);
|
||||
return response;
|
||||
});
|
||||
} else {
|
||||
return response;
|
||||
}
|
||||
} else {
|
||||
return response;
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.then((response) => {
|
||||
if (response) {
|
||||
return response;
|
||||
} else {
|
||||
return fetch(event.request.clone())
|
||||
.then((response) => {
|
||||
if (response.status < 400) {
|
||||
if (
|
||||
~SUPPORTED_METHODS.indexOf(event.request.method) &&
|
||||
!isBlacklisted(event.request.url) &&
|
||||
event.request.url.slice(0, 4) === 'http'
|
||||
) {
|
||||
cache.put(event.request, response.clone());
|
||||
}
|
||||
return response;
|
||||
} else {
|
||||
return caches.open(CACHE_VERSIONS.notFound).then((cache) => {
|
||||
return cache.match(NOT_FOUND_PAGE);
|
||||
});
|
||||
}
|
||||
})
|
||||
.then((response) => {
|
||||
if (response) {
|
||||
return response;
|
||||
}
|
||||
})
|
||||
.catch(() => {
|
||||
return caches
|
||||
.open(CACHE_VERSIONS.offline)
|
||||
.then((offlineCache) => {
|
||||
return offlineCache.match(OFFLINE_PAGE);
|
||||
});
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error(' Error in fetch handler:', error);
|
||||
throw error;
|
||||
});
|
||||
})
|
||||
);
|
||||
});
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
"use strict";function _createForOfIteratorHelper(a,b){var c="undefined"!=typeof Symbol&&a[Symbol.iterator]||a["@@iterator"];if(!c){if(Array.isArray(a)||(c=_unsupportedIterableToArray(a))||b&&a&&"number"==typeof a.length){c&&(a=c);var d=0,e=function(){};return{s:e,n:function n(){return d>=a.length?{done:!0}:{done:!1,value:a[d++]}},e:function e(a){throw a},f:e}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var f,g=!0,h=!1;return{s:function s(){c=c.call(a)},n:function n(){var a=c.next();return g=a.done,a},e:function e(a){h=!0,f=a},f:function f(){try{g||null==c.return||c.return()}finally{if(h)throw f}}}}function _unsupportedIterableToArray(a,b){if(a){if("string"==typeof a)return _arrayLikeToArray(a,b);var c=Object.prototype.toString.call(a).slice(8,-1);return"Object"===c&&a.constructor&&(c=a.constructor.name),"Map"===c||"Set"===c?Array.from(a):"Arguments"===c||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(c)?_arrayLikeToArray(a,b):void 0}}function _arrayLikeToArray(a,b){(null==b||b>a.length)&&(b=a.length);for(var c=0,d=Array(b);c<b;c++)d[c]=a[c];return d}var CACHE_VERSION=1,BASE_CACHE_FILES=["/css/style.min.css","/js/theme.min.js","/site.webmanifest"],OFFLINE_CACHE_FILES=["/css/style.min.css","/js/theme.min.js","/site.webmanifest","/offline"],NOT_FOUND_CACHE_FILES=["/css/style.min.css","/js/theme.min.js","/site.webmanifest","/404.html"],OFFLINE_PAGE="/offline",NOT_FOUND_PAGE="/404.html",CACHE_VERSIONS={assets:"assets-v1",content:"content-v1",offline:"offline-v1",notFound:"404-v1"},MAX_TTL={"/":3600,html:3600,json:86400,js:86400,css:86400},CACHE_BLACKLIST=[function(a){return!a.startsWith("http://localhost")}],SUPPORTED_METHODS=["GET"];function isBlacklisted(a){return!!(0<CACHE_BLACKLIST.length)&&!CACHE_BLACKLIST.filter(function(b){return!("function"!=typeof b)&&!b(a)}).length}function getFileExtension(a){var b=a.split(".").reverse()[0].split("?")[0];return b.endsWith("/")?"/":b}function getTTL(a){if("string"==typeof a){var b=getFileExtension(a);return"number"==typeof MAX_TTL[b]?MAX_TTL[b]:null}return null}function installServiceWorker(){return Promise.all([caches.open(CACHE_VERSIONS.assets).then(function(a){return a.addAll(BASE_CACHE_FILES)}),caches.open(CACHE_VERSIONS.offline).then(function(a){return a.addAll(OFFLINE_CACHE_FILES)}),caches.open(CACHE_VERSIONS.notFound).then(function(a){return a.addAll(NOT_FOUND_CACHE_FILES)})]).then(function(){return self.skipWaiting()})}function cleanupLegacyCache(){var a=Object.keys(CACHE_VERSIONS).map(function(a){return CACHE_VERSIONS[a]});return new Promise(function(b,c){caches.keys().then(function(b){return b.filter(function(b){return!~a.indexOf(b)})}).then(function(a){a.length?Promise.all(a.map(function(a){return caches.delete(a)})).then(function(){b()}).catch(function(a){c(a)}):b()}).catch(function(a){c(a)})})}self.addEventListener("install",function(a){a.waitUntil(Promise.all([installServiceWorker(),self.skipWaiting()]))}),self.addEventListener("activate",function(a){a.waitUntil(Promise.all([cleanupLegacyCache(),self.clients.claim(),self.skipWaiting()]).catch(function(a){console.log(a),self.skipWaiting()}))}),self.addEventListener("fetch",function(a){a.respondWith(caches.open(CACHE_VERSIONS.content).then(function(b){return b.match(a.request).then(function(c){if(c){var d,e=c.headers.entries(),f=null,g=_createForOfIteratorHelper(e);try{for(g.s();!(d=g.n()).done;){var j=d.value;"date"===j[0]&&(f=new Date(j[1]))}}catch(a){g.e(a)}finally{g.f()}if(f){var h=parseInt((new Date().getTime()-f.getTime())/1e3),i=getTTL(a.request.url);return i&&h>i?new Promise(function(d){return fetch(a.request.clone()).then(function(e){e?(b.put(a.request,e.clone()),d(e)):d(c)}).catch(function(){d(c)})}).catch(function(a){return console.log(a),c}):c}return c}return null}).then(function(c){return c?c:fetch(a.request.clone()).then(function(c){return 400>c.status?(~SUPPORTED_METHODS.indexOf(a.request.method)&&!isBlacklisted(a.request.url)&&"http"===a.request.url.slice(0,4)&&b.put(a.request,c.clone()),c):caches.open(CACHE_VERSIONS.notFound).then(function(a){return a.match(NOT_FOUND_PAGE)})}).then(function(a){if(a)return a}).catch(function(){return caches.open(CACHE_VERSIONS.offline).then(function(a){return a.match(OFFLINE_PAGE)})})}).catch(function(a){throw console.error(" Error in fetch handler:",a),a})}))});
|
||||
|
||||
//# sourceMappingURL=service-worker.min.js.map
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user