Skip to content

Instantly share code, notes, and snippets.

@anurag-majumdar
Created September 2, 2018 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anurag-majumdar/2a46725d2decf8059e6a86b1e5226c0f to your computer and use it in GitHub Desktop.
Save anurag-majumdar/2a46725d2decf8059e6a86b1e5226c0f to your computer and use it in GitHub Desktop.
(function () {
class App {
constructor() {
this.registerServiceWorker();
this._app = document.querySelector('.app');
this.fetchOfflineContent = this.fetchOfflineContent.bind(this);
this.fetchAppContent = this.fetchAppContent.bind(this);
self.addEventListener('online', this.fetchAppContent);
self.addEventListener('offline', this.fetchOfflineContent);
this.checkNetworkState();
}
checkNetworkState() {
this._offlineEvent = new Event('offline');
if (!navigator.onLine) {
self.dispatchEvent(this._offlineEvent);
}
}
fetchOfflineContent() {
if (!navigator.onLine) {
const offlineUrl = self.location.origin + '/public/offline.html';
caches.match(offlineUrl)
.then(response => {
return response.text();
})
.then(offlineHtml => {
while (this._app.firstChild) {
this._app.removeChild(this._app.firstChild);
}
this._app.insertAdjacentHTML('afterbegin', offlineHtml);
})
.catch(error => {
console.log(`Could not fetch offline content due to - ${error}`);
});
}
}
fetchAppContent() {
while (this._app.firstChild) {
this._app.removeChild(this._app.firstChild);
}
this._app.textContent = 'Main Application';
}
registerServiceWorker() {
const serviceWorker = navigator.serviceWorker;
if (!serviceWorker) {
return;
}
serviceWorker.register('./sw.js')
.then((registration) => {
console.log('Service worker registered successfully!');
}).catch((error) => {
console.log('Some error occurred while registering Service Worker');
});
}
}
new App();
})();(function () {
class App {
constructor() {
this.registerServiceWorker();
this._app = document.querySelector('.app');
this.fetchOfflineContent = this.fetchOfflineContent.bind(this);
this.fetchAppContent = this.fetchAppContent.bind(this);
self.addEventListener('online', this.fetchAppContent);
self.addEventListener('offline', this.fetchOfflineContent);
this.checkNetworkState();
}
checkNetworkState() {
this._offlineEvent = new Event('offline');
if (!navigator.onLine) {
self.dispatchEvent(this._offlineEvent);
}
}
fetchOfflineContent() {
if (!navigator.onLine) {
const offlineUrl = self.location.origin + '/public/offline.html';
caches.match(offlineUrl)
.then(response => {
return response.text();
})
.then(offlineHtml => {
while (this._app.firstChild) {
this._app.removeChild(this._app.firstChild);
}
this._app.insertAdjacentHTML('afterbegin', offlineHtml);
})
.catch(error => {
console.log(`Could not fetch offline content due to - ${error}`);
});
}
}
fetchAppContent() {
while (this._app.firstChild) {
this._app.removeChild(this._app.firstChild);
}
this._app.textContent = 'Main Application';
}
registerServiceWorker() {
const serviceWorker = navigator.serviceWorker;
if (!serviceWorker) {
return;
}
serviceWorker.register('./sw.js')
.then((registration) => {
console.log('Service worker registered successfully!');
}).catch((error) => {
console.log('Some error occurred while registering Service Worker');
});
}
}
new App();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment