{"id":13695714,"url":"https://github.com/oliviertassinari/serviceworker-webpack-plugin","last_synced_at":"2025-05-03T13:33:03.394Z","repository":{"id":57109578,"uuid":"60124445","full_name":"oliviertassinari/serviceworker-webpack-plugin","owner":"oliviertassinari","description":"Simplifies creation of a service worker to serve your webpack bundles. :recycle:","archived":true,"fork":false,"pushed_at":"2019-07-20T12:56:17.000Z","size":510,"stargazers_count":458,"open_issues_count":26,"forks_count":76,"subscribers_count":12,"default_branch":"master","last_synced_at":"2024-11-06T02:05:56.172Z","etag":null,"topics":["plugin","serviceworker","webpack"],"latest_commit_sha":null,"homepage":"https://oliviertassinari.github.io/serviceworker-webpack-plugin/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oliviertassinari.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-05-31T21:23:31.000Z","updated_at":"2024-05-15T09:28:52.000Z","dependencies_parsed_at":"2022-08-20T17:41:03.202Z","dependency_job_id":null,"html_url":"https://github.com/oliviertassinari/serviceworker-webpack-plugin","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oliviertassinari%2Fserviceworker-webpack-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oliviertassinari%2Fserviceworker-webpack-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oliviertassinari%2Fserviceworker-webpack-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oliviertassinari%2Fserviceworker-webpack-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oliviertassinari","download_url":"https://codeload.github.com/oliviertassinari/serviceworker-webpack-plugin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224364361,"owners_count":17299055,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["plugin","serviceworker","webpack"],"created_at":"2024-08-02T18:00:32.686Z","updated_at":"2024-11-12T23:30:47.688Z","avatar_url":"https://github.com/oliviertassinari.png","language":"JavaScript","funding_links":[],"categories":["Tools","Libraries and Tools","JavaScript"],"sub_categories":["Webpack and Framework Plugins"],"readme":"# serviceworker-webpack-plugin\n\n\u003e Simplifies creation of a service worker to serve your webpack bundles.\n\n[![npm version](https://img.shields.io/npm/v/serviceworker-webpack-plugin.svg?style=flat-square)](https://www.npmjs.com/package/serviceworker-webpack-plugin)\n[![npm downloads](https://img.shields.io/npm/dm/serviceworker-webpack-plugin.svg?style=flat-square)](https://www.npmjs.com/package/serviceworker-webpack-plugin)\n[![Build Status](https://travis-ci.org/oliviertassinari/serviceworker-webpack-plugin.svg?branch=master)](https://travis-ci.org/oliviertassinari/serviceworker-webpack-plugin)\n\n[![Dependencies](https://img.shields.io/david/oliviertassinari/serviceworker-webpack-plugin.svg?style=flat-square)](https://david-dm.org/oliviertassinari/serviceworker-webpack-plugin)\n[![DevDependencies](https://img.shields.io/david/dev/oliviertassinari/serviceworker-webpack-plugin.svg?style=flat-square)](https://david-dm.org/oliviertassinari/serviceworker-webpack-plugin#info=devDependencies\u0026view=list)\n\n## Installation\n\n```sh\nnpm install serviceworker-webpack-plugin\n```\n\n## The problem solved\n\nWhen building a service worker, you probably want to [cache all](https://github.com/oliviertassinari/serviceworker-webpack-plugin/blob/master/docs/src/sw.js#L38)\nyour assets during the `install` phase.\nBut in order to do so, you need their **names**.\nThat's not simple when you are using *Webpack*:\n- The assets names are *non-deterministic* when taking advantage of the long term caching.\n- The assets list can even evolve over time as you add splitting points or more resources.\n- You want to be able to use your service worker with the **dev-server** mode of Webpack.\n- You want to keep the build process as simple as possible.\n\n## Setup\n\n### 1. Add the plugin to your webpack config\n\n```js\nimport ServiceWorkerWebpackPlugin from 'serviceworker-webpack-plugin';\n\n...\n\n  plugins: [\n    new ServiceWorkerWebpackPlugin({\n      entry: path.join(__dirname, 'src/sw.js'),\n    }),\n  ],\n\n```\n\n### 2. Register the service worker in your main JS thread\n\n```js\nimport runtime from 'serviceworker-webpack-plugin/lib/runtime';\n\nif ('serviceWorker' in navigator) {\n  const registration = runtime.register();\n}\n```\n\n### 3. Write your own `sw.js`\n\nYou can now use the global `serviceWorkerOption` variable in your `sw.js`.\nE.g. In our example this object looks like:\n```js\n{\n  assets: [\n    './main.256334452761ef349e91.js',\n  ],\n}\n```\n\n## Simple example\n\nYou can have a look at the [`/docs`](https://github.com/oliviertassinari/serviceworker-webpack-plugin/tree/master/docs)\nfolder if you need a full working example.\n\n## API\n\n### `ServiceWorkerWebpackPlugin(options)`\n\n- `options`\n - `entry`, **required**, *string*:\nPath to the actual service worker implementation.\n - `filename`, *string*, default `'sw.js'`:\nRelative (from the webpack's config `output.path`) output path for emitted script.\n - `excludes`, *array*, default `['**/.*', '**/*.map']`:\nExclude matched assets from being added to the `serviceWorkerOption.assets` variable. (Blacklist)\n - `includes`, *array*, default `['**/*']`:\nInclude matched assets added to the `serviceWorkerOption.assets` variable. (Whitelist)\n - `publicPath`, *string*, default `'/'`:\nSpecifies the public URL address of the output files when referenced in a browser.\nWe use this value to load the service worker over the network.\n - `template`, *function*, default noop:\nThis callback function can be used to inject statically generated service worker.\nIt's taking a `serviceWorkerOption` argument and must return a promise.\n- `transformOptions`, *function*:\nThis callback function receives a raw `serviceWorkerOption` argument.\nThe `jsonStats` key contains all the webpack build information.\n- `minimize`:\nWhether to minimize output. Defaults to `process.env.NODE_ENV === 'production'`\n\n### `runtime(options)`\n\n- `options`: That's forwarded to the `options` argument of the\n[`ServiceWorkerContainer.register()`](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register) function.\n\n## Credit\n\n- The [offline-plugin](https://github.com/NekR/offline-plugin) package\nwas a great source of inspiration.\n- The [html-webpack-plugin](https://github.com/ampedandwired/html-webpack-plugin)\npackage was also really helpful.\n\n## Why simply not use the `offline-plugin`?\n\nI wouldn't have been able to write this plugin without the [offline-plugin](https://github.com/NekR/offline-plugin) project.\nThanks [@NekR](https://github.com/NekR/offline-plugin) for sharing it!\n\nStill, soon after using it, I realized that it wasn't what I was looking for.\n - The *abstraction* provided was **too high**.\n (I needed to build some custom fetch logic.)\n - It was making me, even more, **dependent** on Webpack.\n (What if later, I want to switch to another build system?)\n\nHence, I decided to change the approach and created this **thin layer** on\ntop of Webpack to solve the assets name issue. Nothing more.\n\nIf you don't care about my two issues with `offline-plugin`\nthen you don't need to use this package, `offline-plugin` is great.\n\n## The specs\n\n- [Service Workers](https://w3c.github.io/ServiceWorker/)\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foliviertassinari%2Fserviceworker-webpack-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foliviertassinari%2Fserviceworker-webpack-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foliviertassinari%2Fserviceworker-webpack-plugin/lists"}