Преглед на файлове

Serve the service worker off of `/service-worker`

Trevor Elliott преди 3 години
родител
ревизия
621c556361
променени са 3 файла, в които са добавени 26 реда и са изтрити 1 реда
  1. 1 1
      js/index.js
  2. 19 0
      js/serviceWorker.js
  3. 6 0
      lc/app.py

+ 1 - 1
js/index.js

@@ -72,7 +72,7 @@ document.addEventListener("DOMContentLoaded", () => {
 
 if ('serviceWorker' in navigator) {
   navigator.serviceWorker
-    .register('/static/serviceWorker.js')
+    .register('/service-worker')
     .then(function(registration) {
       console.log("ServiceWorker registration successful with scope: ", registration.scope);
     }).catch(function(err) {

+ 19 - 0
js/serviceWorker.js

@@ -0,0 +1,19 @@
+
+self.addEventListener("install", event => {
+  console.log("installing...");
+});
+
+self.addEventListener("activate", event => {
+  console.log("activate!");
+});
+
+self.addEventListener("fetch", event => {
+  console.log("start service worker");
+
+  event.respondWith(
+    fetch(event.request).then(response => {
+      console.log("fetching %s", event.request.url);
+      return response;
+    })
+  );
+});

+ 6 - 0
lc/app.py

@@ -290,3 +290,9 @@ class PinboardImport(Endpoint):
             raise e.BadFileUpload("no file selected")
         u.import_pinboard_data(file.stream)
         return self.api_ok(u.base_url())
+
+
+@endpoint("/service-worker")
+class ServiceWorker(Endpoint):
+    def route(self, *args, **kwargs):
+        return flask.send_file("../js/serviceWorker.js", mimetype="text/javascript")