如何运行离线"Progressive Web Apps"?
How to run offline "Progressive Web Apps"?
我是渐进式 Web 应用程序开发的新手。我想实现渐进式网络应用程序。所以我已经实现了一个演示页面,这个页面在网络连接下工作正常但没有网络(离线)它不工作。
我想在没有任何互联网连接(离线)的情况下打开我的进步网站。我看过一个 link https://developers.google.com/web/fundamentals/getting-started/codelabs/offline/。我已经实现了 service worker javascript 文件。
我一步一步来解释:
第一步:
我正在使用 网络服务器 chrome:
第二步:
index.html
// Register the service worker if available.
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./service-worker.js').then(function(reg) {
console.log('Successfully registered service worker', reg);
}).catch(function(err) {
console.warn('Error whilst registering service worker', err);
});
}
window.addEventListener('online', function(e) {
// Resync data with server.
console.log("You are online");
Page.hideOfflineWarning();
Arrivals.loadData();
}, false);
window.addEventListener('offline', function(e) {
// Queue up events for server.
console.log("You are offline");
Page.showOfflineWarning();
}, false);
// Check if the user is connected.
if (navigator.onLine) {
Arrivals.loadData();
} else {
// Show offline message
Page.showOfflineWarning();
}
// Set Knockout view model bindings.
ko.applyBindings(Page.vm);
服务-worker.js
// Use a cacheName for cache versioning
var cacheName = 'v1:static';
// During the installation phase, you'll usually want to cache static assets.
self.addEventListener('install', function(e) {
// Once the service worker is installed, go ahead and fetch the resources to make this work offline.
e.waitUntil(
caches.open(cacheName).then(function(cache) {
return cache.addAll([
'./index.html',
'./screen.css',
'./script.js',
'./styles/app.css',
'./styles/font.css',
'./styles/header.css',
'./styles/hidden.css',
'./styles/list.css',
'./styles/page.css',
'./styles/suggest.css',
'./styles/tags.css',
]).then(function() {
self.skipWaiting();
});
})
);
});
// when the browser fetches a URL…
self.addEventListener('fetch', function(event) {
// … either respond with the cached object or go ahead and fetch the actual URL
event.respondWith(
caches.match(event.request).then(function(response) {
if (response) {
// retrieve from cache
return response;
}
// fetch as normal
return fetch(event.request);
})
);
});
第三步
签入网络:
签到申请:
Service-worker.js 文件工作正常,您可以在屏幕截图中看到:
但是当我点击离线复选框时,我的网站无法运行。如果所有这些事情都以正确的方式进行,那么它必须离线打开。
如果有任何遗漏,请告诉我。请不要拒绝这个问题。如果有人有想法,请分享。
If anybody has doubt then see this link https://pwa.rocks/. You can
open this link in chrome and after then without internet connection it
will open.
如果需要解释请向我询问。
在处理 fetch
事件时,根请求 /
需要一个额外的条件:
self.addEventListener('fetch', function(event) {
// … either respond with the cached object or go ahead and fetch the actual URL
event.respondWith(
caches.match(event.request).then(function(response) {
if (response) {
// retrieve from cache
return response;
}
// if not found in cache, return default offline content (only if this is a navigation request)
if (event.request.mode === 'navigate') {
return caches.match('./index.html');
}
// fetch as normal
return fetch(event.request);
})
);
});
我是渐进式 Web 应用程序开发的新手。我想实现渐进式网络应用程序。所以我已经实现了一个演示页面,这个页面在网络连接下工作正常但没有网络(离线)它不工作。
我想在没有任何互联网连接(离线)的情况下打开我的进步网站。我看过一个 link https://developers.google.com/web/fundamentals/getting-started/codelabs/offline/。我已经实现了 service worker javascript 文件。
我一步一步来解释:
第一步:
我正在使用 网络服务器 chrome:
第二步: index.html
// Register the service worker if available.
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./service-worker.js').then(function(reg) {
console.log('Successfully registered service worker', reg);
}).catch(function(err) {
console.warn('Error whilst registering service worker', err);
});
}
window.addEventListener('online', function(e) {
// Resync data with server.
console.log("You are online");
Page.hideOfflineWarning();
Arrivals.loadData();
}, false);
window.addEventListener('offline', function(e) {
// Queue up events for server.
console.log("You are offline");
Page.showOfflineWarning();
}, false);
// Check if the user is connected.
if (navigator.onLine) {
Arrivals.loadData();
} else {
// Show offline message
Page.showOfflineWarning();
}
// Set Knockout view model bindings.
ko.applyBindings(Page.vm);
服务-worker.js
// Use a cacheName for cache versioning
var cacheName = 'v1:static';
// During the installation phase, you'll usually want to cache static assets.
self.addEventListener('install', function(e) {
// Once the service worker is installed, go ahead and fetch the resources to make this work offline.
e.waitUntil(
caches.open(cacheName).then(function(cache) {
return cache.addAll([
'./index.html',
'./screen.css',
'./script.js',
'./styles/app.css',
'./styles/font.css',
'./styles/header.css',
'./styles/hidden.css',
'./styles/list.css',
'./styles/page.css',
'./styles/suggest.css',
'./styles/tags.css',
]).then(function() {
self.skipWaiting();
});
})
);
});
// when the browser fetches a URL…
self.addEventListener('fetch', function(event) {
// … either respond with the cached object or go ahead and fetch the actual URL
event.respondWith(
caches.match(event.request).then(function(response) {
if (response) {
// retrieve from cache
return response;
}
// fetch as normal
return fetch(event.request);
})
);
});
第三步
签入网络:
签到申请:
Service-worker.js 文件工作正常,您可以在屏幕截图中看到:
但是当我点击离线复选框时,我的网站无法运行。如果所有这些事情都以正确的方式进行,那么它必须离线打开。
如果有任何遗漏,请告诉我。请不要拒绝这个问题。如果有人有想法,请分享。
If anybody has doubt then see this link https://pwa.rocks/. You can open this link in chrome and after then without internet connection it will open.
如果需要解释请向我询问。
在处理 fetch
事件时,根请求 /
需要一个额外的条件:
self.addEventListener('fetch', function(event) {
// … either respond with the cached object or go ahead and fetch the actual URL
event.respondWith(
caches.match(event.request).then(function(response) {
if (response) {
// retrieve from cache
return response;
}
// if not found in cache, return default offline content (only if this is a navigation request)
if (event.request.mode === 'navigate') {
return caches.match('./index.html');
}
// fetch as normal
return fetch(event.request);
})
);
});