Skip to content

Commit 6b34901

Browse files
committed
refactor(pwa): revert to JS and Liquid mixing
The gem package won't be able to pass `/sw.min.js` to the user end
1 parent 90693ff commit 6b34901

File tree

9 files changed

+25
-51
lines changed

9 files changed

+25
-51
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,4 @@ package-lock.json
1818
.idea
1919

2020
# Misc
21-
*.map
22-
sw.min.js
2321
assets/js/dist

_includes/head.html

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,6 @@
5050

5151
{{ seo_tags }}
5252

53-
<!-- PWA cache settings -->
54-
<meta
55-
name="pwa-cache"
56-
content="{{ site.pwa.cache.enabled | default: 'false' }}"
57-
{%- if site.baseurl and site.baseurl != empty -%}
58-
data-baseurl="{{ site.baseurl }}"
59-
{%- endif -%}
60-
>
61-
6253
<title>
6354
{%- unless page.layout == 'home' -%}
6455
{{ page.title | append: ' | ' }}
Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
/* PWA loader */
1+
---
2+
layout: compress
3+
permalink: /assets/js/dist/:basename.min.js
4+
---
25

36
if ('serviceWorker' in navigator) {
4-
const meta = document.querySelector('meta[name="pwa-cache"]');
5-
const isEnabled = meta.content === 'true';
7+
const isEnabled = '{{ site.pwa.enabled }}' === 'true';
68

79
if (isEnabled) {
8-
let swUrl = '/sw.min.js';
9-
const baseUrl = meta.getAttribute('data-baseurl');
10-
11-
if (baseUrl !== null) {
12-
swUrl = `${baseUrl}${swUrl}?baseurl=${encodeURIComponent(baseUrl)}`;
13-
}
14-
10+
const swUrl = '{{ '/sw.min.js' | relative_url }}';
1511
const $notification = $('#notification');
1612
const $btnRefresh = $('#notification .toast-body>button');
1713

1814
navigator.serviceWorker.register(swUrl).then((registration) => {
19-
// In case the user ignores the notification
15+
{% comment %}In case the user ignores the notification{% endcomment %}
2016
if (registration.waiting) {
2117
$notification.toast('show');
2218
}
@@ -41,7 +37,7 @@ if ('serviceWorker' in navigator) {
4137

4238
let refreshing = false;
4339

44-
// Detect controller change and refresh all the opened tabs
40+
{% comment %}Detect controller change and refresh all the opened tabs{% endcomment %}
4541
navigator.serviceWorker.addEventListener('controllerchange', () => {
4642
if (!refreshing) {
4743
window.location.reload();
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
/* PWA service worker */
1+
---
2+
layout: compress
3+
permalink: /:basename.min.js
4+
# PWA service worker
5+
---
26

3-
const swconfPath = '/assets/js/data/swconf.js';
4-
const params = new URL(location).searchParams;
5-
const swconfUrl = params.has('baseurl')
6-
? `${params.get('baseurl')}${swconfPath}`
7-
: swconfPath;
7+
const swconfUrl = '{{ '/assets/js/data/swconf.js' | relative_url }}';
88

99
importScripts(swconfUrl);
1010
const purge = swconf.purge;
@@ -88,7 +88,7 @@ self.addEventListener('fetch', (event) => {
8888
return response;
8989
}
9090

91-
// See : <https://developers.google.com/web/fundamentals/primers/service-workers#cache_and_return_requests>
91+
{% comment %}See: <https://developers.google.com/web/fundamentals/primers/service-workers#cache_and_return_requests>{% endcomment %}
9292
let responseToCache = response.clone();
9393

9494
caches.open(swconf.cacheName).then((cache) => {

jekyll-theme-chirpy.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
1111
spec.license = "MIT"
1212

1313
spec.files = `git ls-files -z`.split("\x0").select { |f|
14-
f.match(%r!^((_(includes|layouts|sass|(data\/(locales|origin)))|assets)\/|sw|README|LICENSE)!i)
14+
f.match(%r!^((_(includes|layouts|sass|(data\/(locales|origin)))|assets)\/|README|LICENSE)!i)
1515
}
1616

1717
spec.metadata = {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
},
1414
"homepage": "https://github.com/cotes2020/jekyll-theme-chirpy/",
1515
"scripts": {
16-
"prebuild": "npx rimraf assets/js/dist sw.min.js*",
16+
"prebuild": "npx rimraf assets/js/dist",
1717
"build": "NODE_ENV=production npx rollup -c --bundleConfigAsCjs",
18-
"prewatch": "npx rimraf assets/js/dist sw.min.js*",
18+
"prewatch": "npx rimraf assets/js/dist",
1919
"watch": "npx rollup -c --bundleConfigAsCjs -w",
2020
"test": "npx stylelint _sass/**/*.scss",
2121
"fixlint": "npm run test -- --fix"

rollup.config.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,17 @@ const SRC_DEFAULT = '_javascript';
77
const DIST_DEFAULT = 'assets/js/dist';
88
const isProd = process.env.NODE_ENV === 'production';
99

10-
function build(filename, opts) {
11-
let src = SRC_DEFAULT;
12-
let dist = DIST_DEFAULT;
13-
14-
if (typeof opts !== 'undefined') {
15-
src = opts.src || src;
16-
dist = opts.dist || dist;
17-
}
18-
10+
function build(filename) {
1911
return {
20-
input: [`${src}/${filename}.js`],
12+
input: [`${SRC_DEFAULT}/${filename}.js`],
2113
output: {
22-
file: `${dist}/${filename}.min.js`,
14+
file: `${DIST_DEFAULT}/${filename}.min.js`,
2315
format: 'iife',
2416
name: 'Chirpy',
2517
sourcemap: !isProd
2618
},
2719
watch: {
28-
include: `${src}/**`
20+
include: `${SRC_DEFAULT}/**`
2921
},
3022
plugins: [
3123
babel({
@@ -50,7 +42,5 @@ export default [
5042
build('categories'),
5143
build('page'),
5244
build('post'),
53-
build('misc'),
54-
build('app', { src: `${SRC_DEFAULT}/pwa` }),
55-
build('sw', { src: `${SRC_DEFAULT}/pwa`, dist: '.' })
45+
build('misc')
5646
];

tools/init

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ init_files() {
103103
npm i && npm run build
104104

105105
# track the js output
106-
_sedi "/^assets.*\/dist/d;/^sw.*\.js/d" .gitignore
106+
_sedi "/^assets.*\/dist/d" .gitignore
107107
}
108108

109109
commit() {

tools/release

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ NODE_CONFIG="package.json"
2727
CHANGE_LOG="docs/CHANGELOG.md"
2828

2929
JS_DIST="assets/js/dist"
30-
PWA_SW="sw.min.js"
3130
BACKUP_PATH="$(mktemp -d)"
3231

3332
FILES=(
@@ -159,7 +158,7 @@ build_gem() {
159158
rm -f ./*.gem
160159

161160
npm run build
162-
git add "$JS_DIST" "$PWA_SW" -f # add JS distribution files to gem
161+
git add "$JS_DIST" -f # add JS distribution files to gem
163162
gem build "$GEM_SPEC"
164163
cp "$JS_DIST"/* "$BACKUP_PATH"
165164

0 commit comments

Comments
 (0)