Skip to content

Commit 0ed92bd

Browse files
committed
Implement PWA Support
1 parent 080a4b9 commit 0ed92bd

File tree

6 files changed

+149
-0
lines changed

6 files changed

+149
-0
lines changed

source/_extensions/pwa_service.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import sphinx as Sphinx
2+
from typing import Any, Dict, List
3+
import os
4+
from docutils import nodes
5+
import json
6+
import shutil
7+
8+
manifest = {
9+
"name": "",
10+
"short_name": "",
11+
"theme_color": "",
12+
"background_color": "",
13+
"display": "standalone",
14+
"scope": "/",
15+
"start_url": "/index.html",
16+
"icons": [],
17+
}
18+
19+
20+
def get_files_to_cache(outDir: str):
21+
files_to_cache = []
22+
for (dirpath, dirname, filenames) in os.walk(outDir):
23+
dirpath = dirpath.split(outDir)[1]
24+
25+
# skip adding sources to cache
26+
if os.sep + "_sources" + os.sep in dirpath:
27+
continue
28+
29+
# add files to cache
30+
for name in filenames:
31+
if "sw.js" in name:
32+
continue
33+
34+
dirpath = dirpath.replace("\\", "/")
35+
dirpath = dirpath.lstrip("/")
36+
if dirpath == "":
37+
files_to_cache.append(name)
38+
else:
39+
files_to_cache.append(dirpath + "/" + name)
40+
41+
return files_to_cache
42+
43+
44+
def build_finished(app: Sphinx, exception: Exception):
45+
outDir = app.outdir
46+
outDirStatic = outDir + os.sep + "_static" + os.sep
47+
files_to_cache = get_files_to_cache(outDir)
48+
49+
# dumps a json file with our cache
50+
with open(outDirStatic + "cache.json", "w") as f:
51+
json.dump(files_to_cache, f)
52+
53+
# copies over our service worker
54+
shutil.copyfile(
55+
os.path.dirname(__file__) + os.sep + "pwa_service_files" + os.sep + "sw.js",
56+
outDir + os.sep + "sw.js",
57+
)
58+
59+
60+
def html_page_context(
61+
app: Sphinx,
62+
pagename: str,
63+
templatename: str,
64+
context: Dict[str, Any],
65+
doctree: nodes.document,
66+
) -> None:
67+
if pagename == "index":
68+
context[
69+
"metatags"
70+
] += '<script>"serviceWorker"in navigator&&navigator.serviceWorker.register("sw.js").catch((e) => window.alert(e));</script>'
71+
context[
72+
"metatags"
73+
] += f'<link rel="manifest" href="_static/frcdocs.webmanifest"/>'
74+
75+
76+
def setup(app: Sphinx) -> Dict[str, Any]:
77+
app.add_config_value("name", "", "html")
78+
app.add_config_value("short_name", "", "html")
79+
app.add_config_value("theme_color", "", "html")
80+
app.add_config_value("background_color", "", "html")
81+
app.add_config_value("display", "standalone", "html")
82+
app.add_config_value("icons", [], "html")
83+
84+
app.connect("html-page-context", html_page_context)
85+
app.connect("build-finished", build_finished)
86+
87+
return {
88+
"parallel_read_safe": True,
89+
"parallel_write_safe": True,
90+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// extend this to update the service worker every push
2+
// https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps/Offline_Service_workers
3+
var cacheName = 'js13kPWA-v1';
4+
5+
self.addEventListener('install', function(e) {
6+
e.waitUntil(
7+
caches.open('frc-docs').then(function(cache) {
8+
fetch('_static/cache.json')
9+
.then(response => response.json())
10+
.then(async (data) => {
11+
for (let i = 0; i < data.length / 10; i++) {
12+
const tofetch = data.slice(i * 10, i * 10 + 10)
13+
await cache.addAll(tofetch)
14+
}
15+
})
16+
.catch(error => {
17+
console.error(error);
18+
});
19+
})
20+
);
21+
});
22+
23+
self.addEventListener('fetch', function(event) {
24+
event.respondWith(async function() {
25+
try{
26+
console.log(event.request.url)
27+
console.log(caches.match(event.request))
28+
var res = await fetch(event.request);
29+
var cache = await caches.open('frc-docs');
30+
cache.put(event.request.url, res.clone());
31+
return res;
32+
}
33+
catch(error){
34+
return caches.match(event.request);
35+
}
36+
}());
37+
});

source/_static/first-logo-256px.png

19.7 KB
Loading

source/_static/first-logo-512px.png

43.3 KB
Loading

source/_static/frcdocs.webmanifest

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "FRC Docs",
3+
"short_name": "FRC Docs",
4+
"theme_color": "#003974",
5+
"background_color": "#003974",
6+
"display": "standalone",
7+
"scope": "../",
8+
"start_url": "../index.html",
9+
"icons": [
10+
{
11+
"src": "/_static/first-logo-256px.png",
12+
"type": "image/png",
13+
"sizes": "256x256"
14+
},
15+
{
16+
"src": "/_static/first-logo-512px.png",
17+
"type": "image/png",
18+
"sizes": "512x512"
19+
}
20+
]
21+
}

source/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"_extensions.post_process",
6060
"_extensions.rtd_patch",
6161
"_extensions.localization",
62+
"_extensions.pwa_service",
6263
]
6364

6465
extensions += local_extensions

0 commit comments

Comments
 (0)