|
| 1 | +# @vue/cli-plugin-pwa |
| 2 | + |
| 3 | +> pwa plugin for vue-cli |
| 4 | +
|
| 5 | +The service worker added with this plugin is only enabled in the production environment (e.g. only if you run `npm run build` or `yarn build`). Enabling service worker in a development mode is not a recommended practice, because it can lead to the situation when previously cached assets are used and the latest local changes are not included. |
| 6 | + |
| 7 | +Instead, in the development mode the `noopServiceWorker.js` is included. This service worker file is effectively a 'no-op' that will reset any previous service worker registered for the same host:port combination. |
| 8 | + |
| 9 | +If you need to test a service worker locally, build the application and run a simple HTTP-server from your build directory. It's recommended to use a browser incognito window to avoid complications with your browser cache. |
| 10 | + |
| 11 | +## Configuration |
| 12 | + |
| 13 | +Configuration is handled via the `pwa` property of either the `vue.config.js` |
| 14 | +file, or the `"vue"` field in `package.json`. |
| 15 | + |
| 16 | +- **pwa.workboxPluginMode** |
| 17 | + |
| 18 | + This allows you to the choose between the two modes supported by the underlying |
| 19 | + [`workbox-webpack-plugin`](https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin). |
| 20 | + |
| 21 | + - `'GenerateSW'` (default), will lead to a new service worker file being created |
| 22 | + each time you rebuild your web app. |
| 23 | + |
| 24 | + - `'InjectManifest'` allows you to start with an existing service worker file, |
| 25 | + and creates a copy of that file with a "precache manifest" injected into it. |
| 26 | + |
| 27 | + The "[Which Plugin to Use?](https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin#which_plugin_to_use)" |
| 28 | + guide can help you choose between the two modes. |
| 29 | + |
| 30 | +- **pwa.workboxOptions** |
| 31 | + |
| 32 | + These options are passed on through to the underlying `workbox-webpack-plugin`. |
| 33 | + |
| 34 | + For more information on what values are supported, please see the guide for |
| 35 | + [`GenerateSW`](https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin#full_generatesw_config) |
| 36 | + or for [`InjectManifest`](https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin#full_injectmanifest_config). |
| 37 | + |
| 38 | +- **pwa.name** |
| 39 | + |
| 40 | + - Default: "name" field in `package.json` |
| 41 | + |
| 42 | + Used as the value for the `apple-mobile-web-app-title` meta tag in the generated HTML. Note you will need to edit `public/manifest.json` to match this. |
| 43 | + |
| 44 | +- **pwa.themeColor** |
| 45 | + |
| 46 | + - Default: `'#4DBA87'` |
| 47 | + |
| 48 | +- **pwa.msTileColor** |
| 49 | + |
| 50 | + - Default: `'#000000'` |
| 51 | + |
| 52 | +- **pwa.appleMobileWebAppCapable** |
| 53 | + |
| 54 | + - Default: `'no'` |
| 55 | + |
| 56 | + This defaults to `'no'` because iOS before 11.3 does not have proper PWA support. See [this article](https://medium.com/@firt/dont-use-ios-web-app-meta-tag-irresponsibly-in-your-progressive-web-apps-85d70f4438cb) for more details. |
| 57 | + |
| 58 | +- **pwa.appleMobileWebAppStatusBarStyle** |
| 59 | + |
| 60 | + - Default: `'default'` |
| 61 | + |
| 62 | +- **pwa.assetsVersion** |
| 63 | + |
| 64 | + - Default: `''` |
| 65 | + |
| 66 | + This option is used if you need to add a version to your icons and manifest, against browser’s cache. This will append `?v=<pwa.assetsVersion>` to the URLs of the icons and manifest. |
| 67 | + |
| 68 | +- **pwa.manifestPath** |
| 69 | + |
| 70 | + - Default: `'manifest.json'` |
| 71 | + |
| 72 | + The path of app’s manifest. |
| 73 | + |
| 74 | +- **pwa.manifestOptions** |
| 75 | + |
| 76 | + - Default: `{}` |
| 77 | + |
| 78 | + The object will be used to generate the `manifest.json` |
| 79 | + |
| 80 | + If the following attributes are not defined in the object, the options of `pwa` or default options will be used instead. |
| 81 | + - name: `pwa.name` |
| 82 | + - short_name: `pwa.name` |
| 83 | + - start_url: `'.'` |
| 84 | + - display: `'standalone'` |
| 85 | + - theme_color: `pwa.themeColor` |
| 86 | + |
| 87 | +- **pwa.iconPaths** |
| 88 | + |
| 89 | + - Defaults: |
| 90 | + |
| 91 | + ```js |
| 92 | + { |
| 93 | + favicon32: 'img/icons/favicon-32x32.png', |
| 94 | + favicon16: 'img/icons/favicon-16x16.png', |
| 95 | + appleTouchIcon: 'img/icons/apple-touch-icon-152x152.png', |
| 96 | + maskIcon: 'img/icons/safari-pinned-tab.svg', |
| 97 | + msTileImage: 'img/icons/msapplication-icon-144x144.png' |
| 98 | + } |
| 99 | + ``` |
| 100 | + |
| 101 | + Change these values to use different paths for your icons. |
| 102 | + |
| 103 | +### Example Configuration |
| 104 | + |
| 105 | +```js |
| 106 | +// Inside vue.config.js |
| 107 | +module.exports = { |
| 108 | + // ...other vue-cli plugin options... |
| 109 | + pwa: { |
| 110 | + name: 'My App', |
| 111 | + themeColor: '#4DBA87', |
| 112 | + msTileColor: '#000000', |
| 113 | + appleMobileWebAppCapable: 'yes', |
| 114 | + appleMobileWebAppStatusBarStyle: 'black', |
| 115 | +
|
| 116 | + // configure the workbox plugin |
| 117 | + workboxPluginMode: 'InjectManifest', |
| 118 | + workboxOptions: { |
| 119 | + // swSrc is required in InjectManifest mode. |
| 120 | + swSrc: 'dev/sw.js', |
| 121 | + // ...other Workbox options... |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +## Installing in an Already Created Project |
| 128 | + |
| 129 | +``` sh |
| 130 | +vue add @vue/pwa |
| 131 | +``` |
| 132 | + |
| 133 | +## Injected webpack-chain Rules |
| 134 | + |
| 135 | +- `config.plugin('workbox')` |
0 commit comments