Bläddra i källkod

convert to pwa app

Karl Hudgell 3 veckor sedan
förälder
incheckning
e1d48bf7de

+ 4 - 1
app.py

@@ -1,5 +1,5 @@
 # app.py
-from flask import Flask, render_template, request, redirect, url_for, session, flash
+from flask import Flask, render_template, request, redirect, url_for, session, flash, send_file
 from flask_caching import Cache
 import requests.auth
 import os
@@ -17,6 +17,9 @@ app.config.from_object(
 ) 
 cache = Cache(app, config={"CACHE_TYPE": "SimpleCache"})
 
+@app.route('/manifest.json')
+def serve_manifest():
+    return send_file('manifest.json', mimetype='application/manifest+json')
 
 @app.route("/favicon.ico")
 def favicon():

BIN
static/icons/android-launchericon-144-144.png


BIN
static/icons/android-launchericon-192-192.png


BIN
static/icons/android-launchericon-512-512.png


+ 29 - 0
static/manifest.json

@@ -0,0 +1,29 @@
+{
+    "name": "KTVManager",
+    "short_name": "KTV",
+    "start_url": "https://tv-ui.k-world.me.uk/",
+    "scope": "/",
+    "display": "standalone",
+    "theme_color": "#317EFB",
+    "background_color": "#317EFB",
+    "icons": [
+        {
+            "src": "/static/icons/android-launchericon-144-144.png",
+            "sizes": "144x144",
+            "type": "image/png",
+            "purpose": "any maskable"
+        },
+        {
+            "src": "/static/icons/android-launchericon-192-192.png",
+            "sizes": "192x192",
+            "type": "image/png",
+            "purpose": "any maskable"
+        },
+        {
+            "src": "/static/icons/android-launchericon-512-512.png",
+            "sizes": "512x512",
+            "type": "image/png",
+            "purpose": "any maskable"
+        }
+    ]
+}

+ 22 - 0
static/service-worker.js

@@ -0,0 +1,22 @@
+self.addEventListener('install', function (event) {
+    event.waitUntil(
+        caches.open('my-cache').then(function (cache) {
+            return cache.addAll([
+                '/',
+                '/index.html',
+                '/static/styles.css',
+                '/static/script.js',
+                '/static/icons/icon-192x192.png',
+                '/static/icons/icon-512x512.png'
+            ]);
+        })
+    );
+});
+
+self.addEventListener('fetch', function (event) {
+    event.respondWith(
+        caches.match(event.request).then(function (response) {
+            return response || fetch(event.request);
+        })
+    );
+});

+ 15 - 0
templates/index.html

@@ -6,6 +6,7 @@
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>KTV Manager</title>
     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
+    <link rel="manifest" href="{{ url_for('static', filename='manifest.json') }}">
 </head>
 <body>
 
@@ -58,5 +59,19 @@
     <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
     <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.0.7/dist/umd/popper.min.js"></script>
     <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
+    <script>
+        if ('serviceWorker' in navigator) {
+          window.addEventListener('load', function() {
+            navigator.serviceWorker.register("{{ url_for('static', filename='service-worker.js') }}")
+              .then(function(registration) {
+                console.log('Service Worker registered with scope: ', registration.scope);
+              })
+              .catch(function(error) {
+                console.log('Service Worker registration failed: ', error);
+              });
+          });
+        }
+      </script>
+      
 </body>
 </html>