A place to cache linked articles (think custom and personal wayback machine)
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

index.html 50KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. <!doctype html><!-- This is a valid HTML5 document. -->
  2. <!-- Screen readers, SEO, extensions and so on. -->
  3. <html lang=fr>
  4. <!-- Has to be within the first 1024 bytes, hence before the <title>
  5. See: https://www.w3.org/TR/2012/CR-html5-20121217/document-metadata.html#charset -->
  6. <meta charset=utf-8>
  7. <!-- Why no `X-UA-Compatible` meta: https://stackoverflow.com/a/6771584 -->
  8. <!-- The viewport meta is quite crowded and we are responsible for that.
  9. See: https://codepen.io/tigt/post/meta-viewport-for-2015 -->
  10. <meta name=viewport content="width=device-width,minimum-scale=1,initial-scale=1,shrink-to-fit=no">
  11. <!-- Required to make a valid HTML5 document. -->
  12. <title>ServiceWorker: Revolution of the Web Platform (archive) — David Larlet</title>
  13. <!-- Generated from https://realfavicongenerator.net/ such a mess. -->
  14. <link rel="apple-touch-icon" sizes="180x180" href="/static/david/icons/apple-touch-icon.png">
  15. <link rel="icon" type="image/png" sizes="32x32" href="/static/david/icons/favicon-32x32.png">
  16. <link rel="icon" type="image/png" sizes="16x16" href="/static/david/icons/favicon-16x16.png">
  17. <link rel="manifest" href="/manifest.json">
  18. <link rel="mask-icon" href="/static/david/icons/safari-pinned-tab.svg" color="#5bbad5">
  19. <link rel="shortcut icon" href="/static/david/icons/favicon.ico">
  20. <meta name="apple-mobile-web-app-title" content="David Larlet">
  21. <meta name="application-name" content="David Larlet">
  22. <meta name="msapplication-TileColor" content="#da532c">
  23. <meta name="msapplication-config" content="/static/david/icons/browserconfig.xml">
  24. <meta name="theme-color" content="#f0f0ea">
  25. <!-- That good ol' feed, subscribe :p. -->
  26. <link rel=alternate type="application/atom+xml" title=Feed href="/david/log/">
  27. <meta name="robots" content="noindex, nofollow">
  28. <meta content="origin-when-cross-origin" name="referrer">
  29. <!-- Canonical URL for SEO purposes -->
  30. <link rel="canonical" href="https://ponyfoo.com/articles/serviceworker-revolution">
  31. <style>
  32. /* http://meyerweb.com/eric/tools/css/reset/ */
  33. html, body, div, span,
  34. h1, h2, h3, h4, h5, h6, p, blockquote, pre,
  35. a, abbr, address, big, cite, code,
  36. del, dfn, em, img, ins,
  37. small, strike, strong, tt, var,
  38. dl, dt, dd, ol, ul, li,
  39. fieldset, form, label, legend,
  40. table, caption, tbody, tfoot, thead, tr, th, td,
  41. article, aside, canvas, details, embed,
  42. figure, figcaption, footer, header, hgroup,
  43. menu, nav, output, ruby, section, summary,
  44. time, mark, audio, video {
  45. margin: 0;
  46. padding: 0;
  47. border: 0;
  48. font-size: 100%;
  49. font: inherit;
  50. vertical-align: baseline;
  51. }
  52. /* HTML5 display-role reset for older browsers */
  53. article, aside, details, figcaption, figure,
  54. footer, header, hgroup, menu, nav, section { display: block; }
  55. body { line-height: 1; }
  56. blockquote, q { quotes: none; }
  57. blockquote:before, blockquote:after,
  58. q:before, q:after {
  59. content: '';
  60. content: none;
  61. }
  62. table {
  63. border-collapse: collapse;
  64. border-spacing: 0;
  65. }
  66. /* http://practicaltypography.com/equity.html */
  67. /* https://calendar.perfplanet.com/2016/no-font-face-bulletproof-syntax/ */
  68. /* https://www.filamentgroup.com/lab/js-web-fonts.html */
  69. @font-face {
  70. font-family: 'EquityTextB';
  71. src: url('/static/david/css/fonts/Equity-Text-B-Regular-webfont.woff2') format('woff2'),
  72. url('/static/david/css/fonts/Equity-Text-B-Regular-webfont.woff') format('woff');
  73. font-weight: 300;
  74. font-style: normal;
  75. font-display: swap;
  76. }
  77. @font-face {
  78. font-family: 'EquityTextB';
  79. src: url('/static/david/css/fonts/Equity-Text-B-Italic-webfont.woff2') format('woff2'),
  80. url('/static/david/css/fonts/Equity-Text-B-Italic-webfont.woff') format('woff');
  81. font-weight: 300;
  82. font-style: italic;
  83. font-display: swap;
  84. }
  85. @font-face {
  86. font-family: 'EquityTextB';
  87. src: url('/static/david/css/fonts/Equity-Text-B-Bold-webfont.woff2') format('woff2'),
  88. url('/static/david/css/fonts/Equity-Text-B-Bold-webfont.woff') format('woff');
  89. font-weight: 700;
  90. font-style: normal;
  91. font-display: swap;
  92. }
  93. @font-face {
  94. font-family: 'ConcourseT3';
  95. src: url('/static/david/css/fonts/concourse_t3_regular-webfont-20190806.woff2') format('woff2'),
  96. url('/static/david/css/fonts/concourse_t3_regular-webfont-20190806.woff') format('woff');
  97. font-weight: 300;
  98. font-style: normal;
  99. font-display: swap;
  100. }
  101. /* http://practice.typekit.com/lesson/caring-about-opentype-features/ */
  102. body {
  103. /* http://www.cssfontstack.com/ Palatino 99% Win 86% Mac */
  104. font-family: "EquityTextB", Palatino, serif;
  105. background-color: #f0f0ea;
  106. color: #07486c;
  107. font-kerning: normal;
  108. -moz-osx-font-smoothing: grayscale;
  109. -webkit-font-smoothing: subpixel-antialiased;
  110. text-rendering: optimizeLegibility;
  111. font-variant-ligatures: common-ligatures contextual;
  112. font-feature-settings: "kern", "liga", "clig", "calt";
  113. }
  114. pre, code, kbd, samp, var, tt {
  115. font-family: 'TriplicateT4c', monospace;
  116. }
  117. em {
  118. font-style: italic;
  119. color: #323a45;
  120. }
  121. strong {
  122. font-weight: bold;
  123. color: black;
  124. }
  125. nav {
  126. background-color: #323a45;
  127. color: #f0f0ea;
  128. display: flex;
  129. justify-content: space-around;
  130. padding: 1rem .5rem;
  131. }
  132. nav:last-child {
  133. border-bottom: 1vh solid #2d7474;
  134. }
  135. nav a {
  136. color: #f0f0ea;
  137. }
  138. nav abbr {
  139. border-bottom: 1px dotted white;
  140. }
  141. h1 {
  142. border-top: 1vh solid #2d7474;
  143. border-bottom: .2vh dotted #2d7474;
  144. background-color: #e3e1e1;
  145. color: #323a45;
  146. text-align: center;
  147. padding: 5rem 0 4rem 0;
  148. width: 100%;
  149. font-family: 'ConcourseT3';
  150. display: flex;
  151. flex-direction: column;
  152. }
  153. h1.single {
  154. padding-bottom: 10rem;
  155. }
  156. h1 span {
  157. position: absolute;
  158. top: 1vh;
  159. left: 20%;
  160. line-height: 0;
  161. }
  162. h1 span a {
  163. line-height: 1.7;
  164. padding: 1rem 1.2rem .6rem 1.2rem;
  165. border-radius: 0 0 6% 6%;
  166. background: #2d7474;
  167. font-size: 1.3rem;
  168. color: white;
  169. text-decoration: none;
  170. }
  171. h2 {
  172. margin: 4rem 0 1rem;
  173. border-top: .2vh solid #2d7474;
  174. padding-top: 1vh;
  175. }
  176. h3 {
  177. text-align: center;
  178. margin: 3rem 0 .75em;
  179. }
  180. hr {
  181. height: .4rem;
  182. width: .4rem;
  183. border-radius: .4rem;
  184. background: #07486c;
  185. margin: 2.5rem auto;
  186. }
  187. time {
  188. display: bloc;
  189. margin-left: 0 !important;
  190. }
  191. ul, ol {
  192. margin: 2rem;
  193. }
  194. ul {
  195. list-style-type: square;
  196. }
  197. a {
  198. text-decoration-skip-ink: auto;
  199. text-decoration-thickness: 0.05em;
  200. text-underline-offset: 0.09em;
  201. }
  202. article {
  203. max-width: 50rem;
  204. display: flex;
  205. flex-direction: column;
  206. margin: 2rem auto;
  207. }
  208. article.single {
  209. border-top: .2vh dotted #2d7474;
  210. margin: -6rem auto 1rem auto;
  211. background: #f0f0ea;
  212. padding: 2rem;
  213. }
  214. article p:last-child {
  215. margin-bottom: 1rem;
  216. }
  217. p {
  218. padding: 0 .5rem;
  219. margin-left: 3rem;
  220. }
  221. p + p,
  222. figure + p {
  223. margin-top: 2rem;
  224. }
  225. blockquote {
  226. background-color: #e3e1e1;
  227. border-left: .5vw solid #2d7474;
  228. display: flex;
  229. flex-direction: column;
  230. align-items: center;
  231. padding: 1rem;
  232. margin: 1.5rem;
  233. }
  234. blockquote cite {
  235. font-style: italic;
  236. }
  237. blockquote p {
  238. margin-left: 0;
  239. }
  240. figure {
  241. border-top: .2vh solid #2d7474;
  242. background-color: #e3e1e1;
  243. text-align: center;
  244. padding: 1.5rem 0;
  245. margin: 1rem 0 0;
  246. font-size: 1.5rem;
  247. width: 100%;
  248. }
  249. figure img {
  250. max-width: 250px;
  251. max-height: 250px;
  252. border: .5vw solid #323a45;
  253. padding: 1px;
  254. }
  255. figcaption {
  256. padding: 1rem;
  257. line-height: 1.4;
  258. }
  259. aside {
  260. display: flex;
  261. flex-direction: column;
  262. background-color: #e3e1e1;
  263. padding: 1rem 0;
  264. border-bottom: .2vh solid #07486c;
  265. }
  266. aside p {
  267. max-width: 50rem;
  268. margin: 0 auto;
  269. }
  270. /* https://fvsch.com/code/css-locks/ */
  271. p, li, pre, code, kbd, samp, var, tt, time, details, figcaption {
  272. font-size: 1rem;
  273. line-height: calc( 1.5em + 0.2 * 1rem );
  274. }
  275. h1 {
  276. font-size: 1.9rem;
  277. line-height: calc( 1.2em + 0.2 * 1rem );
  278. }
  279. h2 {
  280. font-size: 1.6rem;
  281. line-height: calc( 1.3em + 0.2 * 1rem );
  282. }
  283. h3 {
  284. font-size: 1.35rem;
  285. line-height: calc( 1.4em + 0.2 * 1rem );
  286. }
  287. @media (min-width: 20em) {
  288. /* The (100vw - 20rem) / (50 - 20) part
  289. resolves to 0-1rem, depending on the
  290. viewport width (between 20em and 50em). */
  291. p, li, pre, code, kbd, samp, var, tt, time, details, figcaption {
  292. font-size: calc( 1rem + .6 * (100vw - 20rem) / (50 - 20) );
  293. line-height: calc( 1.5em + 0.2 * (100vw - 50rem) / (20 - 50) );
  294. margin-left: 0;
  295. }
  296. h1 {
  297. font-size: calc( 1.9rem + 1.5 * (100vw - 20rem) / (50 - 20) );
  298. line-height: calc( 1.2em + 0.2 * (100vw - 50rem) / (20 - 50) );
  299. }
  300. h2 {
  301. font-size: calc( 1.5rem + 1.5 * (100vw - 20rem) / (50 - 20) );
  302. line-height: calc( 1.3em + 0.2 * (100vw - 50rem) / (20 - 50) );
  303. }
  304. h3 {
  305. font-size: calc( 1.35rem + 1.5 * (100vw - 20rem) / (50 - 20) );
  306. line-height: calc( 1.4em + 0.2 * (100vw - 50rem) / (20 - 50) );
  307. }
  308. }
  309. @media (min-width: 50em) {
  310. /* The right part of the addition *must* be a
  311. rem value. In this example we *could* change
  312. the whole declaration to font-size:2.5rem,
  313. but if our baseline value was not expressed
  314. in rem we would have to use calc. */
  315. p, li, pre, code, kbd, samp, var, tt, time, details, figcaption {
  316. font-size: calc( 1rem + .6 * 1rem );
  317. line-height: 1.5em;
  318. }
  319. p, li, pre, details {
  320. margin-left: 3rem;
  321. }
  322. h1 {
  323. font-size: calc( 1.9rem + 1.5 * 1rem );
  324. line-height: 1.2em;
  325. }
  326. h2 {
  327. font-size: calc( 1.5rem + 1.5 * 1rem );
  328. line-height: 1.3em;
  329. }
  330. h3 {
  331. font-size: calc( 1.35rem + 1.5 * 1rem );
  332. line-height: 1.4em;
  333. }
  334. figure img {
  335. max-width: 500px;
  336. max-height: 500px;
  337. }
  338. }
  339. figure.unsquared {
  340. margin-bottom: 1.5rem;
  341. }
  342. figure.unsquared img {
  343. height: inherit;
  344. }
  345. @media print {
  346. body { font-size: 100%; }
  347. a:after { content: " (" attr(href) ")"; }
  348. a, a:link, a:visited, a:after {
  349. text-decoration: underline;
  350. text-shadow: none !important;
  351. background-image: none !important;
  352. background: white;
  353. color: black;
  354. }
  355. abbr[title] { border-bottom: 0; }
  356. abbr[title]:after { content: " (" attr(title) ")"; }
  357. img { page-break-inside: avoid; }
  358. @page { margin: 2cm .5cm; }
  359. h1, h2, h3 { page-break-after: avoid; }
  360. p3 { orphans: 3; widows: 3; }
  361. img {
  362. max-width: 250px !important;
  363. max-height: 250px !important;
  364. }
  365. nav, aside { display: none; }
  366. }
  367. ul.with_columns {
  368. column-count: 1;
  369. }
  370. @media (min-width: 20em) {
  371. ul.with_columns {
  372. column-count: 2;
  373. }
  374. }
  375. @media (min-width: 50em) {
  376. ul.with_columns {
  377. column-count: 3;
  378. }
  379. }
  380. ul.with_two_columns {
  381. column-count: 1;
  382. }
  383. @media (min-width: 20em) {
  384. ul.with_two_columns {
  385. column-count: 1;
  386. }
  387. }
  388. @media (min-width: 50em) {
  389. ul.with_two_columns {
  390. column-count: 2;
  391. }
  392. }
  393. .gallery {
  394. display: flex;
  395. flex-wrap: wrap;
  396. justify-content: space-around;
  397. }
  398. .gallery figure img {
  399. margin-left: 1rem;
  400. margin-right: 1rem;
  401. }
  402. .gallery figure figcaption {
  403. font-family: 'ConcourseT3'
  404. }
  405. footer {
  406. font-family: 'ConcourseT3';
  407. display: flex;
  408. flex-direction: column;
  409. border-top: 3px solid white;
  410. padding: 4rem 0;
  411. background-color: #07486c;
  412. color: white;
  413. }
  414. footer > * {
  415. max-width: 50rem;
  416. margin: 0 auto;
  417. }
  418. footer a {
  419. color: #f1c40f;
  420. }
  421. footer .avatar {
  422. width: 200px;
  423. height: 200px;
  424. border-radius: 50%;
  425. float: left;
  426. -webkit-shape-outside: circle();
  427. shape-outside: circle();
  428. margin-right: 2rem;
  429. padding: 2px 5px 5px 2px;
  430. background: white;
  431. border-left: 1px solid #f1c40f;
  432. border-top: 1px solid #f1c40f;
  433. border-right: 5px solid #f1c40f;
  434. border-bottom: 5px solid #f1c40f;
  435. }
  436. </style>
  437. <h1>
  438. <span><a id="jumper" href="#jumpto" title="Un peu perdu ?">?</a></span>
  439. ServiceWorker: Revolution of the Web Platform (archive)
  440. <time>Pour la pérennité des contenus liés. Non-indexé, retrait sur simple email.</time>
  441. </h1>
  442. <section>
  443. <article>
  444. <h3><a href="https://ponyfoo.com/articles/serviceworker-revolution">Source originale du contenu</a></h3>
  445. <section class="at-teaser"><div class="at-teaser-markdown md-markdown"><p>While not the most amusingly named feature of the web platform, everything seems to point at ServiceWorker being the <strong>most significant addition</strong> to the web platform since the introduction of <em>AJAX</em> &#x2013; over 10 years ago. Not to be confused with WebWorker <em>(used to offload intense compute operations onto another execution thread)</em>, ServiceWorker allows you to intercept <em>(and hijack)</em> network requests originating from your site before they&#x2019;re even dispatched. This article explores how it works, what it means and what it enables, and how you can implement it by following a case study.</p></div></section>
  446. <section class="at-container de-host"><div class="de-column"><article itemscope="itemscope" itemtype="http://schema.org/BlogPosting" class="at-article ly-section"><meta itemprop="dateCreated" content="2015-10-21T03:22:23+00:00"/><meta itemprop="dateModified" content="2015-10-28T07:10:17+00:00"/><meta itemprop="datePublished" content="2015-10-23T11:54:37.484Z"/><meta itemprop="keywords" content="serviceworker,offline-first"/><meta itemprop="discussionUrl" content="https://ponyfoo.com/articles/serviceworker-revolution#comments"/><meta itemprop="interactionCount" content="9 UserComments"/><section itemprop="articleBody" class="at-corpus"><section itemprop="about" class="md-markdown at-introduction"><p>Sitting between humans and websites, ServiceWorker puts you in the driver&#x2019;s seat. When a ServiceWorker is installed, you&#x2019;ll be able to deliver responses to requests from the service worker <em>(which lies on the client-side)</em>, without necessarily hitting the network.</p><p>ServiceWorker is the evolution of the AppCache manifest. AppCache has been bashed for years because of several issues &#x2013; that were <a href="http://alistapart.com/article/application-cache-is-a-douchebag" aria-label="Application Cache is a Douchebag - alistapart.com">extensively documented</a> &#x2013; in a resonating <em>&#x201C;AppCache sucks&#x201D;</em> roar across the blogosphere. Most of the issues in AppCache revolved around it being a simple-looking declarative interface to offline behavior that, behind the curtains, had <strong>a complicated ruleset that wasn&#x2019;t simple nor intuitive.</strong></p><blockquote><p>The ApplicationCache spec is like an onion: it has many many layers, and as you peel through them you&#x2019;ll be reduced to tears.<br><em>&#x2013; <a href="https://twitter.com/jaffathecake" aria-label="@jaffathecake on Twitter">Jake Archibald</a></em></p></blockquote><p>On the other hand, ServiceWorker offers a programmatic API that allows you to accomplish so much more than AppCache ever could. Instead of almost-too-magical rules around a simplistic declarative syntax, ServiceWorker relies on plain JavaScript code.</p><p>Even though it doesn&#x2019;t have perfect <a href="https://jakearchibald.github.io/isserviceworkerready/" aria-label="Is ServiceWorker Ready?">browser support today</a>, ServiceWorker is just the beginning. Once you implement ServiceWorker in your sites, you&#x2019;ll be able to provide offline functionality for your pages. Yes, even when the human doesn&#x2019;t have any network connectivity at all, <strong>they&#x2019;ll be able to access your site while offline</strong>, provided that they&#x2019;ve visited at least once before. And that&#x2019;s <em>just the basic offering</em>. ServiceWorker also enables you to send <strong>Push Notifications</strong>, just like mobile apps do, and even when the website is running in the background; you also get to use <strong>Background Sync</strong>, allowing you to execute one-time or periodic updates of the content in your site while it&#x2019;s backgrounded; and <strong>Add to Home Screen</strong>, which does exactly what it sounds like, making your website feel almost like a native app.</p></section><section class="md-markdown at-body"><p><img alt="A picture of clocks" data-src="https://i.imgur.com/SMFZGJ6.jpg" class="js-only"><noscript><img src="https://i.imgur.com/SMFZGJ6.jpg" alt="A picture of clocks"></noscript></p><p>I&#x2019;ve spent some time playing around with the ServiceWorker API and felt compelled to write about it. While brand new, this API is a web platform powerhouse that we must become acquainted with if we want to have a fighting chance when it comes to mobile devices. Furthermore, ServiceWorker is great for performance besides its offline capabilities, as it grants fine-grained control over caching. Remember those pesky <em>&#x201C;this gravatar isn&#x2019;t cached for that long&#x201D;</em> messages on PageSpeed? You can use ServiceWorker as an intermediary cache that caches them for far longer!</p><p>If you have a personal website, blog, or small site you use to toy around with, I encourage you to make your way through this guide by trying to implement ServiceWorker on your own site. I did that with <a href="/" aria-label="You&apos;re looking at it, dummy!"><code class="md-code md-code-inline">ponyfoo.com</code></a> and besides being a great thought exercise it feels great to know that now we can access these articles while offline, too.</p><h1 id="getting-started">Getting Started</h1><p>Before getting started, we should go over a few notes on ServiceWorker.</p><ul><li>ServiceWorker doesn&#x2019;t have DOM access &#x2013; it&#x2019;s a worker that runs outside of the scope of any one page</li><li>ServiceWorker relies heavily on promises, you&#x2019;ll have to feel comfortable with them <a href="/articles/es6-promises-in-depth" aria-label="ES6 Promises in Depth on Pony Foo"><em>(read the guide)</em></a></li><li><a href="http://www.html5rocks.com/en/tutorials/service-worker/introduction/#toc-before" aria-label="Introduction to Service Worker on html5rocks.com"><code class="md-code md-code-inline">https</code> is required</a> for deployed sites to leverage ServiceWorker, although you can test on <code class="md-code md-code-inline">http://localhost</code> just fine</li></ul><p>ServiceWorker is a powerful addition to the web platform, having the ability to intercept all requests originating from a site &#x2013; including those made against other origins <em>(e.g: <code class="md-code md-code-inline">i.imgur.com</code>)</em>. For that reason, the <code class="md-code md-code-inline">https</code> requirement is a given for security concerns.</p><p>The quintessential ServiceWorker installation example is in the code snippet below. This is what I&#x2019;ve used in <a href="https://github.com/ponyfoo/ponyfoo/blob/e5052ab1545d929f192f6fd7fdbf3e44e10c8eae/client/js/main.js#L40-L42" aria-label="ServiceWorker registration for ponyfoo on GitHub"><code class="md-code md-code-inline">ponyfoo/ponyfoo</code></a> as well, and it shows how ServiceWorker is easily a progressive enhancement, as this is all the code in the main client-side codebase that references it. The feature test ensures we don&#x2019;t break older browsers, and the rest of the ServiceWorker-enabled code will live in the <code class="md-code md-code-inline">service-worker.js</code> script.</p><pre class="md-code-block"><code class="md-code md-lang-javascript"><span class="md-code-keyword">if</span> (<mark class="md-mark md-code-mark"><span class="md-code-string">&apos;serviceWorker&apos;</span> <span class="md-code-keyword">in</span> navigator</mark>) {
  447. navigator.serviceWorker.register(<span class="md-code-string">&apos;/service-worker.js&apos;</span>);
  448. }
  449. </code></pre><p>It should be noted that a ServiceWorker is scoped according to the endpoint we use to register the worker. If we just use <code class="md-code md-code-inline">/service-worker.js</code> then the scope is the entire origin, but if you register a ServiceWorker with an endpoint like <code class="md-code md-code-inline">/js/service-worker.js</code>, it&#x2019;ll only be able to intercept requests on your origin scoped to <code class="md-code md-code-inline">/js/</code>, such as <code class="md-code md-code-inline">/js/all.js</code> &#x2013; not at all useful.</p><p>Once a ServiceWorker is registered, it&#x2019;ll be downloaded and executed. After that, the first step in its lifecycle will be to fire the <code class="md-code md-code-inline">install</code> event. You can register event listeners that will fire at this time in your <code class="md-code md-code-inline">service-worker.js</code> file.</p><p>The example below is a simplified version of how I install the ServiceWorker for Pony Foo. The <code class="md-code md-code-inline">event.waitUntil</code> method takes a promise and then waits for it to be settled. If the promise fulfills, the ServiceWorker becomes installed, and otherwise the installation fails. ServiceWorker has access to the <code class="md-code md-code-inline">caches</code> API that can be used as an intermediate cache that lives on the client-side.<br>As you can observe in the code below, the <code class="md-code md-code-inline">caches</code> API is heavily <code class="md-code md-code-inline">Promise</code>-based as well. After opening the <code class="md-code md-code-inline">v1::fundamentals</code> cache we use <code class="md-code md-code-inline">cache.addAll</code> to store <code class="md-code md-code-inline">GET</code> responses for each of the <code class="md-code md-code-inline">offlineFundamentals</code> resources in said <code class="md-code md-code-inline">cache</code>.</p><pre class="md-code-block"><code class="md-code md-lang-javascript"><span class="md-code-keyword">var</span> offlineFundamentals = [
  450. <span class="md-code-string">&apos;/&apos;</span>,
  451. <span class="md-code-string">&apos;/offline&apos;</span>,
  452. <span class="md-code-string">&apos;/css/all.css&apos;</span>,
  453. <span class="md-code-string">&apos;/js/all.js&apos;</span>
  454. ];
  455. self.addEventListener(<mark class="md-mark md-code-mark"><span class="md-code-string">&apos;install&apos;</span></mark>, <span class="md-code-function"><span class="md-code-keyword">function</span> <span class="md-code-title">installer</span> <span class="md-code-params">(event)</span> </span>{
  456. <mark class="md-mark md-code-mark">event.waitUntil</mark>(
  457. caches
  458. .open(<span class="md-code-string">&apos;v1::fundamentals&apos;</span>)
  459. .then(<span class="md-code-function"><span class="md-code-keyword">function</span> <span class="md-code-title">prefill</span> <span class="md-code-params">(cache)</span> </span>{
  460. <mark class="md-mark md-code-mark">cache.addAll</mark>(offlineFundamentals);
  461. })
  462. );
  463. });
  464. </code></pre><p>Why am I caching those particular resources? Because visitors will be able to visit the home page of my site while offline. Additionally, the <code class="md-code md-code-inline">/offline</code> resource can be used as a default offline response for <code class="md-code md-code-inline">Content-Type: application/html</code> requests made against endpoints in my origin <em>(e.g: <a href="/articles/history" aria-label="Full Publication History on Pony Foo"><code class="md-code md-code-inline">ponyfoo.com/articles/history</code></a> while offline)</em>, as we&#x2019;ll see later on.</p><blockquote><p>You can inspect the ServiceWorker lifecycle on DevTools. It&#x2019;ll make your life much easier while debugging! Just go to the Resources tab and choose the last option in there &#x2013; Service Workers. Available starting in Chrome 48 <em>(already in Chrome Canary)</em>.</p><p><img alt="ServiceWorker lifecycle on DevTools" data-src="https://i.imgur.com/VqGQPFY.png" class="js-only"><noscript><img src="https://i.imgur.com/VqGQPFY.png" alt="ServiceWorker lifecycle on DevTools"></noscript></p></blockquote><p>Once those resources are successfully cached, the ServiceWorker becomes <code class="md-code md-code-inline">installed</code>.</p><h1 id="serviceworker-lifecycle">ServiceWorker Lifecycle</h1><p>In addition to <code class="md-code md-code-inline">installed</code> &#x2013; and <code class="md-code md-code-inline">installing</code> while waiting on the cache to be filled up &#x2013; there&#x2019;s also an activation step during which an older ServiceWorker becomes <code class="md-code md-code-inline">redundant</code> while the newer one replaces it and becomes <code class="md-code md-code-inline">activated</code> <em>(until then, the newer ServiceWorker is <code class="md-code md-code-inline">activating</code>)</em>. There&#x2019;s <a href="http://slightlyoff.github.io/ServiceWorker/spec/service_worker/#service-worker-state-enum" aria-label="ServiceWorkerState enum in the ServiceWorker Specification">five possible states</a> in the ServiceWorker lifecycle.</p><ul><li><code class="md-code md-code-inline">installing</code> while blocked on <code class="md-code md-code-inline">event.waitUntil</code> promises during the <code class="md-code md-code-inline">install</code> event</li><li><code class="md-code md-code-inline">installed</code> while waiting to become active</li><li><code class="md-code md-code-inline">activating</code> while blocked on <code class="md-code md-code-inline">event.waitUntil</code> promises during the <code class="md-code md-code-inline">activate</code> event</li><li><code class="md-code md-code-inline">activated</code> when completely operational and able to intercept <code class="md-code md-code-inline">fetch</code> requests</li><li><code class="md-code md-code-inline">redundant</code> when being replaced by a newer ServiceWorker script version, or being discarded due to a failed <code class="md-code md-code-inline">install</code></li></ul><p>After a ServiceWorker is <code class="md-code md-code-inline">installed</code>, the <code class="md-code md-code-inline">activate</code> event fires, and the exact same drill is followed. During the activation step, you could clear up room in the <code class="md-code md-code-inline">caches</code>. Remember how I prefixed my cache as <code class="md-code md-code-inline">v1::</code> earlier? If I updated the fundamental files in my cache I could just <code class="md-code md-code-inline">.delete</code> the older cache and bump the version number, as seen below.</p><pre class="md-code-block"><code class="md-code md-lang-javascript"><span class="md-code-keyword">var</span> version = <span class="md-code-string">&apos;v2::&apos;</span>;
  465. self.addEventListener(<mark class="md-mark md-code-mark"><span class="md-code-string">&apos;activate&apos;</span></mark>, <span class="md-code-function"><span class="md-code-keyword">function</span> <span class="md-code-title">activator</span> <span class="md-code-params">(event)</span> </span>{
  466. event.waitUntil(
  467. caches<mark class="md-mark md-code-mark">.keys()</mark>.then(<span class="md-code-function"><span class="md-code-keyword">function</span> <span class="md-code-params">(keys)</span> </span>{
  468. <span class="md-code-keyword">return</span> <mark class="md-mark md-code-mark">Promise.all</mark>(keys
  469. .filter(<span class="md-code-function"><span class="md-code-keyword">function</span> <span class="md-code-params">(key)</span> </span>{
  470. <span class="md-code-keyword">return</span> key.indexOf(version) !== <span class="md-code-number">0</span>;
  471. })
  472. .map(<span class="md-code-function"><span class="md-code-keyword">function</span> <span class="md-code-params">(key)</span> </span>{
  473. <span class="md-code-keyword">return</span> <mark class="md-mark md-code-mark">caches.delete(key)</mark>;
  474. })
  475. );
  476. })
  477. );
  478. });
  479. </code></pre><p>Now that you have an <code class="md-code md-code-inline">activated</code> ServiceWorker, you can begin intercepting requests.</p><h1 id="intercepting-requests-in-serviceworker">Intercepting Requests in ServiceWorker</h1><p>Whenever a network request would be initiated and a ServiceWorker is activated, a <code class="md-code md-code-inline">fetch</code> event is raised on that ServiceWorker instead. Event handlers for the <code class="md-code md-code-inline">fetch</code> event are expected to produce a response for the request, and they may or may not access the network.</p><p>In its simplest form, your ServiceWorker could just act as a network passthrough. In this case, the application would seldom be any different than without a ServiceWorker. Note that, by default, <code class="md-code md-code-inline">fetch</code> doesn&#x2019;t include credentials such as cookies and fails to make requests to third parties that don&#x2019;t support CORS.</p><pre class="md-code-block"><code class="md-code md-lang-javascript">self.addEventListener(<span class="md-code-string">&apos;fetch&apos;</span>, <span class="md-code-function"><span class="md-code-keyword">function</span> <span class="md-code-title">fetcher</span> <span class="md-code-params">(event)</span> </span>{
  480. event.respondWith(fetch(event.request));
  481. });
  482. </code></pre><p>Typically, you don&#x2019;t want to cache non-<code class="md-code md-code-inline">GET</code> responses, so those should probably be filtered out. The code below will default to a networked response for every <code class="md-code md-code-inline">POST</code>, <code class="md-code md-code-inline">PUT</code>, <code class="md-code md-code-inline">PATCH</code>, <code class="md-code md-code-inline">HEAD</code>, or <code class="md-code md-code-inline">OPTIONS</code> originating from the ServiceWorker&#x2019;s scope.</p><pre class="md-code-block"><code class="md-code md-lang-javascript">self.addEventListener(<span class="md-code-string">&apos;fetch&apos;</span>, <span class="md-code-function"><span class="md-code-keyword">function</span> <span class="md-code-title">fetcher</span> <span class="md-code-params">(event)</span> </span>{
  483. <span class="md-code-keyword">var</span> request = event.request;
  484. <span class="md-code-keyword">if</span> (request.method !== <span class="md-code-string">&apos;GET&apos;</span>) {
  485. event.respondWith(fetch(request)); <span class="md-code-keyword">return</span>;
  486. }
  487. <span class="md-code-comment">// handle other requests</span>
  488. });
  489. </code></pre><p>If you&#x2019;re looking for recipes to handle requests, the <a href="https://jakearchibald.com/2014/offline-cookbook/" aria-label="The offline cookbook - jakearchibald.com">offline cookbook</a> is a great place to look.</p><h1 id="strategies">Strategies</h1><p>There are several different strategies you can apply to resolving requests in your ServiceWorker. Here are the ones I&#x2019;ve found the most interesting.</p><h1 id="network-then-cached">Network then Cached</h1><blockquote><p>Fetch from the network first, and fall back to a cached response if <code class="md-code md-code-inline">fetch</code> fails.</p></blockquote><p>You don&#x2019;t get to leverage the caching capabilities of ServiceWorker when you&#x2019;re online with this strategy, because the network always comes first. For that reason, <code class="md-code md-code-inline">fetch</code>-first also has the drawback that an intermittent, unreliable, or very slow network connection will never produce responses, even though it may have a perfectly usable cache going to waste.</p><ul><li>Always hit the network for non-<code class="md-code md-code-inline">GET</code> requests</li><li>Hit the network<ul><li>If network request succeeds, use its <code class="md-code md-code-inline">response</code> and store it in the cache</li><li>If network request fails, try <code class="md-code md-code-inline">caches.match(request)</code><ul><li>If there&#x2019;s a cache hit, use that as the <code class="md-code md-code-inline">response</code></li><li>If there&#x2019;s no cache hit, attempt to fall back to <code class="md-code md-code-inline">/offline</code></li></ul></li></ul></li></ul><p>This flow is better at producing fresh content <em>(as opposed to stale cached responses)</em> than others, but isn&#x2019;t all that useful beyond entirely-offline <em>(as opposed to <strong>effectively-offline</strong> due to low connectivity)</em> improvements. Mobile devices won&#x2019;t take full advantage of this strategy because their connectivity might be very low but not low enough for the device to turn <code class="md-code md-code-inline">navigator.online</code> off, and so you end up in the same place as with no ServiceWorker.</p><h2 id="cached-then-network">Cached then Network</h2><blockquote><p>Look for a cached response first, but always fetch from the network regardless of cache state.</p></blockquote><p>This flow is similar to the previous one, except you go to the cache first. Here responses may be immediate and you see performance improvements across visits to any content that was previously cached.</p><ul><li>Always hit the network for non-<code class="md-code md-code-inline">GET</code> requests</li><li>Check <code class="md-code md-code-inline">caches.match(request)</code> to see if there&#x2019;s a cache hit</li><li>Hit the network, regardless of cache hits<ul><li>If network request succeeds, cache its response</li><li>If network request fails, attempt to fall back to <code class="md-code md-code-inline">/offline</code></li></ul></li><li>Return <code class="md-code md-code-inline">cached</code> response in case of cache hit, <code class="md-code md-code-inline">fetch</code> response otherwise</li></ul><p>Eventually, the cache becomes fresh again, because <code class="md-code md-code-inline">fetch</code> is always used &#x2013; regardless of cache hits.</p><p>The problem in that case is that the cache may be stale. Suppose you visit a page once. The worker uses <code class="md-code md-code-inline">fetch</code> and then the response is cached. When you visit the page a second time, you get the cached response from the last time immediately, and then a <code class="md-code md-code-inline">fetch</code> is executed that pulls the latest version into the cache. At that point you&#x2019;ve already applied the previous version, which isn&#x2019;t the latest content.</p><h2 id="cached-then-network-and-postmessage">Cached then Network and <code class="md-code md-code-inline">postMessage</code></h2><p>The previous flow could serve stale content, but you could update the UI when the updated response comes in. I haven&#x2019;t experimented with <a href="https://googlechrome.github.io/samples/service-worker/post-message/" aria-label="Service Worker postMessage() Sample"><code class="md-code md-code-inline">postMessage</code></a> at this point, so this is mostly a thought exercise. The <code class="md-code md-code-inline">postMessage</code> interface can be used to transmit messages between the worker and the browser tabs under its control.</p><p>Using the same flow as described in <a href="#cached-then-network">Cached then Network</a>, you could add messaging between the worker and the app so that when the cache is updated, any tabs that are on the same page as the updated cache endpoint get updated. Of course, the interaction should be carefully crafted. A combination of virtual DOM diffing and careful planning when dealing with cached resources other than HTML pages would go a long way towards making these updates seamless.</p><p>That being said, this approach is probably a bit too sophisticated for most applications. As usual, it all depends on your use case anyways.</p><h1 id="implementation">Implementation</h1><p>On this blog I went for the <a href="#cached-then-network">&#x201C;Cached then Network&#x201D;</a> approach. This is the code we had so far. It helped us bail on non-<code class="md-code md-code-inline">GET</code> requests.</p><pre class="md-code-block"><code class="md-code md-lang-javascript">self.addEventListener(<span class="md-code-string">&apos;fetch&apos;</span>, <span class="md-code-function"><span class="md-code-keyword">function</span> <span class="md-code-title">fetcher</span> <span class="md-code-params">(event)</span> </span>{
  490. <span class="md-code-keyword">var</span> request = event.request;
  491. <span class="md-code-keyword">if</span> (request.method !== <span class="md-code-string">&apos;GET&apos;</span>) {
  492. event.respondWith(fetch(request)); <span class="md-code-keyword">return</span>;
  493. }
  494. <span class="md-code-comment">// handle other requests</span>
  495. });
  496. </code></pre><p>After that, We can look for cache hits with <code class="md-code md-code-inline">caches.match(request)</code> and then respond with the result from a <code class="md-code md-code-inline">queriedCache</code> callback.</p><pre class="md-code-block"><code class="md-code md-lang-javascript">event<mark class="md-mark md-code-mark">.respondWith</mark>(caches
  497. <mark class="md-mark md-code-mark">.match</mark>(request)
  498. <mark class="md-mark md-code-mark">.then(queriedCache)</mark>
  499. );
  500. </code></pre><p>The <code class="md-code md-code-inline">queriedCache</code> method receives the <code class="md-code md-code-inline">cached</code> response, if any. It then makes a <code class="md-code md-code-inline">fetch</code> request regardless of the cache getting a hit. We also try to fall back gracefully when either fetch or caching fail, with an <code class="md-code md-code-inline">unableToResolve</code> callback. Lastly, we return the <code class="md-code md-code-inline">cached</code> response and fall back to the <code class="md-code md-code-inline">networked</code> promise if we miss the cache.</p><pre class="md-code-block"><code class="md-code md-lang-javascript"><span class="md-code-function"><span class="md-code-keyword">function</span> <span class="md-code-title">queriedCache</span> <span class="md-code-params">(cached)</span> </span>{
  501. <span class="md-code-keyword">var</span> networked = <mark class="md-mark md-code-mark">fetch(request)</mark>
  502. .then(fetchedFromNetwork, unableToResolve)
  503. .catch(unableToResolve);
  504. <span class="md-code-keyword">return</span> cached || networked;
  505. }
  506. </code></pre><p>When <code class="md-code md-code-inline">fetch</code> succeeds and <code class="md-code md-code-inline">fetchedFromNetwork</code> is called, we store a copy of the response in the <code class="md-code md-code-inline">cache</code> and then we return the <code class="md-code md-code-inline">response</code> unchanged.</p><pre class="md-code-block"><code class="md-code md-lang-javascript"><span class="md-code-function"><span class="md-code-keyword">function</span> <span class="md-code-title">fetchedFromNetwork</span> <span class="md-code-params">(response)</span> </span>{
  507. <span class="md-code-keyword">var</span> clonedResponse = response<mark class="md-mark md-code-mark">.clone()</mark>;
  508. caches.open(version + <span class="md-code-string">&apos;pages&apos;</span>).then(<span class="md-code-function"><span class="md-code-keyword">function</span> <span class="md-code-title">add</span> <span class="md-code-params">(cache)</span> </span>{
  509. <mark class="md-mark md-code-mark">cache.put(request, clonedResponse)</mark>;
  510. });
  511. <span class="md-code-keyword">return</span> response;
  512. }
  513. </code></pre><p>When we&#x2019;re unable to resolve <code class="md-code md-code-inline">fetch</code> requests we have to return a fallback. By default, we can make that an opaque <code class="md-code md-code-inline">offlineResponse</code>. As you can see, you can hardcode <code class="md-code md-code-inline">Response</code> objects and use those to react to network requests.</p><pre class="md-code-block"><code class="md-code md-lang-javascript"><span class="md-code-function"><span class="md-code-keyword">function</span> <span class="md-code-title">unableToResolve</span> <span class="md-code-params">()</span> </span>{
  514. <span class="md-code-keyword">return</span> offlineResponse();
  515. }
  516. <span class="md-code-function"><span class="md-code-keyword">function</span> <span class="md-code-title">offlineResponse</span> <span class="md-code-params">()</span> </span>{
  517. <span class="md-code-keyword">return</span> <mark class="md-mark md-code-mark">new Response(<span class="md-code-string">&apos;&apos;</span>, { status: <span class="md-code-number">503</span>, statusText: <span class="md-code-string">&apos;Service Unavailable&apos;</span> })</mark>;
  518. }
  519. </code></pre><p>If we were dealing with an image, we could return some placeholder rainbows image instead &#x2013; provided that <code class="md-code md-code-inline">rainbows</code> is a URL string that was already cached during the ServiceWorker installation step.</p><pre class="md-code-block"><code class="md-code md-lang-javascript"><span class="md-code-function"><span class="md-code-keyword">function</span> <span class="md-code-title">unableToResolve</span> <span class="md-code-params">()</span> </span>{
  520. <span class="md-code-keyword">var</span> accepts = request.headers.get(<span class="md-code-string">&apos;Accept&apos;</span>);
  521. <span class="md-code-keyword">if</span> (<mark class="md-mark md-code-mark">accepts.indexOf(<span class="md-code-string">&apos;image&apos;</span>) !== -<span class="md-code-number">1</span></mark>) {
  522. <span class="md-code-keyword">return</span> caches.match(rainbows);
  523. }
  524. <span class="md-code-keyword">return</span> offlineResponse();
  525. }
  526. </code></pre><p>Furthermore, if its a gravatar, we could use a tailored <code class="md-code md-code-inline">mysteryMan</code> image for that, also cached during installation.</p><pre class="md-code-block"><code class="md-code md-lang-javascript"><span class="md-code-function"><span class="md-code-keyword">function</span> <span class="md-code-title">unableToResolve</span> <span class="md-code-params">()</span> </span>{
  527. <span class="md-code-keyword">var</span> url = <span class="md-code-keyword">new</span> URL(request.url);
  528. <span class="md-code-keyword">var</span> accepts = request.headers.get(<span class="md-code-string">&apos;Accept&apos;</span>);
  529. <span class="md-code-keyword">if</span> (accepts.indexOf(<span class="md-code-string">&apos;image&apos;</span>) !== -<span class="md-code-number">1</span>) {
  530. <span class="md-code-keyword">if</span> (<mark class="md-mark md-code-mark">url.host === <span class="md-code-string">&apos;www.gravatar.com&apos;</span></mark>) {
  531. <span class="md-code-keyword">return</span> caches.match(mysteryMan);
  532. }
  533. <span class="md-code-keyword">return</span> caches.match(rainbows);
  534. }
  535. <span class="md-code-keyword">return</span> offlineResponse();
  536. }
  537. </code></pre><p>Similarly, if it&#x2019;s a request that accepts HTML, we could return the <code class="md-code md-code-inline">/offline</code> view we had installed earlier.</p><pre class="md-code-block"><code class="md-code md-lang-javascript"><span class="md-code-function"><span class="md-code-keyword">function</span> <span class="md-code-title">unableToResolve</span> <span class="md-code-params">()</span> </span>{
  538. <span class="md-code-keyword">var</span> url = <span class="md-code-keyword">new</span> URL(request.url);
  539. <span class="md-code-keyword">var</span> accepts = request.headers.get(<span class="md-code-string">&apos;Accept&apos;</span>);
  540. <span class="md-code-keyword">if</span> (accepts.indexOf(<span class="md-code-string">&apos;image&apos;</span>) !== -<span class="md-code-number">1</span>) {
  541. <span class="md-code-keyword">if</span> (url.host === <span class="md-code-string">&apos;www.gravatar.com&apos;</span>) {
  542. <span class="md-code-keyword">return</span> caches.match(mysteryMan);
  543. }
  544. <span class="md-code-keyword">return</span> caches.match(rainbows);
  545. }
  546. <span class="md-code-keyword">if</span> (url.origin === location.origin) {
  547. <span class="md-code-keyword">return</span> <mark class="md-mark md-code-mark">caches.match(<span class="md-code-string">&apos;/offline&apos;</span>)</mark>;
  548. }
  549. <span class="md-code-keyword">return</span> offlineResponse();
  550. }
  551. </code></pre><p>Lastly, since Pony Foo is a single page application, the ServiceWorker also needs to understand how to render an offline view using JSON. In that case we&#x2019;ll check that the <code class="md-code md-code-inline">origin</code> matches mine and that the headers are expecting <code class="md-code md-code-inline">application/json</code>. I can then construct a response that Taunus will interpret as the <code class="md-code md-code-inline">/offline</code> view.</p><pre class="md-code-block"><code class="md-code md-lang-javascript"><span class="md-code-keyword">if</span> (url.origin === location.origin &amp;&amp; accepts.indexOf(<span class="md-code-string">&apos;application/json&apos;</span>) !== -<span class="md-code-number">1</span>) {
  552. <span class="md-code-keyword">return</span> offlineView();
  553. }
  554. <span class="md-code-function"><span class="md-code-keyword">function</span> <span class="md-code-title">offlineView</span> <span class="md-code-params">()</span> </span>{
  555. <span class="md-code-keyword">var</span> viewModel = {
  556. model: { action: <span class="md-code-string">&apos;error/offline&apos;</span> }
  557. };
  558. <span class="md-code-keyword">var</span> options = {
  559. status: <span class="md-code-number">200</span>,
  560. headers: <span class="md-code-keyword">new</span> Headers({ <span class="md-code-string">&apos;content-type&apos;</span>: <span class="md-code-string">&apos;application/json&apos;</span> })
  561. };
  562. <span class="md-code-keyword">return</span> <span class="md-code-keyword">new</span> Response(<span class="md-code-built_in">JSON</span>.stringify(viewModel), options);
  563. }
  564. </code></pre><p>There&#x2019;s plenty of other options. In content sites you could go as far as to autogenerate a ServiceWorker file that has all of the content inlined in it <em>(or maybe in a big payload that&#x2019;s cached during installation)</em>. Or you could progressively crawl the site from the ServiceWorker using <code class="md-code md-code-inline">requestIdleCallback</code>. Or you could just cache things that humans actually visited. Most of the time, that&#x2019;s good enough.</p><p>As long as I&#x2019;m able to visit content I&#x2019;ve already seen, but offline, I&#x2019;m glad to have implemented ServiceWorker. The screenshot below shows <a href="http://WebPageTest.org">WebPageTest.org</a> results on repeat view where no requests are made whatsoever, shaving <code class="md-code md-code-inline">~200ms</code> from start render time and around <code class="md-code md-code-inline">~2.5s</code> from complete page load. Down <strong>from 43 requests to zero</strong>, and from <code class="md-code md-code-inline">~2mb</code> in page weight to <code class="md-code md-code-inline">~200kb</code>.</p><p><img alt="WebPageTest.org results on repeat view" data-src="https://i.imgur.com/lCH6mGU.png" class="js-only"><noscript><img src="https://i.imgur.com/lCH6mGU.png" alt="WebPageTest.org results on repeat view"></noscript></p><p>Definitely a worthwhile addition to any website.</p></section></section>
  565. </article>
  566. </section>
  567. <nav id="jumpto">
  568. <p>
  569. <a href="/david/blog/">Accueil du blog</a> |
  570. <a href="https://ponyfoo.com/articles/serviceworker-revolution">Source originale</a> |
  571. <a href="/david/stream/2019/">Accueil du flux</a>
  572. </p>
  573. </nav>
  574. <footer>
  575. <div>
  576. <img src="/static/david/david-larlet-avatar.jpg" loading="lazy" class="avatar" width="200" height="200">
  577. <p>
  578. Bonjour/Hi!
  579. Je suis <a href="/david/" title="Profil public">David&nbsp;Larlet</a>, je vis actuellement à Montréal et j’alimente cet espace depuis 15 ans. <br>
  580. Si tu as apprécié cette lecture, n’hésite pas à poursuivre ton exploration. Par exemple via les <a href="/david/blog/" title="Expériences bienveillantes">réflexions bimestrielles</a>, la <a href="/david/stream/2019/" title="Pensées (dés)articulées">veille hebdomadaire</a> ou en t’abonnant au <a href="/david/log/" title="S’abonner aux publications via RSS">flux RSS</a> (<a href="/david/blog/2019/flux-rss/" title="Tiens c’est quoi un flux RSS ?">so 2005</a>).
  581. </p>
  582. <p>
  583. Je m’intéresse à la place que je peux avoir dans ce monde. En tant qu’humain, en tant que membre d’une famille et en tant qu’associé d’une coopérative. De temps en temps, je fais aussi des <a href="https://github.com/davidbgk" title="Principalement sur Github mais aussi ailleurs">trucs techniques</a>. Et encore plus rarement, <a href="/david/talks/" title="En ce moment je laisse plutôt la place aux autres">j’en parle</a>.
  584. </p>
  585. <p>
  586. Voici quelques articles choisis :
  587. <a href="/david/blog/2019/faire-equipe/" title="Accéder à l’article complet">Faire équipe</a>,
  588. <a href="/david/blog/2018/bivouac-automnal/" title="Accéder à l’article complet">Bivouac automnal</a>,
  589. <a href="/david/blog/2018/commodite-effondrement/" title="Accéder à l’article complet">Commodité et effondrement</a>,
  590. <a href="/david/blog/2017/donnees-communs/" title="Accéder à l’article complet">Des données aux communs</a>,
  591. <a href="/david/blog/2016/accompagner-enfant/" title="Accéder à l’article complet">Accompagner un enfant</a>,
  592. <a href="/david/blog/2016/senior-developer/" title="Accéder à l’article complet">Senior developer</a>,
  593. <a href="/david/blog/2016/illusion-sociale/" title="Accéder à l’article complet">L’illusion sociale</a>,
  594. <a href="/david/blog/2016/instantane-scopyleft/" title="Accéder à l’article complet">Instantané Scopyleft</a>,
  595. <a href="/david/blog/2016/enseigner-web/" title="Accéder à l’article complet">Enseigner le Web</a>,
  596. <a href="/david/blog/2016/simplicite-defaut/" title="Accéder à l’article complet">Simplicité par défaut</a>,
  597. <a href="/david/blog/2016/minimalisme-esthetique/" title="Accéder à l’article complet">Minimalisme et esthétique</a>,
  598. <a href="/david/blog/2014/un-web-omni-present/" title="Accéder à l’article complet">Un web omni-présent</a>,
  599. <a href="/david/blog/2014/manifeste-developpeur/" title="Accéder à l’article complet">Manifeste de développeur</a>,
  600. <a href="/david/blog/2013/confort-convivialite/" title="Accéder à l’article complet">Confort et convivialité</a>,
  601. <a href="/david/blog/2013/testament-numerique/" title="Accéder à l’article complet">Testament numérique</a>,
  602. et <a href="/david/blog/" title="Accéder aux archives">bien d’autres…</a>
  603. </p>
  604. <p>
  605. On peut <a href="mailto:david%40larlet.fr" title="Envoyer un courriel">échanger par courriel</a>. Si éventuellement tu souhaites que l’on travaille ensemble, tu devrais commencer par consulter le <a href="http://larlet.com">profil dédié à mon activité professionnelle</a> et/ou contacter directement <a href="http://scopyleft.fr/">scopyleft</a>, la <abbr title="Société coopérative et participative">SCOP</abbr> dont je fais partie depuis six ans. Je recommande au préalable de lire <a href="/david/blog/2018/cout-site/" title="Attention ce qui va suivre peut vous choquer">combien coûte un site</a> et pourquoi je suis plutôt favorable à une <a href="/david/pro/devis/" title="Discutons-en !">non-demande de devis</a>.
  606. </p>
  607. <p>
  608. Je ne traque pas ta navigation mais mon
  609. <abbr title="Alwaysdata, 62 rue Tiquetonne 75002 Paris, +33.184162340">hébergeur</abbr>
  610. conserve des logs d’accès.
  611. </p>
  612. </div>
  613. </footer>
  614. <script type="text/javascript">
  615. ;(_ => {
  616. const jumper = document.getElementById('jumper')
  617. jumper.addEventListener('click', e => {
  618. e.preventDefault()
  619. const anchor = e.target.getAttribute('href')
  620. const targetEl = document.getElementById(anchor.substring(1))
  621. targetEl.scrollIntoView({behavior: 'smooth'})
  622. })
  623. })()
  624. </script>