2017-03-29 58 views
2

我已經創建了新項目PWA。 當我開發沒有使用節點js的(socket.io運行)只是像我期望和需要離線運行。服務工作人員不能在使用nodejs服務器的脫機模式下運行

但是當與nodejs服務器集成時。當上網時,運行良好的應用程序不會有任何問題。但是當我切換到離線模式時,我的服務工作不運行,但顯示離線瀏覽器。

這是我的代碼節點JS服務器:在指數JS

if ('serviceWorker' in navigator && 'PushManager' in window) { 
 
\t \t console.log('Service Worker and Push is supported'); 
 

 
\t window.addEventListener('load', function() { 
 
\t \t \t navigator.serviceWorker 
 
      .register('./service-worker.js') 
 
      .then(function() { console.log('Service Worker Registered'); }); 
 
\t }); 
 
\t }

和THI

var express = require('express'); 
 
var app = express(); 
 
var server = require('http').Server(app); 
 
var io = require('socket.io')(server); 
 
var cors = require('cors'); 
 

 
app.use(cors()); 
 
app.use(express.static(__dirname + '/')); 
 

 
app.use(function(req, res, next){ 
 
    res.header('Access-Control-Allow-Origin', '*'); 
 
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); 
 
    res.header('Access-Control-Allow-Headers', 'Content-Type'); 
 
    next(); 
 
}); 
 

 

 
io.on('connection', function(socket) { 
 
    console.log('new connection'); 
 

 
    socket.on('afterBid', function(data) { 
 
    io.emit('bcCurrentBid', { 
 
     current_bidding: data.auction_current_bidding, 
 
     user_id_dominated: data.user_id_dominated 
 
    }); 
 
    }); 
 

 
}); 
 

 
server.listen(9991, function() { 
 
    console.log('server up and running at 2205 port'); 
 
});

,這是我的服務註冊s是我的服務worker.js文件:

var cacheName = 'Auction-v2'; 
 
var filesToCache = [ 
 
    'index.html', 
 
    'server.js', 
 
    '/', 
 
    './app/app.js', 
 
    './app/listAuctionController.js', 
 
    './app/auctionDetailController.js', 
 
    './app/service/auctionDataService.js', 
 
    './media/frontend/images/auction_logo_white.png', 
 
    // './media/frontend/', 
 
    // './media/catalog/', 
 
    // './view/', 
 
    './lib/css/materialize.min.css', 
 
    './lib/css/owl.carousel.min.css', 
 
    './lib/css/jquery.countdown.css', 
 
    './lib/css/owl.theme.default.min.css', 
 
    './lib/css/materialize-icon.css', 
 
    './lib/js/jquery.min.js', 
 
    './lib/js/jquery.lazyLoad.js', 
 
    './lib/js/jquery.maskMoney.js', 
 
    './lib/js/jquery.countdown.min.js', 
 
    './lib/js/materialize.min.js', 
 
    './lib/js/owl.carousel.min.js', 
 
    './lib/js/angular/angular.min.js', 
 
    './lib/js/angular/angular-route.min.js', 
 
    './lib/js/angular/angular-sanitize.min.js', 
 
    './lib/js/angular/angular-locale_id-id.js', 
 
    './lib/js/angular/angular-animate.min.js', 
 
]; 
 

 
var dataCacheName = 'Auction-Data-v1'; 
 

 
// SW Install 
 
self.addEventListener('install', function(e) { 
 
    console.log('[ServiceWorker] Install'); 
 
    e.waitUntil(
 
    caches.open(cacheName).then(function(cache) { 
 
     console.log('[ServiceWorker] Caching app shell'); 
 
     return cache.addAll(filesToCache); 
 
    }) 
 
); 
 
}); 
 

 

 

 

 
self.addEventListener('fetch', function(e) { 
 
    // console.log('[Service Worker] Fetch', e.request.url); 
 
    var dataUrl = '/backendFrame/public/api/v1/'; 
 
    var dataUrl2 = '/view/'; 
 
    if (e.request.url.indexOf(dataUrl) > -1 || e.request.url.indexOf(dataUrl2)) { 
 
    /* 
 
    * When the request URL contains dataUrl, the app is asking for fresh 
 
    * weather data. In this case, the service worker always goes to the 
 
    * network and then caches the response. This is called the "Cache then 
 
    * network" strategy: 
 
    * https://jakearchibald.com/2014/offline-cookbook/#cache-then-network 
 
    */ 
 
    e.respondWith(
 
     caches.open(dataCacheName).then(function(cache) { 
 
     return fetch(e.request).then(function(response){ 
 
      // console.log('url to cache =' + e.request.url); 
 
      cache.put(e.request.url, response.clone()); 
 
      return response; 
 
     }); 
 
     }) 
 
    ); 
 
    } else { 
 
    /* 
 
    * The app is asking for app shell files. In this scenario the app uses the 
 
    * "Cache, falling back to the network" offline strategy: 
 
    * https://jakearchibald.com/2014/offline-cookbook/#cache-falling-back-to-network 
 
    */ 
 
    e.respondWith(
 
     caches.match(e.request).then(function(response) { 
 
     return response || fetch(e.request); 
 
     }) 
 
    ); 
 
    } 
 
}); 
 

 
// SW Activate 
 
self.addEventListener('activate', function(e) { 
 
    console.log('[ServiceWorker] Activate'); 
 
    e.waitUntil(
 
    caches.keys().then(function(keyList) { 
 
     return Promise.all(keyList.map(function(key) { 
 
     if (key !== cacheName && key !== dataCacheName) { 
 
      console.log('[ServiceWorker] Removing old cache', key); 
 
      return caches.delete(key); 
 
     } 
 
     })); 
 
    }) 
 
); 
 
    return self.clients.claim(); 
 
});

感謝也許有人可以給我的解決方案

+0

沒有看到你的'service-worker.js'的內容,很難給出反饋。一個普遍的建議是,服務人員不能攔截WebSocket API請求/響應,即不能處理你的socket.io流量。 –

+0

@JeffPosnick但可以服務worker和nodejs服務器運行嗎?..我只需要運行我的節點js服務器,當離線模式服務工作人員能夠顯示來自緩存的應用程序 –

回答

1

的服務人員僅僅是一個JavaScript文件,從您的網絡服務器提供服務。無論它是否是node.js都沒關係。

您是否檢查過chrome調試工具中的'application'選項卡以查看服務工作者是否已加載?如果是,請分享您的'service-worker.js'文件的代碼。 註冊服務人員時,您也可以添加'catch'部分。這可能會告訴你,如果服務人員無法加載,會發生什麼情況。

window.addEventListener('load', function() { 
 
\t \t navigator.serviceWorker 
 
    .register('./service-worker.js') 
 
    .then(function() { console.log('Service Worker Registered'); }) 
 
    
 
    .catch(function (err) { 
 
      console.log('ServiceWorker registration failed: ', err); 
 
    }); 
 
    
 
});

+0

我編輯我的問題,並添加我的服務人員。 js文件 我得到的問題是..當我沒有nodejs運行時,我的應用程序可以在服務工作者的離線模式下運行@Sorskoot –

0

我在這裏發表類似的問題,並得到一個答案: Service Worker not working in Offline mode with node js server

要長話短說,牢記服務人員的範圍是它自己的目錄和文件夾下面...它不能訪問你的lib或應用程序目錄...

更詳細地說,這意味着如果你的內容開始於https://example.com/,你的服務人員必須住在https://example.com/service-worker.js。如果您將其設置爲https://example.com/js/service-worker.js,它將無法正常工作。

相關問題