{"id":20565790,"url":"https://github.com/phenax/service-worker-js","last_synced_at":"2025-08-10T06:37:24.537Z","repository":{"id":83227668,"uuid":"77744609","full_name":"phenax/service-worker-js","owner":"phenax","description":"Service worker recipies library","archived":false,"fork":false,"pushed_at":"2018-04-28T14:28:34.000Z","size":21,"stargazers_count":27,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-14T15:49:56.639Z","etag":null,"topics":["javascript","library","promise","service-worker"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/phenax.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2016-12-31T15:02:14.000Z","updated_at":"2021-12-14T04:13:27.000Z","dependencies_parsed_at":null,"dependency_job_id":"fffe20e0-6842-47b2-9eea-cd9bb0c6ebfe","html_url":"https://github.com/phenax/service-worker-js","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/phenax/service-worker-js","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phenax%2Fservice-worker-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phenax%2Fservice-worker-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phenax%2Fservice-worker-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phenax%2Fservice-worker-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/phenax","download_url":"https://codeload.github.com/phenax/service-worker-js/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/phenax%2Fservice-worker-js/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269687935,"owners_count":24459393,"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","status":"online","status_checked_at":"2025-08-10T02:00:08.965Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["javascript","library","promise","service-worker"],"created_at":"2024-11-16T04:39:08.944Z","updated_at":"2025-08-10T06:37:24.516Z","avatar_url":"https://github.com/phenax.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ServiceWorkersJS\n\nService worker recipe library\n\n\n### Usage\n\n* Import the library\n```javascript\nimportScripts([ '../service-worker.js' ]);\n```\n\n* Create the service worker instance\n```javascript\nconst sw= new ServiceWorkerJS();\n```\n\n* Precaching\n```javascript\nsw.precache('precached-scripts', [\n\t'/main.js',\n\t'/vendor.js',\n]);\n```\n\n* Simple routing\n\n```javascript\n\n// Custom recipe\nsw.addRoute('script.js', { method: 'get' }, e =\u003e { /* Do some stuff and return a promise */ });\n\n// Use cache first recipe for style.css file\n// (The responses from the network will be cached in 'cache-scripts')\nsw.addRoute('style.css', { method: 'get' }, sw.cacheFirst({ cache: 'cache-styles' }));\n\n// Use network only recipe for main.js file\n// (The timeout for the request is 4seconds(10 seconds by default))\nsw.addRoute('main.js', { method: 'get' }, sw.networkOnly({ timeout: 4000 }));\n\n\n// You can directly pass a route object\nconst scriptRoute= new SWRoute('script-1.js', { method: 'get' }, e =\u003e fetch(e.request));\nsw.addRoute(scriptRoute);\n\n```\n\n\n* Push Notification[Not ready]\n\n```javascript\n\n// For handling push notifications\nsw.onPushNotification= event =\u003e {\n\n\tconsole.log('Got a notification', event);\n\n\t// Needs to return a promise that resolves with the notification data\n\treturn Promise.resolve({\n\t\ttitle: 'Got a notification',\n\t\toptions: {\n\t\t\tbody: 'Lorem ipsum for this notification',\n\t\t\ticon: 'img/icon.png',\n\t\t\tbadge: 'img/badge.png',\n\t\t}\n\t});\n};\n\n```\n\n\n\n\n* Available recipies\n\n\t- Race\n\t\tRaces the network and cache and responds with whatever happens first\n\t\tOptions- ``` sw.race({ cache: 'cache-scripts', timeout: 3000 }) ```\n\n\t- CacheFirst\n\t\tCheck the cache for the file and if it wasnt present, make a fetch request to the network\n\t\tOptions- ``` sw.cacheFirst({ cache: 'cache-styles' }) ```\n\n\t- NetworkFirst\n\t\tMake a fetch request to the network, if something goes wrong, check the cache for the file\n\t\tOptions- ``` sw.networkFirst({ cache: 'cache-scripts', timeout: 3000 }) ```\n\n\t- CacheOnly\n\t\tCheck the cache for the file and if the file isnt precached, respond with an error or a default response.\n\t\tOptions- ``` sw.cacheOnly({ default: new Response('Dummy response') }) ```\n\n\t- NetworkOnly\n\t\tCheck the network for a response and respond with an error if it takes more than the timeout period.\n\t\tOptions- ``` sw.networkOnly({ timeout: 4000 }) ```\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphenax%2Fservice-worker-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fphenax%2Fservice-worker-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fphenax%2Fservice-worker-js/lists"}