New: Add offline support using service workers (fixes #23)

This commit is contained in:
Gyandeep Singh committed 2017-09-03 20:53:58 -05:00
1 parent 2eafe60832
commit b2ce2cad21
4 files changed
+38 -2

No files matched your search

+1
View File
@@ -0,0 +1 @@
package-lock=false
+22 -1
View File
@@ -3,6 +3,7 @@ const webpack = require("webpack")
const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
const { ANALYZE } = process.env
const glob = require("glob")
const SWPrecacheWebpackPlugin = require("sw-precache-webpack-plugin")
module.exports = {
webpack: (config, { dev }) => {
@@ -27,6 +28,26 @@ module.exports = {
config.plugins.push(
new BabiliPlugin()
)
config.plugins.push(
new SWPrecacheWebpackPlugin({
minify: true,
staticFileGlobsIgnorePatterns: [/\.next\//],
staticFileGlobs: [
'static/**/*' // Precache all static files by default
],
runtimeCaching: [
{
handler: 'fastest',
urlPattern: /[.](png|jpg|css)/
},
{
handler: "networkFirst",
urlPattern: /^https?.*/
}
]
})
)
}
if (ANALYZE) {
@@ -49,4 +70,4 @@ module.exports = {
})
return pathMap
}
};
};
+2 -1
View File
@@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"start": "next start",
"build": "rm -rf out && next build && next export",
"build": "rm -rf out && next build && next export && cp ./.next/service-worker.js ./out/service-worker.js",
"dev": "next dev",
"addContributor": "all-contributors add",
"analyze": "cross-env ANALYZE=1 next build",
@@ -51,6 +51,7 @@
"glob": "^7.1.2",
"prettier": "^1.6.1",
"style-loader": "^0.18.2",
"sw-precache-webpack-plugin": "^0.11.4",
"webpack-bundle-analyzer": "^2.9.0"
}
}
+13
View File
@@ -30,6 +30,19 @@ export default class Main extends PureComponent {
${code}`;
};
async componentDidMount() {
if ("serviceWorker" in navigator) {
navigator.serviceWorker
.register("/service-worker.js")
.then(registration => {
console.log("service worker registration successful");
})
.catch(err => {
console.warn("service worker registration failed");
});
}
}
render() {
return (
<Layout pathname={this.props.url.pathname}>