{"id":17635951,"url":"https://github.com/mohawk2/sw-turnkey","last_synced_at":"2025-03-30T03:43:07.625Z","repository":{"id":80539270,"uuid":"73647309","full_name":"mohawk2/sw-turnkey","owner":"mohawk2","description":"Turnkey drop-in Service Worker","archived":false,"fork":false,"pushed_at":"2020-05-26T00:19:22.000Z","size":20,"stargazers_count":0,"open_issues_count":3,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-05T06:14:22.746Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mohawk2.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2016-11-13T22:33:10.000Z","updated_at":"2020-05-26T00:19:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"6d20f254-7c9c-4040-83dc-9fdd25bc2abd","html_url":"https://github.com/mohawk2/sw-turnkey","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohawk2%2Fsw-turnkey","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohawk2%2Fsw-turnkey/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohawk2%2Fsw-turnkey/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohawk2%2Fsw-turnkey/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mohawk2","download_url":"https://codeload.github.com/mohawk2/sw-turnkey/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246273515,"owners_count":20750904,"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":[],"created_at":"2024-10-23T02:24:46.494Z","updated_at":"2025-03-30T03:43:07.601Z","avatar_url":"https://github.com/mohawk2.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sw-turnkey\n\n## Introduction\n\nDo *you* want to take advantage of the new(ish) Service Worker\nfunctionality?\n\nWould *you* like your website to be able to work offline, and to\ncontrol what's cached?\n\nHave *you* read up on Service Workers and thought it seems very powerful, but a huge learning curve?\n\nWould *you* like to just be able to drop a couple of files onto your\nwebsite, configure a JSON file, add one line to your HTML, and go?\n\n**HELP IS AT HAND**\n\n## How to use these files\n\n* Drop `serviceworker-install.js` and `serviceworker.js` at the top level of your app, so that its \"scope\" covers the whole app\n\n* Add this line to all your HTML (maybe adjusting for location):\n\n```\n\u003cscript src=\"serviceworker-install.js\"\u003e\u003c/script\u003e\n```\n\n* Edit `serviceworker-config.json` for your needs and put it in the\nsame location\n\n* Er...\n\n* That's it\n\n## Configuration\n\nThe content of `serviceworker-config.json` is a JSON-encoded hash (or\n\"object\"). Its keys, and the values for them:\n\n* `precache_urls`\n\nArray of URLs to precache when the service worker is installed.\n\n* `network_only` - don't cache at all\n\n* `cache_only` - once cached, never look up again\n\n* `network_first` - fetch from the network; if that fails, return cached\n\n* `debug` - boolean value for whether to log cache hits and misses\n\nThe values of these are all hashes with currently one key, \"re\" - a\nregular expression. A fetched URL is checked against each in the above\norder. When it matches, it will use that strategy. If none match, will\nfall back to cache first (aka \"stale while revalidate\").\n\nWhen the service worker `install` runs, it finds and removes from all its\navailable caches, cached versions of the config. This is to avoid older\nversions of the config being found and not overwritten, causing chaos.\n\n## Example 1: Single-Page App\n\nSee in the supplied directory `sample-app` for the actual files.\n\nImagine you have a single-page app (SPA) with a button that you click\nand it says \"Hi\". You want that to work offline!\n\n`index.html` before:\n\n```\n\u003c!doctype html\u003e\n\u003cbody\u003e\n\u003cinput type=button onclick=\"alert('Hi')\" value=\"Say Hi\" /\u003e\n\u003c/body\u003e\n```\n\nTo make this available offline, and generally be fast, we will use the\ndefault cache first strategy, together with pre-caching.\n\nTo use `sw-turnkey` here, drop `serviceworker-install.js` and\n`serviceworker.js` into the same directory as `index.html`.\nAlso place `serviceworker-config.json` with these contents:\n\n```\n{\n  \"precache_urls\": [\n    \"\",\n    \"serviceworker-install.js\",\n    \"serviceworker.js\",\n    \"serviceworker-config.json\"\n  ]\n}\n```\n\n`index.html` after:\n\n```\n\u003c!doctype html\u003e\n\u003cscript src=\"serviceworker-install.js\"\u003e\u003c/script\u003e\n\u003cbody\u003e\n\u003cinput type=button onclick=\"alert('Hi')\" value=\"Say Hi\" /\u003e\n\u003c/body\u003e\n```\n\n## Example 2: Single-Page App with dynamic component\n\nImagine you have a single-page app (SPA) with a button that you click\nand it loads dynamically-generated content from your website. You want\nthat to load fast, but you don't want to cache that generated content,\nand you don't mind it not working offline.\n\n`index.html` before:\n\n```\n\u003c!doctype html\u003e\n\u003cbody\u003e\n\u003ca href=\"dynamic.cgi\" target=\"iframe\"\u003eLoad\u003c/a\u003e\n\u003cbr /\u003e\n\u003ciframe name=iframe\u003e\u003c/iframe\u003e\n\u003c/body\u003e\n```\n\nYou'll need to add the same `serviceworker.js` and `serviceworker-install.js`.\n\nFor most of this content, the default cache first strategy,\ntogether with pre-caching works well, but we'll also use `network_only`\nfor `dynamic.cgi`. So we'll use this `serviceworker-config.json`:\n\n```\n{\n  \"precache_urls\": [\n    \"\",\n    \"serviceworker-install.js\",\n    \"serviceworker.js\",\n    \"serviceworker-config.json\"\n  ],\n  \"network_only\": {\n    \"re\": \"dynamic\\\\.cgi.*\"\n  }\n}\n```\n\n`index.html` after:\n\n```\n\u003c!doctype html\u003e\n\u003cscript src=\"serviceworker-install.js\"\u003e\u003c/script\u003e\n\u003cbody\u003e\n\u003ca href=\"dynamic.cgi\" target=\"iframe\"\u003eLoad\u003c/a\u003e\n\u003cbr /\u003e\n\u003ciframe name=iframe\u003e\u003c/iframe\u003e\n\u003c/body\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmohawk2%2Fsw-turnkey","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmohawk2%2Fsw-turnkey","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmohawk2%2Fsw-turnkey/lists"}