Commit 81fc8086 authored by mdsecurity's avatar mdsecurity
Browse files

WE HAVE A SERVICE WORKER NOW

it doesnt do much yet but be patient ;)
parent 4af64328
......@@ -21,7 +21,9 @@
"styles": [
"styles.scss"
],
"scripts": [],
"scripts": [
"sw-loader.js"
],
"environments": {
"source": "environments/environment.ts",
"dev": "environments/environment.ts",
......
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
// import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
......@@ -32,7 +32,7 @@ import { ShowHideArrowComponent } from './context-bar/show-hide-arrow/show-hide-
],
imports: [
BrowserModule,
FormsModule,
// FormsModule,
HttpModule
],
providers: [],
......
......@@ -53,6 +53,6 @@
<!--<hr>-->
<app-time-range [place]="place"></app-time-range>
<!--<hr>-->
<app-map></app-map>
<!--<app-map></app-map>-->
</div>
</div>
import { Component, OnInit, Input } from '@angular/core';
import { Component, OnInit} from '@angular/core';
import { DataProviderService } from '../../data-provider.service';
import { Place } from '../../Place';
@Component({
......
import { Component, OnInit, Input } from '@angular/core';
import { Component, OnInit} from '@angular/core';
import { Place } from '../place';
@Component({
selector: 'app-place-window',
......
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('/sw.js').then(function(registration) {
// Registration was successful
console.log('ServiceWorker registration successful with scope: ', registration.scope);
}).catch(function(err) {
// registration failed :(
console.log('ServiceWorker registration failed: ', err);
});
});
}
\ No newline at end of file
var CACHE_NAME = 'my-site-cache-v1';
var urlsToCache = [
'/',
'/assets/Bking.jpg'
];
self.addEventListener('install', function(event) {
// Perform install steps
event.waitUntil(
caches.open(CACHE_NAME)
.then(function(cache) {
console.log('Opened cache');
return cache.addAll(urlsToCache);
})
);
});
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request)
.then(function(response) {
// Cache hit - return response
if (response) {
return response;
}
// IMPORTANT: Clone the request. A request is a stream and
// can only be consumed once. Since we are consuming this
// once by cache and once by the browser for fetch, we need
// to clone the response.
var fetchRequest = event.request.clone();
return fetch(fetchRequest).then(
function(response) {
// Check if we received a valid response
if(!response || response.status !== 200 || response.type !== 'basic') {
return response;
}
// IMPORTANT: Clone the response. A response is a stream
// and because we want the browser to consume the response
// as well as the cache consuming the response, we need
// to clone it so we have two streams.
var responseToCache = response.clone();
caches.open(CACHE_NAME)
.then(function(cache) {
cache.put(event.request, responseToCache);
});
return response;
}
);
})
);
});
// this probably wont be used but this whitelists caches so if i want to use multiple they are not destroyed
// when a new service worker takes control
self.addEventListener('activate', function(event) {
var cacheWhitelist = ['pages-cache-v1'];
event.waitUntil(
caches.keys().then(function(cacheNames) {
return Promise.all(
cacheNames.map(function(cacheName) {
if (cacheWhitelist.indexOf(cacheName) === -1) {
return caches.delete(cacheName);
}
})
);
})
);
});
{
"compilerOptions": {
"baseUrl": "",
"declaration": false,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": ["es2015", "dom"],
"mapRoot": "./",
"module": "es2015",
"moduleResolution": "node",
"outDir": "../dist/out-tsc",
"sourceMap": true,
"target": "es5",
"typeRoots": [
"../node_modules/@types"
],
"suppressImplicitAnyIndexErrors": true
},
"files": [
"src/app/app.module.ts",
"src/main.ts"
],
"angularCompilerOptions": {
"genDir": "aot",
"skipMetadataEmit": true
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment