Pārlūkot izejas kodu

Fixes to score 100% Lighthouse report

Herton 4 gadi atpakaļ
vecāks
revīzija
447625b08b

+ 3 - 3
htmlServer.js

@@ -18,7 +18,7 @@ const types = {
 };
 /* Simple media path validation for single page app */
 function getType(path) {
-    if (path.indexOf(`media/`) == -1) return 'html';
+    if (path == '/' || path.indexOf(`.html`) != -1) return 'html';
     for (const type in types) {
         if (path.indexOf(`.${type}`) != -1) return type;
     }
@@ -32,9 +32,9 @@ const server = http.createServer((req, res) => {
     res.writeHead(200, {
         'Content-Type': mediaType == 'html' ? 'text/html' : types[mediaType]
     });
-    const filePath = mediaType != 'html' ?  public + path : htmlFile;
+    const filePath = mediaType == 'html' ? htmlFile : public + path;
     res.end(fs.readFileSync(filePath));
 });
 
 server.listen(port, '127.0.0.1');
-console.info('Server running on port '+port)
+console.info(`Server running on port ${port}`)

BIN
public/android-chrome-192x192.png


BIN
public/android-chrome-512x512.png


BIN
public/apple-touch-icon.png


BIN
public/favicon-16x16.png


BIN
public/favicon-32x32.png


BIN
public/favicon.ico


+ 7 - 3
public/index.html

@@ -6,12 +6,16 @@
     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     <title>Basic single file HTML</title>
     <meta name="description" content="Simgle file page basic page">
-    <link rel="stylesheet" type="text/css" href="media/styles.css">
-    <script type="text/javascript" src="media/scripts.js"></script>
+    <link rel="preload" href="styles.css" as="style" onload="this.onload=null;this.rel='stylesheet'">
+    <noscript><link rel="stylesheet" href="styles.css"></noscript>
+    <script type="text/javascript" src="scripts.js" async></script>
+    <link rel="manifest" href="manifest.json">
+    <meta name="theme-color" content="white">
+    <link rel="apple-touch-icon" href="apple-touch-icon.png">
 </head>
 <body>
     <div class="wrapper">
-        <img src="media/favicon.ico" alt="icon"/>
+        <img src="favicon-32x32.png" width="16" height="16" alt="icon"/>
         <p>Basic Node Single file Server Setup</p>
     </div>
 </body>

+ 28 - 0
public/manifest.json

@@ -0,0 +1,28 @@
+{
+    "short_name": "SimpleServer",
+    "name": "Simple Single Page Server Setup",
+    "icons": [
+      {
+        "src": "android-chrome-192x192.png",
+        "type": "image/png",
+        "sizes": "192x192"
+      },
+      {
+        "src": "android-chrome-512x512.png",
+        "type": "image/png",
+        "sizes": "512x512"
+      },
+      {
+        "src": "apple-touch-icon.png",
+        "type": "image/png",
+        "sizes": "180x180",
+        "purpose": "any maskable"
+      }
+    ],
+    "start_url": "/?source=pwa",
+    "background_color": "#3367D6",
+    "display": "standalone",
+    "scope": "/",
+    "prefer_related_applications": false,
+    "theme_color": "white"
+  }

BIN
public/media/favicon.ico


+ 0 - 1
public/media/robots.txt

@@ -1 +0,0 @@
-User-agent: * Disallow: /media

+ 0 - 1
public/media/scripts.js

@@ -1 +0,0 @@
-console.log('Script loaded!');

+ 1 - 0
public/robots.txt

@@ -0,0 +1 @@
+User-agent: * Disallow:

+ 9 - 0
public/scripts.js

@@ -0,0 +1,9 @@
+
+'use strict';
+
+window.onload = () => {
+    if ('serviceWorker' in navigator) {
+        navigator.serviceWorker.register('./sw.js');
+        console.info('serviceWorker loaded!');
+    }
+}

public/media/styles.css → public/styles.css


+ 13 - 0
public/sw.js

@@ -0,0 +1,13 @@
+self.addEventListener('fetch', function(event) {
+    event.respondWith(async function() {
+        try{
+            var res = await fetch(event.request);
+            var cache = await caches.open('cache');
+            cache.put(event.request.url, res.clone());
+            return res;
+        }
+        catch (error) {
+            return caches.match(event.request);
+        }
+    }());
+});