Skip to content

Commit d9a3bfe

Browse files
committed
add service worker for offline
1 parent 6cdea40 commit d9a3bfe

File tree

5 files changed

+14477
-219
lines changed

5 files changed

+14477
-219
lines changed

.github/workflows/gh-pages.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
extended: true
2323

2424
- name: Build
25-
run: hugo --gc
25+
run: npm install gulp -g && npm i && gulp build
2626

2727
- name: New Post Check
2828
uses: actions/setup-node@v2

gulpfile.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
const gulp = require("gulp");
2+
const clean = require("gulp-clean");
3+
const shell = require("gulp-shell");
4+
const workbox = require("workbox-build");
5+
6+
gulp.task("clean", function () {
7+
return gulp.src("public", { read: false, allowEmpty: true })
8+
.pipe(clean());
9+
});
10+
11+
gulp.task("hugo-build", shell.task(["hugo --gc"]));
12+
13+
gulp.task("generate-service-worker", () => {
14+
return workbox.generateSW({
15+
cacheId: "bmpi",
16+
globDirectory: "./public",
17+
globPatterns: [
18+
"**/*.{css,js,eot,ttf,woff,woff2,otf}"
19+
],
20+
swDest: "./public/sw.js",
21+
modifyURLPrefix: {
22+
"": "/"
23+
},
24+
clientsClaim: true,
25+
skipWaiting: true,
26+
ignoreURLParametersMatching: [/./],
27+
offlineGoogleAnalytics: true,
28+
maximumFileSizeToCacheInBytes: 50 * 1024 * 1024,
29+
runtimeCaching: [
30+
{
31+
urlPattern: /(?:\/)$/,
32+
handler: "StaleWhileRevalidate",
33+
options: {
34+
cacheName: "html",
35+
expiration: {
36+
maxAgeSeconds: 60 * 60 * 24 * 7,
37+
},
38+
},
39+
},
40+
{
41+
urlPattern: /\.(?:png|jpg|jpeg|gif|bmp|webp|svg|ico)$/,
42+
handler: "CacheFirst",
43+
options: {
44+
cacheName: "images",
45+
expiration: {
46+
maxEntries: 1000,
47+
maxAgeSeconds: 60 * 60 * 24 * 365,
48+
},
49+
},
50+
},
51+
{
52+
urlPattern: /\.(?:mp3|wav|m4a)$/,
53+
handler: "CacheFirst",
54+
options: {
55+
cacheName: "audio",
56+
expiration: {
57+
maxEntries: 1000,
58+
maxAgeSeconds: 60 * 60 * 24 * 365,
59+
},
60+
},
61+
},
62+
{
63+
urlPattern: /\.(?:m4v|mpg|avi)$/,
64+
handler: "CacheFirst",
65+
options: {
66+
cacheName: "videos",
67+
expiration: {
68+
maxEntries: 1000,
69+
maxAgeSeconds: 60 * 60 * 24 * 365,
70+
},
71+
},
72+
}
73+
],
74+
});
75+
});
76+
77+
gulp.task("build", gulp.series("clean", "hugo-build", "generate-service-worker"));

0 commit comments

Comments
 (0)