-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
149 additions
and
0 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
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, | ||
} |
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,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); | ||
} | ||
}()); | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,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" | ||
} | ||
] | ||
} |
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