瀏覽代碼

Fix Error

Akash 7 年之前
父節點
當前提交
628139ff34
共有 3 個文件被更改,包括 37 次插入43 次删除
  1. 4 3
      client/pages/_document.js
  2. 31 38
      server/offline/sw.js
  3. 2 2
      server/server.js

+ 4 - 3
client/pages/_document.js

@@ -39,8 +39,8 @@ class AppDocument extends Document {
           <link rel="icon" sizes="16x16" href="/images/favicon-16x16.png" />
           <link rel="apple-touch-icon" href="/images/favicon-196x196.png" />
           <link rel="mask-icon" href="/images/icon.svg" color="blue" />
-          <link rel = "manifest" href = "manifest.webmanifest" />
-          <meta name = "theme-color" content = "#f3f3f3" />
+          <link rel="manifest" href="manifest.webmanifest" />
+          <meta name="theme-color" content="#f3f3f3" />
 
           <meta property="fb:app_id" content="123456789" />
           <meta property="og:url" content="https://kutt.it" />
@@ -61,7 +61,8 @@ class AppDocument extends Document {
             }}
           />
 
-          <script dangerouslySetInnerHTML = {{
+          <script
+            dangerouslySetInnerHTML={{
               __html: `
                 if('serviceWorker' in navigator) {
                   navigator.serviceWorker.register('sw.js', {

+ 31 - 38
server/offline/sw.js

@@ -1,45 +1,38 @@
-//This is the "Offline copy of pages" service worker
+// This is the "Offline copy of pages" service worker
 
-//Install stage sets up the index page (home page) in the cache and opens a new cache
-const {self} = window;
+// Install stage sets up the index page (home page) in the cache and opens a new cache
+const { self } = window;
 
-self.addEventListener('install', function(event) {
-    var indexPage = new Request('index.html');
-    event.waitUntil(
-        fetch(indexPage).then(function(response) {
-            return caches.open('kutt-offline').then(function(cache) {
-                console.log('Kutt Cached index page during Install' + response.url);
-                return cache.put(indexPage, response);
-            });
-        }));
+self.addEventListener('install', event => {
+  const indexPage = new Request('index.html');
+  event.waitUntil(
+    fetch(indexPage).then(response =>
+      caches.open('kutt-offline').then(cache => cache.put(indexPage, response))
+    )
+  );
 });
 
-//If any fetch fails, it will look for the request in the cache and serve it from there first
-self.addEventListener('fetch', function(event) {
-    var updateCache = function(request) {
-        return caches.open('kutt-offline').then(function(cache) {
-            return fetch(request).then(function(response) {
-                console.log('Kutt add page to offline' + response.url)
-                return cache.put(request, response);
-            });
-        });
-    };
+// If any fetch fails, it will look for the request in the cache and serve it from there first
+self.addEventListener('fetch', event => {
+  const updateCache = request =>
+    caches
+      .open('kutt-offline')
+      .then(cache => fetch(request).then(response => cache.put(request, response)));
 
-    event.waitUntil(updateCache(event.request));
+  event.waitUntil(updateCache(event.request));
 
-    event.respondWith(
-        fetch(event.request).catch(function(error) {
-            console.log('Kutt Network request Failed. Serving content from cache: ' + error);
-
-            //Check to see if you have it in the cache
-            //Return response
-            //If not in the cache, then return error page
-            return caches.open('kutt-offline').then(function(cache) {
-                return cache.match(event.request).then(function(matching) {
-                    var report = !matching || matching.status == 404 ? Promise.reject(new Error('no-match')) : matching;
-                    return report
-                });
-            });
+  event.respondWith(
+    fetch(event.request).catch(() =>
+      // Check to see if you have it in the cache
+      // Return response
+      // If not in the cache, then return error page
+      caches.open('kutt-offline').then(cache =>
+        cache.match(event.request).then(matching => {
+          const report =
+            !matching || matching.status === 404 ? Promise.reject(new Error('no-match')) : matching;
+          return report;
         })
-    );
-})
+      )
+    )
+  );
+});

+ 2 - 2
server/server.js

@@ -87,8 +87,8 @@ app.prepare().then(() => {
     app.render(req, res, '/verify', req.user)
   );
   server.get('/sw.js', (_req, res) => {
-    res.sendFile(__dirname + '/offline/sw.js')
-});
+    res.sendFile(`${__dirname}/offline/sw.js`);
+  });
 
   /* User and authentication */
   server.post('/api/auth/signup', validationCriterias, validateBody, catchErrors(auth.signup));