Skip to content

Commit

Permalink
feat(service-worker): add sw-precache for offline
Browse files Browse the repository at this point in the history
  • Loading branch information
r-park committed Jul 9, 2017
1 parent 2e0429e commit 7e1fc78
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 21 deletions.
146 changes: 127 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 35 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@
"node": ">=8.1",
"npm": ">= 5"
},
"browserslist": [
"last 2 versions",
"not ie <= 10",
"not ie_mob <= 10"
],
"scripts": {
"build": "ng build --prod",
"e2e": "ng e2e",
"lint": "ng lint",
"ng": "ng",
"prebuild": "ng lint",
"postbuild": "npm run precache",
"precache": "sw-precache --config=sw-precache.config.js --verbose",
"start": "ng serve",
"test": "ng test"
},
Expand Down Expand Up @@ -57,8 +63,35 @@
"karma-jasmine-html-reporter": "^0.2.2",
"minx": "r-park/minx.git",
"protractor": "^5.1.2",
"sw-precache": "^5.2.0",
"ts-node": "^3.2.0",
"tslint": "^5.5.0",
"typescript": "^2.4.1"
}
},
"author": {
"name": "Richard Park",
"email": "[email protected]"
},
"contributors": [
{
"name": "Cody Lundquist",
"email": "[email protected]"
},
{
"name": "Jason Shultz",
"email": ""
},
{
"name": "Luke Schlangen",
"email": "[email protected]"
},
{
"name": "Sinan Bolel",
"email": "[email protected]"
},
{
"name": "Yannick Koehler",
"email": "[email protected]"
}
]
}
7 changes: 7 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ if (environment.production) {


platformBrowserDynamic().bootstrapModule(AppModule)
.then(() => {
if (environment.production && 'serviceWorker' in navigator) {
navigator.serviceWorker
.register('/service-worker.js')
.then(registration => console.log('[Service Worker] registered with scope', registration.scope));
}
})
.catch(error => console.error(error));
31 changes: 31 additions & 0 deletions sw-precache.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports = {
navigateFallback: '/index.html',

// @see https://github.com/Polymer/polymer-build/issues/35
navigateFallbackWhitelist: [
/^(?!\/__)/
],

root: 'dist',
runtimeCaching: [
{
urlPattern: /^https:\/\/fonts\.googleapis\.com\//,
handler: 'cacheFirst'
},
{
urlPattern: /^https:\/\/maxcdn\.bootstrapcdn\.com\//,
handler: 'cacheFirst'
},
{
urlPattern: /^https:\/\/use\.typekit\.net\//,
handler: 'cacheFirst'
}
],
staticFileGlobs: [
'dist/assets/**/*',
'dist/*.css',
'dist/*.html',
'dist/*.js'
],
stripPrefix: 'dist/'
};

0 comments on commit 7e1fc78

Please sign in to comment.