{"action":"created","comment":{"url":"https://api.github.com/repos/jeffposnick/WebFundamentals/pulls/comments/47977480","id":47977480,"diff_hunk":"@@ -0,0 +1,170 @@\n+---\n+layout: shared/narrow\n+published: false\n+title: \"Setting up a basic service worker\"\n+description: \"This section describes the most basic service worker possible. It shows you how a client gets, or registers, a service worker. It shows you how a service worker prepares to act as a proxy for its clients. Both subjects have more depth than is shown in this section. It's not intended to be comprehensive; It's intended to give you a basic foundation on which to build service worker knowledge.\"\n+authors:\n+  - josephmedley\n+published_on: 2015-10-01\n+updated_on: 2015-10-01\n+order: 4\n+key-takeaways:\n+  tldr:   \n+  - \"Call <code>register()</code> to install a service worker.\" \n+  - \"Learn to use promises before attempting service workers.\"\n+  - \"Service workers have a limited scope and requrie HTTPS.\"\n+  - \"The install process has two events: install and activate.\"\n+  - \"The user has to navigate before the service worker start proxying.\"\n+  - \"Call <code>waintUntil()</code> to keep a service worker out of its own way.\"\n+notes:\n+  promises:\n+    - \"<b>Promises</b>&mdash;Notice the use of <code>.then()</code> at the end of the <code>register()</code> function. This is an example of an ECMAScript 2015 construct called a <a href='http://www.html5rocks.com/en/tutorials/es6/promises/'>promise</a>. Service workers make heavy use of promises. If you've never used promises before, you should familiarize yourself with them before trying to implement a service worker.\"\n+  https-only:\n+    - \"<b>HTTPS Only</b>&mdash;Service workers can do almost whatever they want to HTTP requests and responses. Since this would make them targets for man-in-the-middle attacks, they must be served over HTTPS. This doesn't mean you need HTTPS for development and testing. Service workers served over localhost will also work.\"\n+  sws-dont-control:\n+    - \"<b>Service Workers Don't Take Control</b>&mdash;If you look around the web, you'll find that many of the pages discussing service workers refer to the service worker as 'taking control' of a page. But as we saw earlier, a service workers can't change a page's DOM. That's why it may be more accurate to think of a service worker as ready to proxy.\"\n+---\n+\n+<p class=\"intro\">\n+  This section describes the most basic service worker possible. It shows you \n+  how a client gets, or registers, a service worker. It shows you how a service \n+  worker prepares to act as a proxy for its clients. Both subjects have more \n+  depth than is shown in this section. It's not intended to be comprehensive; \n+  It's intended to give you a basic foundation on which to build service worker \n+  knowledge.\n+</p>\n+\n+{% include shared/toc.liquid %}\n+\n+{% include shared/takeaway.liquid list=page.key-takeaways.tldr %}\n+\n+## A client registers a service worker\n+\n+To use a service worker, a client tells the browser to install it by calling\n+`register()`. More than one page can implement `register()`. You can implement\n+it in either an inline script or a separate JavaScript file. Only the first a\n+browser encounters trigger the download. The most basic register implementation\n+looks like this:\n+\n+{% highlight javascript %}\n+// Does the browser support service workers?\n+if ('serviceWorker' in navigator) {\n+  // Yes\n+  navigator.serviceWorker.register('/service-worker.js').then(function() {\n+    // Actions not covered by this primer.\n+  });\n+} else {\n+  // No, but this client should work anyway.\n+};\n+{% endhighlight %}\n+\n+There's little more that needs to be said about clients. Now let's talk about \n+the service worker itself.\n+\n+{% include shared/note.liquid list=page.notes.promises %}\n+\n+## Where can the service worker play?\n+\n+A client tells the service worker where it can operate. This is called the\n+service worker's scope, and it's the lowest level of your website where the\n+service worker can be used. Let's look at the registration code again, but with\n+some changes to the `register()` parameters.\n+\n+{% highlight javascript %}\n+// Does the browser support service workers?\n+if ('serviceWorker' in navigator) {\n+  // Yes\n+  navigator.serviceWorker.register('/service-worker.js', {scope: '/'})\n+    .then(function() {\n+    // Actions not covered by this primer.\n+  });\n+} else {\n+  // No, but this client should work anyway.\n+}\n+{% endhighlight %}\n+\n+This code is functionally identical to the example given in the last section. \n+Both examples specify that the scope of the service worker is the entire domain. \n+If you want to restrict the service worker to part of a site, simply specify it \n+in the scope. \n+\n+For example, say that you have an auction site with different features for\n+buyers  and sellers. The buyers section is at `example.com/buyers` and the\n+sellers section  is at `example.com/sellers`. A service worker with a scope of\n+`/buyers` it can only  serve clients under `example.com/buyers`. It cannot serve\n+clients under  `example.com/sellers`. Similarly, a service worker with a scope\n+of `/sellers` can  only serve clients under `example.com/sellers`.\n+\n+{% include shared/note.liquid list=page.notes.https-only %}\n+\n+## A service worker installs and activates\n+\n+Let’s look at what happens during the installation of the service worker. To set \n+up a service worker, you need to implement two events:\n+\n+* `install`\n+* `activate`\n+\n+These events run one time for a service worker in a particular scope, no matter \n+how many clients use it.\n+\n+### Install\n+\n+Use the install event to get everything this service worker needs to operate.\n+This could include initializing caches,  prefetching data, handling old versions\n+of the current service worker, or  handling other service workers in\n+the same scope.\n+\n+The install event is kicked off immediately after the script containing it is\n+downloaded.\n+\n+{% highlight javascript %}\n+self.addEventListener('install', function(installEvent) {\n+  installEvent.waitUntil(\n+    // Prefetch resources and initialize data stores.\n+  );\n+});\n+{% endhighlight %}\n+\n+### Activate\n+\n+Use activate to clean up after a previous version of a service worker. This may\n+include migrating data in ways that can't be done while the old service worker\n+was active. This event is **not** called when a terminated service worker is\n+revived.","path":"src/content/en/fundamentals/primers/service-workers/a-basic-service-worker.markdown","position":142,"original_position":134,"commit_id":"fa6d52e4acc59d92d2db22b50abe36261dca5344","original_commit_id":"ee52349437206b51c645eab945de1dbd28e2f41a","user":{"login":"jpmedley","id":8204171,"avatar_url":"https://avatars.githubusercontent.com/u/8204171?v=3","gravatar_id":"","url":"https://api.github.com/users/jpmedley","html_url":"https://github.com/jpmedley","followers_url":"https://api.github.com/users/jpmedley/followers","following_url":"https://api.github.com/users/jpmedley/following{/other_user}","gists_url":"https://api.github.com/users/jpmedley/gists{/gist_id}","starred_url":"https://api.github.com/users/jpmedley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jpmedley/subscriptions","organizations_url":"https://api.github.com/users/jpmedley/orgs","repos_url":"https://api.github.com/users/jpmedley/repos","events_url":"https://api.github.com/users/jpmedley/events{/privacy}","received_events_url":"https://api.github.com/users/jpmedley/received_events","type":"User","site_admin":false},"body":"Added your line. ","created_at":"2015-12-17T23:39:30Z","updated_at":"2015-12-17T23:39:30Z","html_url":"https://github.com/jeffposnick/WebFundamentals/pull/1#discussion_r47977480","pull_request_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/jeffposnick/WebFundamentals/pulls/comments/47977480"},"html":{"href":"https://github.com/jeffposnick/WebFundamentals/pull/1#discussion_r47977480"},"pull_request":{"href":"https://api.github.com/repos/jeffposnick/WebFundamentals/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/jeffposnick/WebFundamentals/pulls/1","id":53999397,"html_url":"https://github.com/jeffposnick/WebFundamentals/pull/1","diff_url":"https://github.com/jeffposnick/WebFundamentals/pull/1.diff","patch_url":"https://github.com/jeffposnick/WebFundamentals/pull/1.patch","issue_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/issues/1","number":1,"state":"open","locked":false,"title":"Added the service workers primer.","user":{"login":"jeffposnick","id":1749548,"avatar_url":"https://avatars.githubusercontent.com/u/1749548?v=3","gravatar_id":"","url":"https://api.github.com/users/jeffposnick","html_url":"https://github.com/jeffposnick","followers_url":"https://api.github.com/users/jeffposnick/followers","following_url":"https://api.github.com/users/jeffposnick/following{/other_user}","gists_url":"https://api.github.com/users/jeffposnick/gists{/gist_id}","starred_url":"https://api.github.com/users/jeffposnick/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jeffposnick/subscriptions","organizations_url":"https://api.github.com/users/jeffposnick/orgs","repos_url":"https://api.github.com/users/jeffposnick/repos","events_url":"https://api.github.com/users/jeffposnick/events{/privacy}","received_events_url":"https://api.github.com/users/jeffposnick/received_events","type":"User","site_admin":false},"body":"@jpmedley I've created this PR with all the service worker material to facilitate commenting.","created_at":"2015-12-17T16:36:43Z","updated_at":"2015-12-17T23:39:30Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c61a15e6699805d12a4eb7a8a637fa888680e0e5","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/pulls/1/commits","review_comments_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/pulls/1/comments","review_comment_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/pulls/comments{/number}","comments_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/issues/1/comments","statuses_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/statuses/fa6d52e4acc59d92d2db22b50abe36261dca5344","head":{"label":"jeffposnick:with-service-workers","ref":"with-service-workers","sha":"fa6d52e4acc59d92d2db22b50abe36261dca5344","user":{"login":"jeffposnick","id":1749548,"avatar_url":"https://avatars.githubusercontent.com/u/1749548?v=3","gravatar_id":"","url":"https://api.github.com/users/jeffposnick","html_url":"https://github.com/jeffposnick","followers_url":"https://api.github.com/users/jeffposnick/followers","following_url":"https://api.github.com/users/jeffposnick/following{/other_user}","gists_url":"https://api.github.com/users/jeffposnick/gists{/gist_id}","starred_url":"https://api.github.com/users/jeffposnick/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jeffposnick/subscriptions","organizations_url":"https://api.github.com/users/jeffposnick/orgs","repos_url":"https://api.github.com/users/jeffposnick/repos","events_url":"https://api.github.com/users/jeffposnick/events{/privacy}","received_events_url":"https://api.github.com/users/jeffposnick/received_events","type":"User","site_admin":false},"repo":{"id":43522974,"name":"WebFundamentals","full_name":"jeffposnick/WebFundamentals","owner":{"login":"jeffposnick","id":1749548,"avatar_url":"https://avatars.githubusercontent.com/u/1749548?v=3","gravatar_id":"","url":"https://api.github.com/users/jeffposnick","html_url":"https://github.com/jeffposnick","followers_url":"https://api.github.com/users/jeffposnick/followers","following_url":"https://api.github.com/users/jeffposnick/following{/other_user}","gists_url":"https://api.github.com/users/jeffposnick/gists{/gist_id}","starred_url":"https://api.github.com/users/jeffposnick/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jeffposnick/subscriptions","organizations_url":"https://api.github.com/users/jeffposnick/orgs","repos_url":"https://api.github.com/users/jeffposnick/repos","events_url":"https://api.github.com/users/jeffposnick/events{/privacy}","received_events_url":"https://api.github.com/users/jeffposnick/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jeffposnick/WebFundamentals","description":"Best practices for modern web development","fork":true,"url":"https://api.github.com/repos/jeffposnick/WebFundamentals","forks_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/forks","keys_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/teams","hooks_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/hooks","issue_events_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/issues/events{/number}","events_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/events","assignees_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/assignees{/user}","branches_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/branches{/branch}","tags_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/tags","blobs_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/git/refs{/sha}","trees_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/statuses/{sha}","languages_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/languages","stargazers_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/stargazers","contributors_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/contributors","subscribers_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/subscribers","subscription_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/subscription","commits_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/commits{/sha}","git_commits_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/git/commits{/sha}","comments_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/comments{/number}","issue_comment_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/issues/comments{/number}","contents_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/contents/{+path}","compare_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/merges","archive_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/downloads","issues_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/issues{/number}","pulls_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/pulls{/number}","milestones_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/milestones{/number}","notifications_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/labels{/name}","releases_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/releases{/id}","created_at":"2015-10-01T21:45:47Z","updated_at":"2015-10-01T21:46:18Z","pushed_at":"2015-12-17T23:35:45Z","git_url":"git://github.com/jeffposnick/WebFundamentals.git","ssh_url":"git@github.com:jeffposnick/WebFundamentals.git","clone_url":"https://github.com/jeffposnick/WebFundamentals.git","svn_url":"https://github.com/jeffposnick/WebFundamentals","homepage":"https://developers.google.com/web/fundamentals","size":894261,"stargazers_count":0,"watchers_count":0,"language":"HTML","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"jeffposnick:sans-service-workers","ref":"sans-service-workers","sha":"d089cb32b183098aa1e91bf73795d4b574d8d95c","user":{"login":"jeffposnick","id":1749548,"avatar_url":"https://avatars.githubusercontent.com/u/1749548?v=3","gravatar_id":"","url":"https://api.github.com/users/jeffposnick","html_url":"https://github.com/jeffposnick","followers_url":"https://api.github.com/users/jeffposnick/followers","following_url":"https://api.github.com/users/jeffposnick/following{/other_user}","gists_url":"https://api.github.com/users/jeffposnick/gists{/gist_id}","starred_url":"https://api.github.com/users/jeffposnick/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jeffposnick/subscriptions","organizations_url":"https://api.github.com/users/jeffposnick/orgs","repos_url":"https://api.github.com/users/jeffposnick/repos","events_url":"https://api.github.com/users/jeffposnick/events{/privacy}","received_events_url":"https://api.github.com/users/jeffposnick/received_events","type":"User","site_admin":false},"repo":{"id":43522974,"name":"WebFundamentals","full_name":"jeffposnick/WebFundamentals","owner":{"login":"jeffposnick","id":1749548,"avatar_url":"https://avatars.githubusercontent.com/u/1749548?v=3","gravatar_id":"","url":"https://api.github.com/users/jeffposnick","html_url":"https://github.com/jeffposnick","followers_url":"https://api.github.com/users/jeffposnick/followers","following_url":"https://api.github.com/users/jeffposnick/following{/other_user}","gists_url":"https://api.github.com/users/jeffposnick/gists{/gist_id}","starred_url":"https://api.github.com/users/jeffposnick/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jeffposnick/subscriptions","organizations_url":"https://api.github.com/users/jeffposnick/orgs","repos_url":"https://api.github.com/users/jeffposnick/repos","events_url":"https://api.github.com/users/jeffposnick/events{/privacy}","received_events_url":"https://api.github.com/users/jeffposnick/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jeffposnick/WebFundamentals","description":"Best practices for modern web development","fork":true,"url":"https://api.github.com/repos/jeffposnick/WebFundamentals","forks_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/forks","keys_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/teams","hooks_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/hooks","issue_events_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/issues/events{/number}","events_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/events","assignees_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/assignees{/user}","branches_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/branches{/branch}","tags_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/tags","blobs_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/git/refs{/sha}","trees_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/statuses/{sha}","languages_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/languages","stargazers_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/stargazers","contributors_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/contributors","subscribers_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/subscribers","subscription_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/subscription","commits_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/commits{/sha}","git_commits_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/git/commits{/sha}","comments_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/comments{/number}","issue_comment_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/issues/comments{/number}","contents_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/contents/{+path}","compare_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/merges","archive_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/downloads","issues_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/issues{/number}","pulls_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/pulls{/number}","milestones_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/milestones{/number}","notifications_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/labels{/name}","releases_url":"https://api.github.com/repos/jeffposnick/WebFundamentals/releases{/id}","created_at":"2015-10-01T21:45:47Z","updated_at":"2015-10-01T21:46:18Z","pushed_at":"2015-12-17T23:35:45Z","git_url":"git://github.com/jeffposnick/WebFundamentals.git","ssh_url":"git@github.com:jeffposnick/WebFundamentals.git","clone_url":"https://github.com/jeffposnick/WebFundamentals.git","svn_url":"https://github.com/jeffposnick/WebFundamentals","homepage":"https://developers.google.com/web/fundamentals","size":894261,"stargazers_count":0,"watchers_count":0,"language":"HTML","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/jeffposnick/WebFundamentals/pulls/1"},"html":{"href":"https://github.com/jeffposnick/WebFundamentals/pull/1"},"issue":{"href":"https://api.github.com/repos/jeffposnick/WebFundamentals/issues/1"},"comments":{"href":"https://api.github.com/repos/jeffposnick/WebFundamentals/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/jeffposnick/WebFundamentals/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/jeffposnick/WebFundamentals/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/jeffposnick/WebFundamentals/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/jeffposnick/WebFundamentals/statuses/fa6d52e4acc59d92d2db22b50abe36261dca5344"}}}}