Skip to content

Commit

Permalink
Implement PWA Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Daltz333 committed Feb 9, 2022
1 parent 080a4b9 commit 0ed92bd
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 0 deletions.
90 changes: 90 additions & 0 deletions source/_extensions/pwa_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import sphinx as Sphinx
from typing import Any, Dict, List
import os
from docutils import nodes
import json
import shutil

manifest = {
"name": "",
"short_name": "",
"theme_color": "",
"background_color": "",
"display": "standalone",
"scope": "/",
"start_url": "/index.html",
"icons": [],
}


def get_files_to_cache(outDir: str):
files_to_cache = []
for (dirpath, dirname, filenames) in os.walk(outDir):
dirpath = dirpath.split(outDir)[1]

# skip adding sources to cache
if os.sep + "_sources" + os.sep in dirpath:
continue

# add files to cache
for name in filenames:
if "sw.js" in name:
continue

dirpath = dirpath.replace("\\", "/")
dirpath = dirpath.lstrip("/")
if dirpath == "":
files_to_cache.append(name)
else:
files_to_cache.append(dirpath + "/" + name)

return files_to_cache


def build_finished(app: Sphinx, exception: Exception):
outDir = app.outdir
outDirStatic = outDir + os.sep + "_static" + os.sep
files_to_cache = get_files_to_cache(outDir)

# dumps a json file with our cache
with open(outDirStatic + "cache.json", "w") as f:
json.dump(files_to_cache, f)

# copies over our service worker
shutil.copyfile(
os.path.dirname(__file__) + os.sep + "pwa_service_files" + os.sep + "sw.js",
outDir + os.sep + "sw.js",
)


def html_page_context(
app: Sphinx,
pagename: str,
templatename: str,
context: Dict[str, Any],
doctree: nodes.document,
) -> None:
if pagename == "index":
context[
"metatags"
] += '<script>"serviceWorker"in navigator&&navigator.serviceWorker.register("sw.js").catch((e) => window.alert(e));</script>'
context[
"metatags"
] += f'<link rel="manifest" href="_static/frcdocs.webmanifest"/>'


def setup(app: Sphinx) -> Dict[str, Any]:
app.add_config_value("name", "", "html")
app.add_config_value("short_name", "", "html")
app.add_config_value("theme_color", "", "html")
app.add_config_value("background_color", "", "html")
app.add_config_value("display", "standalone", "html")
app.add_config_value("icons", [], "html")

app.connect("html-page-context", html_page_context)
app.connect("build-finished", build_finished)

return {
"parallel_read_safe": True,
"parallel_write_safe": True,
}
37 changes: 37 additions & 0 deletions source/_extensions/pwa_service_files/sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// extend this to update the service worker every push
// https://developer.mozilla.org/en-US/docs/Web/Progressive_web_apps/Offline_Service_workers
var cacheName = 'js13kPWA-v1';

self.addEventListener('install', function(e) {
e.waitUntil(
caches.open('frc-docs').then(function(cache) {
fetch('_static/cache.json')
.then(response => response.json())
.then(async (data) => {
for (let i = 0; i < data.length / 10; i++) {
const tofetch = data.slice(i * 10, i * 10 + 10)
await cache.addAll(tofetch)
}
})
.catch(error => {
console.error(error);
});
})
);
});

self.addEventListener('fetch', function(event) {
event.respondWith(async function() {
try{
console.log(event.request.url)
console.log(caches.match(event.request))
var res = await fetch(event.request);
var cache = await caches.open('frc-docs');
cache.put(event.request.url, res.clone());
return res;
}
catch(error){
return caches.match(event.request);
}
}());
});
Binary file added source/_static/first-logo-256px.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added source/_static/first-logo-512px.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 21 additions & 0 deletions source/_static/frcdocs.webmanifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "FRC Docs",
"short_name": "FRC Docs",
"theme_color": "#003974",
"background_color": "#003974",
"display": "standalone",
"scope": "../",
"start_url": "../index.html",
"icons": [
{
"src": "/_static/first-logo-256px.png",
"type": "image/png",
"sizes": "256x256"
},
{
"src": "/_static/first-logo-512px.png",
"type": "image/png",
"sizes": "512x512"
}
]
}
1 change: 1 addition & 0 deletions source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"_extensions.post_process",
"_extensions.rtd_patch",
"_extensions.localization",
"_extensions.pwa_service",
]

extensions += local_extensions
Expand Down

0 comments on commit 0ed92bd

Please sign in to comment.