-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
generate service worker for installable PWA
- Loading branch information
Showing
4 changed files
with
122 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
from wq.core import wq | ||
import click | ||
import os | ||
import json | ||
from glob import glob | ||
|
||
|
||
@wq.command() | ||
@click.argument('version') | ||
@click.option( | ||
'--template', | ||
type=click.Path(), | ||
help="Override service worker template" | ||
) | ||
@click.option( | ||
'--output', | ||
type=click.Path(), | ||
default="./htdocs/service-worker.js", | ||
help="Destination file (default: ./htdocs/service-worker.js)" | ||
) | ||
@click.option( | ||
'--cache', | ||
type=click.Path(), | ||
multiple=True, | ||
help="File(s) or directories to cache" | ||
) | ||
@click.option( | ||
'--timeout', | ||
type=int, | ||
default=400, | ||
help="Timeout to use before falling back to cache." | ||
) | ||
def serviceworker(version, template, output, cache, timeout): | ||
""" | ||
Generate a service-worker.js. The default service worker will function as | ||
follows: | ||
\b | ||
1. While installing, precache all of the listed paths. | ||
2. While running, attempt to fetch (and re-cache) the latest version | ||
of each file, but fall back to the cached version after the specified | ||
timeout. | ||
This ensures the app can work offline as needed, without requiring a double | ||
refresh whenever a new version is available. Specify the --template option | ||
to override the default. | ||
The list of files to cache should be relative to the directory containing | ||
service-worker.js. Wildcard paths will be resolved to existing filenames, | ||
so this command should generally be run *after* ./manage.py collectstatic. | ||
Note: If you are using wq with npm, a service worker will be generated by | ||
create-react-app, so you do not need to use this command. | ||
""" | ||
|
||
if not template: | ||
template = SW_TMPL | ||
|
||
basedir = os.path.dirname(output) | ||
paths = [] | ||
for path in cache: | ||
if '*' not in path: | ||
paths.append(path) | ||
continue | ||
if path.startswith('/'): | ||
path = path[1:] | ||
for filename in glob(os.path.join(basedir, path)): | ||
paths.append(filename.replace(basedir, '')) | ||
|
||
cache = ",".join(json.dumps(path) for path in paths) | ||
|
||
with open(output, 'w') as f: | ||
f.write( | ||
template.replace("{{VERSION}}", version) | ||
.replace("{{CACHE}}", cache) | ||
.replace("{{TIMEOUT}}", str(timeout)) | ||
) | ||
|
||
|
||
SW_TMPL = """const CACHE_NAME = 'wq-cache-v1'; | ||
// App Version {{VERSION}} | ||
const cacheUrls = [{{CACHE}}]; | ||
self.addEventListener('install', event => { | ||
event.waitUntil( | ||
caches.open(CACHE_NAME).then(cache => cache.addAll(cacheUrls)) | ||
); | ||
}); | ||
self.addEventListener('fetch', event => { | ||
if (!cacheUrls.includes(new URL(event.request.url).pathname)) { | ||
return; | ||
} | ||
event.respondWith( | ||
new Promise((resolve, reject) => { | ||
const timeout = setTimeout(reject, {{TIMEOUT}}); | ||
fetch(event.request).then(response => { | ||
clearTimeout(timeout); | ||
const cacheResponse = response.clone(); | ||
caches | ||
.open(CACHE_NAME) | ||
.then(cache => cache.put(event.request, cacheResponse)); | ||
resolve(response); | ||
}, reject); | ||
}).catch(() => { | ||
return caches | ||
.open(CACHE_NAME) | ||
.then(cache => cache.match(event.request)) | ||
.then(response => response || Promise.reject('no-match')); | ||
}) | ||
); | ||
}); | ||
""" |