{"_id":"@elken/next-offline","_rev":"7-980a45bf972e2a587465742828cc16d8","time":{"3.3.7":"2019-04-02T13:51:22.455Z","3.3.8":"2019-04-02T13:51:22.455Z","created":"2019-04-04T08:18:27.486Z","3.4.0":"2019-04-04T08:18:27.852Z","modified":"2022-04-05T07:12:35.571Z"},"name":"@elken/next-offline","dist-tags":{"latest":"3.4.0"},"versions":{"3.4.0":{"name":"@elken/next-offline","version":"3.4.0","main":"index.js","license":"MIT","repository":{"type":"git","url":"git+https://github.com/elken/next-offline.git"},"scripts":{"test":"cd tests && yarn test"},"bugs":{"url":"https://github.com/elken/next-offline/issues"},"authors":["Jack Hanford"],"contributors":[{"name":"Joao Vieira","email":"joaoguerravieira@gmail.com"}],"tags":["react","next.js","offline","service worker","pre-cache","networkFirst","cacheFirst","webpack","workbox","sw-helpers"],"keywords":["react","next.js","offline","service worker","pre-cache","networkFirst","cacheFirst","webpack","workbox","sw-helpers"],"peerDependencies":{"next":">=7.0.0"},"dependencies":{"clean-webpack-plugin":"^0.1.19","copy-webpack-plugin":"~4.5.2","fs-extra":"~7.0.0","glob":"~7.1.3","webpack":"^4.19.1","workbox-webpack-plugin":"3.6.3"},"devDependencies":{"husky":"^0.14.3","prettier":"^1.14.3","pretty-quick":"^1.7.0"},"husky":{"hooks":{"pre-commit":"pretty-quick --staged"}},"gitHead":"30cfcef6d462295793ad1e2362bc5d758f774db5","description":"<h1 align=\"center\">\r   next-offline\r </h1>","homepage":"https://github.com/elken/next-offline#readme","_id":"@elken/next-offline@3.4.0","_nodeVersion":"10.11.0","_npmVersion":"6.8.0","dist":{"integrity":"sha512-V1SkdH8Fk03Q/dRfNQT5XbcUg8SEnaV5C71t7eqqvJkzwlBcPnvH8fRJ3keh7t8Th4+6tWfyFVP7THr3rynuVA==","shasum":"efb6e44c6b298c9f96bb097346dc9f6157f72c9f","tarball":"https://registry.npmjs.org/@elken/next-offline/-/next-offline-3.4.0.tgz","fileCount":35,"unpackedSize":84492,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJcpb3UCRA9TVsSAnZWagAAFp0QAIHQrLLOS54feUdPXoVD\nwNEpUgGBmOctKg3pd/twhaFYx8VhJQrawP9xstRMt4IK5XIYpQI11ofp7IEv\njyHdB/4tM4xPVUpHr2CU3e9coGr+TqYcp2YTDM98kejU+AbrZ4BPyE3XHOWu\nZbi6gfdA4B3Pgugs4sa597P8gaONV+AXxhHubBIx6IjMqEWAI9htYwUWxgMV\n+OLq2YwepqQ4qPTy1SMROhW7AiVMVVdGtoEQnmH/JtZJzP+nrRFs0AIqItXj\nvxnUvOxIWfq0kfJX4BwfSPckNahNAJd0D9mrktUTGedhW/X2DnqXbdc/aeR6\nv74D9r6vHrmOefhvRHpK63/+x+U4Unz0v6VyHZma7EulTxK1x9olGVRUuYpg\ngRBuK9xcCTWtJerx2Hne40ZDtKyHT8wuD53+MqaXszih5MyK4DAopQSEeRPQ\nCT7Nm3I4Parkh8v2r2aae0vjcd0UbgeyfyWVM29yxtaUXY3eIvxqLS0GT2zg\nuNGJ26oFCrPOJO8UD2xmS2ZNlgjdcTFeU/hgc1ZJW3iYzxuUwYk8SbXARMgW\nXG5hkOr7w/2IXfqbjq8Q5fX6JUzrEzA1SB0iK4GbvIBPcfzoNq2f9cmHw5e3\niVmi4TIKREhXLyZGVpFmC0xgY++PZ1s3wKNQ5enbqMjRwpgDmVTofGiKHsOW\nzIZK\r\n=BBy+\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIGwI5jqRZSzBhoYwu4Qc5liqoij34aimW60aJ0h9cN0oAiEA8WB7obGwlnHBF65l+fuepasth3teLFjh7UHx3S5KJLE="}]},"maintainers":[{"name":"elken","email":"elken@mykolab.com"}],"_npmUser":{"name":"elken","email":"elken@mykolab.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/next-offline_3.4.0_1554365907666_0.31721178949877293"},"_hasShrinkwrap":false}},"maintainers":[{"name":"elken","email":"elken@mykolab.com"}],"description":"<h1 align=\"center\">\r   next-offline\r </h1>","homepage":"https://github.com/elken/next-offline#readme","keywords":["react","next.js","offline","service worker","pre-cache","networkFirst","cacheFirst","webpack","workbox","sw-helpers"],"repository":{"type":"git","url":"git+https://github.com/elken/next-offline.git"},"contributors":[{"name":"Joao Vieira","email":"joaoguerravieira@gmail.com"}],"bugs":{"url":"https://github.com/elken/next-offline/issues"},"license":"MIT","readme":"<h1 align=\"center\">\r\n  next-offline\r\n</h1>\r\n\r\n<p align=\"center\">\r\nUse <a href='https://github.com/GoogleChrome/workbox'>Workbox</a> with <a href='https://github.com/zeit/next.js'>\r\nNext.js</a> and <br />easily enable offline functionality in your application!\r\n</p>\r\n\r\n<p align=\"center\">\r\n  <a href=\"https://www.npmjs.com/package/next-offline\">\r\n    <img src=\"https://img.shields.io/npm/dy/next-offline.svg\">\r\n  </a>\r\n  <a href=\"https://www.npmjs.com/package/next-offline\">\r\n    <img src=\"https://img.shields.io/npm/v/next-offline.svg?maxAge=3600&label=next-offline&colorB=007ec6\">\r\n  </a>\r\n  <img src=\"https://img.shields.io/github/repo-size/hanford/next-offline.svg\" />\r\n</p>\r\n\r\n<br/>\r\n\r\n## Installation\r\n\r\n```sh\r\n$ npm install --save next-offline\r\n```\r\n\r\n```sh\r\n$ yarn add next-offline\r\n```\r\n\r\n## Usage\r\n\r\nCreate a `next.config.js` in your project\r\n\r\n```js\r\n// next.config.js\r\nconst withOffline = require('next-offline')\r\n\r\nconst nextConfig = {\r\n  ...\r\n}\r\n\r\nmodule.exports = withOffline(nextConfig)\r\n```\r\n\r\n## Documentation\r\n- [Installation](#installation)\r\n- [Usage](#usage)\r\n- [Documentation](#documentation)\r\n- [Serving service worker](#serving-service-worker)\r\n  - [Now 1.0](#now-10)\r\n  - [Now 2.0](#now-20)\r\n- [Registering service worker](#registering-service-worker)\r\n  - [Compile-time registration](#compile-time-registration)\r\n  - [Runtime registration](#runtime-registration)\r\n- [Customizing service worker](#customizing-service-worker)\r\n  - [Using workbox](#using-workbox)\r\n  - [next-offline options](#next-offline-options)\r\n  - [further customisations](#further-customisations)\r\n- [Cache strategies](#cache-strategies)\r\n- [Service worker path](#service-worker-path)\r\n- [Development mode](#development-mode)\r\n- [next export](#next-export)\r\n- [License (MIT)](#license-mit)\r\n\r\n## Serving service worker\r\nBecause service workers are so powerful, the API has some restrictions built in. For example, service workers must be served on the domain they're being used on - [you can't use a CDN](https://github.com/w3c/ServiceWorker/issues/940).\r\n\r\n### Now 1.0\r\nYou'll want to use the next.js custom server API. The easiest way to do that is creating a `server.js` that looks like this:\r\n```js\r\nconst { createServer } = require('http')\r\nconst { join } = require('path')\r\nconst { parse } = require('url')\r\nconst next = require('next')\r\n\r\nconst app = next({ dev: process.env.NODE_ENV !== 'production' })\r\nconst handle = app.getRequestHandler()\r\n\r\napp.prepare()\r\n  .then(() => {\r\n    createServer((req, res) => {\r\n      const parsedUrl = parse(req.url, true)\r\n      const { pathname } = parsedUrl\r\n\r\n      // handle GET request to /service-worker.js\r\n      if (pathname === '/service-worker.js') {\r\n        const filePath = join(__dirname, '.next', pathname)\r\n\r\n        app.serveStatic(req, res, filePath)\r\n      } else {\r\n        handle(req, res, parsedUrl)\r\n      }\r\n    })\r\n    .listen(3000, () => {\r\n      console.log(`> Ready on http://localhost:${3000}`)\r\n    })\r\n  })\r\n```\r\nYou can  read more about custom servers in the [Next.js docs](https://github.com/zeit/next.js#custom-server-and-routing)\r\n\r\nIf you're not hosting with Now, I'd probably follow the Now 1.0 approach because the custom server API can enable a lot of functionality, it just simply doesn't work well with Now 2.0 🙇‍♂️\r\n\r\n### Now 2.0\r\nBecause Now 2.0 works so different than the previous version, so does serving the service worker. There are a few different ways to do this, but I'd recommend checking out [this now2 example app](https://github.com/hanford/next-offline/tree/master/examples/now2). The changes to be aware of are in the [now.json](https://github.com/hanford/next-offline/blob/master/examples/now2/now.json) and [next.config.js](https://github.com/hanford/next-offline/blob/master/examples/now2/next.config.js).\r\n\r\n## Registering service worker\r\n### Compile-time registration\r\nBy default `next-offline` will register a service worker with the script below, this is automatically added to your client side bundle once `withOffline` is invoked.\r\n\r\n```js\r\nif ('serviceWorker' in navigator) {\r\n  window.addEventListener('load', function () {\r\n    navigator.serviceWorker.register('/service-worker.js', { scope: '/' }).then(function (registration) {\r\n      console.log('SW registered: ', registration)\r\n    }).catch(function (registrationError) {\r\n      console.log('SW registration failed: ', registrationError)\r\n    })\r\n  })\r\n}\r\n```\r\n\r\n### Runtime registration\r\nAlternative to compile-time, you can take control of registering/unregistering in your application code by using the `next-offline/runtime` module.\r\n\r\n```js\r\nimport { register, unregister } from 'next-offline/runtime'\r\n\r\nclass App extends React.Component {\r\n  componentDidMount () {\r\n    register()\r\n  }\r\n  componentWillUnmount () {\r\n    unregister()\r\n  }\r\n  ..\r\n}\r\n```\r\n\r\nIf you're handling registration on your own, pass `dontAutoRegisterSw` to next-offline.\r\n```js\r\n// next.config.js\r\nconst withOffline = require('next-offline')\r\n\r\nmodule.exports = withOffline({ dontAutoRegisterSw: true })\r\n```\r\n\r\n## Customizing service worker\r\n\r\n### Using workbox\r\n\r\nDefine a `workboxOpts` object in your `next.config.js` and it will gets passed to [workbox-webpack-plugin](https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin#generatesw_plugin). Workbox is what `next-offline` uses under the hood to generate the service worker, you can learn more about it [here](https://developers.google.com/web/tools/workbox/).\r\n\r\n```js\r\n// next.config.js\r\nconst withOffline = require('next-offline')\r\n\r\nconst nextConfig = {\r\n  workboxOpts: {\r\n    ...\r\n  }\r\n}\r\n\r\nmodule.exports = withOffline(nextConfig)\r\n```\r\n\r\n### next-offline options\r\nOn top of the workbox options, next-offline has some options built in flags to give you finer grain control over how your service worker gets generated.\r\n\r\n<table>\r\n  <thead>\r\n    <tr>\r\n      <th>Name</th>\r\n      <th>Type</th>\r\n      <th>Description</th>\r\n      <th>Default</th>\r\n    </tr>\r\n  </thead>\r\n  <tbody>\r\n    <tr>\r\n      <td>generateSw</td>\r\n      <td>Boolean</td>\r\n      <td>If false, next-offline will not generate a service worker and will instead try to modify workboxOpts.swSrc</td>\r\n      <td>true</td>\r\n    </tr>\r\n    <tr>\r\n      <td>dontAutoRegisterSw</td>\r\n      <td>Boolean</td>\r\n      <td>If true, next-offline won't automatically push the registration script into the application code. This is required if you're using runtime registration or are handling registration on your own.</td>\r\n      <td>false</td>\r\n    </tr>\r\n    <tr>\r\n      <td>devSwSrc</td>\r\n      <td>String</td>\r\n      <td>Path to be registered by next-offline during development. By default next-offline will register a noop during development</td>\r\n      <td>false</td>\r\n    </tr>\r\n    <tr>\r\n      <td>generateInDevMode</td>\r\n      <td>Boolean</td>\r\n      <td>If true, the service worker will also be generated in development mode. Otherwise the service worker defined in devSwSrc will be used.</td>\r\n      <td>false</td>\r\n    </tr>\r\n    <tr>\r\n      <td>registerSwPrefix</td>\r\n      <td>String</td>\r\n      <td>If your service worker isn't at the root level of your application, this can help you prefix the path. This is useful if you'd like your service worker on foobar.com/my/long/path/service-worker.js</td>\r\n      <td>false</td>\r\n    </tr>\r\n    <tr>\r\n      <td>scope</td>\r\n      <td>String</td>\r\n      <td>This is passed to the automatically registered service worker allowing increase or decrease what the service worker has control of.</td>\r\n      <td>\"/\"</td>\r\n    </tr>\r\n  </tbody>\r\n</table>\r\n\r\n### further customisations\r\n\r\nIt is also possible to append your own code to the end of the serviceWorker, as shown below.\r\n\r\n```js\r\nextraCodePath: \"static/serviceWorker.js\",\r\nregisterOverridePath: \"static/init-sw.js\"\r\n```\r\n\r\n`extraCodePath` will append the content of the file to the end of the created serviceWorker, whereas `registerOverridePath` will override the existing default register, so you will need to ensure that it is complete.\r\n\r\n## Cache strategies\r\nBy default `next-offline` has the following blanket runtime caching strategy. If you customize `next-offline` with `workboxOpts`, the default behaviour will not be passed into `workbox-webpack-plugin`\r\n```js\r\n{\r\n  globPatterns: ['static/**/*'],\r\n  globDirectory: '.',\r\n  runtimeCaching: { urlPattern: /^https?.*/, handler: 'networkFirst' }\r\n}\r\n```\r\n\r\n\r\n```js\r\n// next.config.js\r\nconst withOffline = require('next-offline')\r\n\r\nmodule.exports = withOffline({\r\n  workboxOpts: {\r\n    runtimeCaching: [\r\n      {\r\n        urlPattern: /.png$/,\r\n        handler: 'cacheFirst'\r\n      },\r\n      {\r\n        urlPattern: /api/,\r\n        handler: 'networkFirst',\r\n        options: {\r\n          cacheableResponse: {\r\n            statuses: [0, 200],\r\n            headers: {\r\n              'x-test': 'true'\r\n            }\r\n          }\r\n        }\r\n      }\r\n    ]\r\n  }\r\n})\r\n```\r\n\r\n## Service worker path\r\nIf your application doesn't live on the root of your domain, you can use `registerSwPrefix`. This is helpful if your application is on domain.com/my/custom/path because by default `next-offline` assumes your application is on domain.com and will try to register domain.com/service-worker.js. We can't support using `assetPrefix` because service workers must be served on the root domain. For a technical breakdown on that limitation, see the following link: [Is it possible to serve service workers from CDN/remote origin?](https://github.com/w3c/ServiceWorker/issues/940)\r\n\r\nBy default `next-offline` will precache all the Next.js webpack emitted files and the user-defined static ones (inside `/static`) - essentially everything that is exported as well.\r\n\r\nIf you'd like to include some more or change the origin of your static files use the given workbox options:\r\n\r\n```js\r\nworkboxOpts: {\r\n  globPatterns: ['app/static/**/*', 'any/other/fileglob/to/cache'],\r\n  globDirectory: '.',\r\n  modifyUrlPrefix: {\r\n    'app': assetPrefix,\r\n  },\r\n  runtimeCaching: {...}\r\n}\r\n```\r\n\r\n## Development mode\r\nBy default `next-offline` will add a no-op service worker in development. If you want to provide your own pass its filepath to `devSwSrc` option. This is particularly useful if you want to test web push notifications in development, for example.\r\n\r\n```js\r\n// next.config.js\r\nconst withOffline = require('next-offline')\r\n\r\nmodule.exports = withOffline({\r\n  devSwSrc: '/path/to/my/dev/service-worker.js'\r\n})\r\n```\r\n\r\nYou can disable this behavior by setting the `generateInDevMode` option to `true`.\r\n\r\n\r\n## next export\r\n\r\nIn next-offline@3.0.0 we've rewritten the export functionality to work in more cases, more reliably, with less code thanks to some of the additions in Next 7.0.0!\r\n\r\nYou can read more about exporting at [Next.js docs]((https://github.com/zeit/next.js#static-html-export)) but next offline should Just Work™️.\r\n\r\n<hr />\r\n\r\nQuestions? Feedback? [Please let me know](https://github.com/hanford/next-offline/issues/new)\r\n\r\n## License (MIT)\r\n\r\n```\r\nWWWWWW||WWWWWW\r\n W W W||W W W\r\n      ||\r\n    ( OO )__________\r\n     /  |           \\\r\n    /o o|    MIT     \\\r\n    \\___/||_||__||_|| *\r\n         || ||  || ||\r\n        _||_|| _||_||\r\n       (__|__|(__|__|\r\n```\r\nCopyright © 2017-present [Jack Hanford](http://jackhanford.com), jackhanford@gmail.com\r\n\r\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\r\n\r\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\r\n\r\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r\n","readmeFilename":"readme.md"}