Skip to content

Commit 3019a92

Browse files
committed
Precache + Preload
With appcache fallback, using offline-plugin
1 parent 43a80dc commit 3019a92

File tree

6 files changed

+48
-30
lines changed

6 files changed

+48
-30
lines changed

client/containers/App/index.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
import React, { Component, PropTypes } from 'react';
22
import Toolbar from 'components/Toolbar';
3+
import preload from 'utils/preload';
34
import './style.scss';
45

5-
const tag = document.createElement('script');
6-
tag.async = 1;
7-
8-
const addScript = src => {
9-
tag.src = src;
10-
document.head.appendChild(tag.cloneNode());
11-
};
12-
136
export default class App extends Component {
147
componentDidMount() {
15-
window.__CHUNKS.forEach(addScript);
8+
window.__CHUNKS.forEach(preload);
169
}
1710

1811
render() {

client/index.ejs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,5 @@
88
<body>
99
<div id="root"></div>
1010
<script>window.__CHUNKS=[];</script>
11-
<script>
12-
if ('serviceWorker' in navigator) {
13-
navigator.serviceWorker.register('/my-service-worker.js');
14-
}
15-
</script>
1611
</body>
1712
</html>

client/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ import { render } from 'react-dom';
22
import React from 'react';
33
import { Router, browserHistory } from 'react-router/es6';
44
import rootRoute from 'pages/routes';
5+
import offline from 'offline-plugin/runtime';
56
import 'general.scss';
67

8+
offline.install();
9+
710
render(
811
<Router history={browserHistory} routes={rootRoute} />,
912
document.getElementById('root')

client/utils/preload.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Preload resources
3+
*/
4+
5+
const tag = document.createElement('link');
6+
tag.rel = 'preload';
7+
tag.as = 'script';
8+
9+
export default (src) => {
10+
tag.href = src;
11+
document.head.appendChild(tag.cloneNode());
12+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
"file-loader": "0.8.5",
2323
"html-webpack-plugin": "2.26.0",
2424
"node-sass": "3.12.2",
25+
"offline-plugin": "^4.5.5",
2526
"sass-loader": "3.2.3",
2627
"style-loader": "0.13.1",
27-
"sw-precache-webpack-plugin": "0.7.2",
2828
"webpack": "2.2.0",
2929
"webpack-dev-server": "2.2.0"
3030
},

webpack.config.js

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const webpack = require('webpack');
22
const path = require('path');
33
const HtmlWebpackPlugin = require('html-webpack-plugin');
4-
const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin');
4+
const OfflinePlugin = require('offline-plugin');
55

66
const nodeEnv = process.env.NODE_ENV || 'development';
77
const isProd = nodeEnv === 'production';
@@ -19,7 +19,6 @@ const plugins = [
1919
new webpack.optimize.CommonsChunkPlugin({
2020
name: 'vendor',
2121
minChunks: Infinity,
22-
filename: 'vendor.bundle.js'
2322
}),
2423

2524
/**
@@ -39,23 +38,39 @@ const plugins = [
3938
}),
4039

4140
/**
42-
* Precache resources using Service Workers
41+
* Precache resources using Service Workers, fallback to appcache
4342
*/
44-
new SWPrecacheWebpackPlugin({
45-
cacheId: 'react-dynamic-route-loading-es6',
46-
filename: 'my-service-worker.js',
47-
runtimeCaching: [{
48-
handler: 'cacheFirst',
49-
urlPattern: /(.*?)/
50-
}],
43+
new OfflinePlugin({
44+
relativePaths: false,
45+
publicPath: '/',
46+
updateStrategy: 'all',
47+
version: '[hash]',
48+
preferOnline: true,
49+
safeToUseOptionalCaches: true,
50+
caches: {
51+
main: ['core.*.js'],
52+
additional: ['chunk.*.js'],
53+
optional: [':rest:'],
54+
},
55+
excludes: [
56+
'*_webpack_*',
57+
],
58+
ServiceWorker: {
59+
navigateFallbackURL: '/',
60+
events: true,
61+
},
62+
AppCache: {
63+
FALLBACK: { '/': '/' },
64+
caches: ['main', 'additional']
65+
},
5166
}),
5267

5368
/**
5469
* Create a JSON file that contains file names of all chunks
5570
*/
5671
function() {
5772
const compiler = this;
58-
const chunkRegEx = /^chunk[.]/;
73+
const chunkRegEx = /^chunk[.].*js$/;
5974
compiler.plugin('compilation', function(compilation) {
6075
compilation.plugin('html-webpack-plugin-before-html-processing', function(htmlPluginData, cb) {
6176
// find all chunk file names
@@ -113,10 +128,10 @@ if (isProd) {
113128
}
114129

115130
module.exports = {
116-
devtool: isProd ? 'source-map' : 'eval',
131+
devtool: isProd ? 'source-map' : 'cheap-module-source-map',
117132
context: sourcePath,
118133
entry: {
119-
js: [
134+
core: [
120135
'index',
121136
'pages/Home'
122137
],
@@ -127,8 +142,8 @@ module.exports = {
127142
},
128143
output: {
129144
path: staticsPath,
130-
filename: 'bundle.js',
131-
chunkFilename: 'chunk.[chunkhash].js',
145+
filename: '[name].[chunkhash:8].js',
146+
chunkFilename: 'chunk.[chunkhash:8].js',
132147
publicPath: '/',
133148
},
134149
module: {

0 commit comments

Comments
 (0)