Skip to content

Commit

Permalink
feat: Optimize PWA support
Browse files Browse the repository at this point in the history
- Auto upload sw.min.js as asset with enablePWA option
- Update scripts in package.json
- Remove the cache for avatar in sw.js
  • Loading branch information
HEIGE-PCloud committed May 6, 2021
1 parent 65776d7 commit 3a4f6be
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 12 deletions.
3 changes: 3 additions & 0 deletions assets/sw.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions assets/sw.min.js.map

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions exampleSite/static/sw.min.js

This file was deleted.

1 change: 0 additions & 1 deletion exampleSite/static/sw.min.js.map

This file was deleted.

6 changes: 6 additions & 0 deletions layouts/partials/head/link.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@
{{- $source := $cdn.animateCSS | default "lib/animate/animate.min.css" -}}
{{- $style := dict "Source" $source "Fingerprint" $fingerprint -}}
{{- partial "plugin/defer-style.html" $style -}}

{{- if eq .Site.Params.enablePWA true -}}
{{- /* sw.min.js */ -}}
{{- $sw := resources.Get "sw.min.js" -}}
{{- $_ := $sw.Permalink -}}
{{- end -}}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"scripts": {
"preinstall": "npx npm-force-resolutions",
"babel": "npx babel src/js/theme.js --out-file assets/js/theme.min.js --source-maps --no-comments && npx babel src/js/sw.js --out-file exampleSite/static/sw.min.js --source-maps --no-comments",
"babel": "npx babel src/js/theme.js --out-file assets/js/theme.min.js --source-maps --no-comments && npx babel src/js/sw.js --out-file assets/sw.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",
Expand Down
7 changes: 0 additions & 7 deletions src/js/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@ const CACHE_VERSION = 1;

const BASE_CACHE_FILES = [
'/css/style.min.css',
'/images/avatar.webp',
'/js/theme.min.js',
'/site.webmanifest',
];

const OFFLINE_CACHE_FILES = [
'/css/style.min.css',
'/images/avatar.webp',
'/js/theme.min.js',
'/site.webmanifest',
'/offline',
];

const NOT_FOUND_CACHE_FILES = [
'/css/style.min.css',
'/images/avatar.webp',
'/js/theme.min.js',
'/site.webmanifest',
'/404.html',
Expand Down Expand Up @@ -182,7 +179,6 @@ function cleanupLegacyCache() {
reject();
}
);

}
);
}
Expand Down Expand Up @@ -240,7 +236,6 @@ self.addEventListener(
date = new Date(pair[1]);
}
}

if (date) {
let age = parseInt((new Date().getTime() - date.getTime()) / 1000);
let ttl = getTTL(event.request.url);
Expand All @@ -266,7 +261,6 @@ self.addEventListener(
resolve(response);
}
);

}
)
.catch(
Expand Down Expand Up @@ -322,7 +316,6 @@ self.addEventListener(
return offlineCache.match(OFFLINE_PAGE)
}
)

}
);
}
Expand Down

0 comments on commit 3a4f6be

Please sign in to comment.