Skip to content

Commit

Permalink
feat/add-workbox
Browse files Browse the repository at this point in the history
  • Loading branch information
thepassle committed Sep 23, 2019
1 parent c2e61bf commit a0571b0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 40 deletions.
39 changes: 11 additions & 28 deletions config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const ManifestPlugin = require('webpack-manifest-plugin');
const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
const SWPrecacheWebpackPlugin = require('sw-precache-webpack-plugin');
const CopyPlugin = require("copy-webpack-plugin");
const workboxPlugin = require("workbox-webpack-plugin");
const paths = require('../config/paths');
const getClientEnvironment = require('./env');

Expand Down Expand Up @@ -322,35 +323,17 @@ module.exports = {
fileName: 'asset-manifest.json',
publicPath: publicPath
}),
// Copies the public folder to the build folder
new CopyPlugin([
{ from: './public/', to: './' },
]),
// Generate a service worker script that will precache, and keep up to date,
// the HTML & assets that are part of the Webpack build.
new SWPrecacheWebpackPlugin({
// By default, a cache-busting query parameter is appended to requests
// used to populate the caches, to ensure the responses are fresh.
// If a URL is already hashed by Webpack, then there is no concern
// about it being stale, and the cache-busting can be skipped.
dontCacheBustUrlsMatching: /\.\w{8}\./,
filename: 'service-worker.js',
logger(message) {
if (message.indexOf('Total precache size is') === 0) {
// This message occurs for every build and is a bit too noisy.
return;
}
if (message.indexOf('Skipping static resource') === 0) {
// This message obscures real errors so we ignore it.
// https://github.com/facebook/create-react-app/issues/2612
return;
}
console.log(message);
},
minify: true,
// Don't precache sourcemaps (they're large) and build asset manifest:
staticFileGlobsIgnorePatterns: [/\.map$/, /asset-manifest\.json$/]
// `navigateFallback` and `navigateFallbackWhitelist` are disabled by default; see
// https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#service-worker-considerations
// navigateFallback: publicUrl + '/index.html',
// navigateFallbackWhitelist: [/^(?!\/__).*/],
})
new workboxPlugin.GenerateSW({
swDest: "./service-worker.js",
skipWaiting: true,
clientsClaim: true
}),
],
// Some libraries import Node modules but don't use them in the browser.
// Tell Webpack to provide empty mocks for them so importing them works.
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"chalk": "^2.3.1",
"cli-table": "0.3.1",
"connect-history-api-fallback": "^1.5.0",
"copy-webpack-plugin": "^5.0.4",
"cosmiconfig": "^5.0.6",
"cross-spawn": "^6.0.5",
"css-loader": "^0.28.9",
Expand Down Expand Up @@ -58,7 +59,8 @@
"webpack": "^4.2.0",
"webpack-dev-server": "^3.1.1",
"webpack-manifest-plugin": "^2.0.3",
"whatwg-fetch": "2.0.4"
"whatwg-fetch": "2.0.4",
"workbox-webpack-plugin": "^4.3.1"
},
"devDependencies": {
"@commitlint/cli": "^8.1.0",
Expand Down
9 changes: 0 additions & 9 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ measureFileSizesBeforeBuild(paths.appBuild)
// Remove all content but keep the directory so that
// if you're in it, you don't end up in Trash
fs.emptyDirSync(paths.appBuild);
// Merge with the public folder
copyPublicFolder();
// Start the webpack build
return build(previousFileSizes);
})
Expand Down Expand Up @@ -138,10 +136,3 @@ function build(previousFileSizes) {
});
});
}

function copyPublicFolder() {
fs.copySync(paths.appPublic, paths.appBuild, {
dereference: true,
filter: file => file !== paths.appHtml
});
}
4 changes: 2 additions & 2 deletions template/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ Progressive Web Apps are faster and more reliable than traditional web pages, an
* Your app will work regardless of network state, even if offline. This means your users will be able to use your app at 10,000 feet and on the Subway.
* On mobile devices, your app can be added directly to the user's home screen, app icon and all. You can also re-engage users using web **push notifications**. This eliminates the need for the app store.
The [`sw-precache-webpack-plugin`](https://github.com/goldhand/sw-precache-webpack-plugin)
The [`workbox-webpack-plygin`](https://www.npmjs.com/package/workbox-webpack-plugin)
is integrated into production configuration,
and it will take care of generating a service worker file that will automatically
precache all of your local assets and keep them up to date as you deploy updates.
Expand Down Expand Up @@ -753,7 +753,7 @@ it may take up to 24 hours for the cache to be invalidated.

1. Users aren't always familiar with offline-first web apps. It can be useful to [let the user know](https://developers.google.com/web/fundamentals/instant-and-offline/offline-ux#inform_the_user_when_the_app_is_ready_for_offline_consumption) when the service worker has finished populating your caches (showing a "This web app works offline!" message) and also let them know when the service worker has fetched the latest updates that will be available the next time they load the page (showing a "New content is available; please refresh." message). Showing this messages is currently left as an exercise to the developer, but as a starting point, you can make use of the logic included in [`src/serviceWorker.js`](src/serviceWorker.js), which demonstrates which service worker lifecycle events to listen for to detect each scenario, and which as a default, just logs appropriate messages to the JavaScript console.

1. By default, the generated service worker file will not intercept or cache any cross-origin traffic, like HTTP [API requests](#integrating-with-an-api-backend), images, or embeds loaded from a different domain. If you would like to use a runtime caching strategy for those requests, you can [`eject`](#npm-run-eject) and then configure the [`runtimeCaching`](https://github.com/GoogleChrome/sw-precache#runtimecaching-arrayobject) option in the `SWPrecacheWebpackPlugin` section of [`webpack.config.prod.js`](../config/webpack.config.prod.js).
1. By default, the generated service worker file will not intercept or cache any cross-origin traffic, like HTTP [API requests](#integrating-with-an-api-backend), images, or embeds loaded from a different domain. If you would like to use a runtime caching strategy for those requests, you can [`eject`](#npm-run-eject) and then configure the [`runtimeCaching`](https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin) option in the `WorkboxPlugin` section of [`webpack.config.prod.js`](../config/webpack.config.prod.js).

### Progressive Web App Metadata

Expand Down

0 comments on commit a0571b0

Please sign in to comment.