Skip to content

Commit 643361c

Browse files
nelsie borjanelsie borja
nelsie borja
authored and
nelsie borja
committed
Added offline support
1 parent 760a19f commit 643361c

File tree

8 files changed

+44
-12
lines changed

8 files changed

+44
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ tomcat.8080/
1212

1313
# Client
1414
node_modules
15+
public

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ env: {
358358

359359
[For Code Splitting](https://github.com/thejameskyle/react-loadable) or [without using a library](https://serverless-stack.com/chapters/code-splitting-in-create-react-app.html)
360360

361+
[For offline support using Service Workers](https://developers.google.com/web/tools/workbox/get-started/webpack)
362+
361363
---
362364

363365
## Dev Tools

TODO

-9
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@
1414
* https://github.com/geelen/react-snapshot
1515

1616

17-
2. Offline Support
18-
* https://github.com/NekR/offline-plugin
19-
* External/api support:
20-
https://github.com/NekR/offline-plugin/issues/64
21-
https://github.com/NekR/offline-plugin/issues/26
22-
https://github.com/NekR/offline-plugin/issues/117 (Sample implementation)
23-
* https://github.com/goldhand/sw-precache-webpack-plugin (pre-cache)
24-
25-
2617
3. Potential Libraries/References
2718
* https://github.com/jasonslyvia/react-lazyload
2819
* https://github.com/amsul/react-translated/tree/master/examples/react-dom

src/main/webapp/client/app/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import React from "react";
22
import { render } from "react-dom";
33
import Provider from "./components/Provider";
4+
import serviceWorker from "./sw";
45

56
if (process.env.APP_ENV === "development") {
67
const { whyDidYouUpdate } = require("why-did-you-update");
78
whyDidYouUpdate(React);
9+
} else {
10+
serviceWorker();
811
}
912

1013
const rootEl = document.querySelector("#root");

src/main/webapp/client/app/sw.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export default () => {
2+
if ("serviceWorker" in navigator) {
3+
window.addEventListener("load", () => {
4+
navigator.serviceWorker
5+
.register("./public/sw.js")
6+
.then(registration => {
7+
console.log("SW registered: ", registration);
8+
})
9+
.catch(registrationError => {
10+
console.log("SW registration failed: ", registrationError);
11+
});
12+
});
13+
}
14+
};

src/main/webapp/client/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"sass-resources-loader": "^1.3.1",
4343
"webpack-dev-server": "^2.9.3",
4444
"webpack-merge": "^4.1.1",
45-
"why-did-you-update": "^0.1.0"
45+
"why-did-you-update": "^0.1.0",
46+
"workbox-webpack-plugin": "^2.1.2"
4647
}
4748
}

src/main/webapp/client/webpack.common.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const CleanWebpackPlugin = require("clean-webpack-plugin");
44

55
// Directories
66
const APP_DIR = path.resolve(__dirname, "./app");
7+
const PUBLIC_DIR = path.resolve(__dirname, "../public");
78

89
module.exports = {
910
entry: {
@@ -22,7 +23,7 @@ module.exports = {
2223
publicPath: "/public/",
2324

2425
// Output folder
25-
path: path.resolve(__dirname, "../public")
26+
path: PUBLIC_DIR
2627
},
2728

2829
plugins: [

src/main/webapp/client/webpack.prod.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ const webpack = require("webpack");
44
const merge = require("webpack-merge");
55
const common = require("./webpack.common.js");
66
const ExtractTextPlugin = require("extract-text-webpack-plugin");
7+
const WorkboxPlugin = require("workbox-webpack-plugin");
78

89
// Directories
910
const APP_DIR = path.resolve(__dirname, "./app");
1011
const SCSS_RESOURCES = path.resolve(__dirname, "./app/scss/resources/*.scss");
12+
const PUBLIC_DIR = path.resolve(__dirname, "../public");
1113

1214
module.exports = merge(common, {
1315
plugins: [
@@ -35,7 +37,24 @@ module.exports = merge(common, {
3537
new webpack.optimize.AggressiveMergingPlugin(),
3638

3739
// Extract SCSS/CSS from bundle into a file; Including minification
38-
new ExtractTextPlugin("style.css")
40+
new ExtractTextPlugin("style.css"),
41+
42+
new WorkboxPlugin({
43+
globDirectory: PUBLIC_DIR,
44+
// globPatterns: ["**/*.{html,js}"], // Set specific file pattern to cache
45+
swDest: path.join(PUBLIC_DIR, "sw.js"),
46+
// Instructs the latest service worker to take control of all clients as soon as it's activated
47+
clientsClaim: true,
48+
// Instructs the latest service worker to activate as soon as it enters the waiting phase
49+
skipWaiting: true,
50+
// Usually for APIs
51+
runtimeCaching: [
52+
{
53+
urlPattern: new RegExp("https://www.carrefouruae.com"),
54+
handler: "staleWhileRevalidate"
55+
}
56+
]
57+
})
3958
],
4059

4160
module: {

0 commit comments

Comments
 (0)