mirror of
https://github.com/ritz078/transform.git
synced 2026-11-22 00:54:01 +03:00
New: Add offline support using service workers (fixes #23)
This commit is contained in:
1 parent
2eafe60832
commit
b2ce2cad21
4 files changed
+38
-2
No files matched your search
+22
-1
@@ -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
@@ -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"
|
||||
}
|
||||
}
|
||||
@@ -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}>
|
||||
|
||||
Reference in new issue
Block a user