<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Devv Romano</title>
    <description>The latest articles on DEV Community by Devv Romano (@devv-romano).</description>
    <link>https://dev.to/devv-romano</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F175754%2F7f504709-0a86-492e-b44b-1aaed32be79a.png</url>
      <title>DEV Community: Devv Romano</title>
      <link>https://dev.to/devv-romano</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devv-romano"/>
    <language>en</language>
    <item>
      <title>How to Scope Your Free Range PWA Service Workers</title>
      <dc:creator>Devv Romano</dc:creator>
      <pubDate>Thu, 12 Dec 2019 17:03:09 +0000</pubDate>
      <link>https://dev.to/devv-romano/how-to-scope-your-pwa-service-workers-1n6m</link>
      <guid>https://dev.to/devv-romano/how-to-scope-your-pwa-service-workers-1n6m</guid>
      <description>&lt;p&gt;&lt;a href="https://developers.google.com/web/progressive-web-apps" rel="noopener noreferrer"&gt;Progressive Web Apps&lt;/a&gt; are quickly becoming an engaging method of writing online applications. In order to use them properly, however, a developer needs to satisfy &lt;a href="https://developers.google.com/web/fundamentals/web-app-manifest" rel="noopener noreferrer"&gt;certain criteria in a web manifest&lt;/a&gt; to have the browser install the app on a user's device through "Add to Home Screen".&lt;/p&gt;

&lt;p&gt;These criteria include adding to a web manifest:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;short_name&lt;/code&gt; / &lt;code&gt;name&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;icons&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;start_url&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;display&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There's a few catches, however. This article will focus on a particular one: &lt;code&gt;start_url&lt;/code&gt; and proper service worker &lt;code&gt;scope&lt;/code&gt; when your files aren't sitting pretty in the same directory. I will assume you are deep enough in the rabbit hole of developing a PWA that concepts like HTTPS, registering service workers, app shells, and caching will be familiar to you.&lt;/p&gt;

&lt;h1&gt;
  
  
  What is scope? 🔭
&lt;/h1&gt;

&lt;p&gt;While developing a PWA, you may have seen the warning in Chrome's DevTools under Application/Manifest: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Site cannot be installed: no matching service worker detected. You may need to reload the page, or check that the service worker for the current page also controls the start URL from the manifest&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you know your service worker is being served to the client, this error is likely due to an issue with your &lt;code&gt;scope&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;According to &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Manifest/scope" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;, &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The scope member [of a web manifest] is a string that defines the navigation scope of this web application's application context. It restricts what web pages can be viewed while the manifest is applied.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So why doesn't your service worker match?&lt;/p&gt;

&lt;h2&gt;
  
  
  Default scope
&lt;/h2&gt;

&lt;p&gt;By default, the &lt;code&gt;scope&lt;/code&gt; member will point to whichever directory the web manifest is located. If your manifest is at &lt;code&gt;/app/manifest.json&lt;/code&gt;, your scope is &lt;code&gt;/app/&lt;/code&gt; unless you declare the &lt;code&gt;scope&lt;/code&gt; member in your manifest. &lt;/p&gt;

&lt;h2&gt;
  
  
  Explicit scope
&lt;/h2&gt;

&lt;p&gt;If you don't explicitly declare your &lt;code&gt;scope&lt;/code&gt; to include the document at your &lt;code&gt;start_url&lt;/code&gt; &lt;strong&gt;and&lt;/strong&gt; the scope of your registered service worker, you are not meeting the "Add to Home Screen" criteria for PWAs, and the browser you're working with will probably annoy you about it. If your manifest is at &lt;code&gt;/app/manifest.json&lt;/code&gt;, but your service worker is at &lt;code&gt;/dist/service-worker.js&lt;/code&gt;, the scopes are &lt;code&gt;/app/&lt;/code&gt; and &lt;code&gt;/dist/&lt;/code&gt; respectively, and &lt;code&gt;/dist/&lt;/code&gt; is out of the scope of &lt;code&gt;/app/&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftuv9dr45ci9yz67xtdym.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftuv9dr45ci9yz67xtdym.gif" width="480" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is scope even relevant?
&lt;/h2&gt;

&lt;p&gt;Since PWAs are really just special browser windows on the device, they need specific instructions as to which paths exist within your app and which paths are outside the intended function of the app. In real terms, pages outside the scope will display a browser bar instead of whatever &lt;code&gt;display&lt;/code&gt; setting you have set.&lt;/p&gt;

&lt;h1&gt;
  
  
  Getting back in scope 👀
&lt;/h1&gt;

&lt;p&gt;Let's say you're like me and use a tool like webpack to bundle your assets into a &lt;code&gt;/dist/&lt;/code&gt; directory, but the path to your app is &lt;code&gt;/app&lt;/code&gt; which points to &lt;code&gt;/app/index.html&lt;/code&gt;. This is the same situation as above. We don't want to change the code structure just to satisfy the browser's demands, so what do we do?&lt;/p&gt;

&lt;p&gt;One option is to use a URL rewrite tool such as those in IIS to point all asset routes that would match &lt;code&gt;/app/{file}&lt;/code&gt; to &lt;code&gt;/dist/{file}&lt;/code&gt;. This can work as the browser will see all the assets under &lt;code&gt;/app/&lt;/code&gt; and stop complaining, but in my case it caused some unintended side effects with other apps that use the &lt;code&gt;/dist/&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;Another option is to put the manifest in the root of your website as it would then include all files in the site under the same "scope", but this could cause issues especially when the site itself isn't entirely meant to be a PWA.&lt;/p&gt;

&lt;p&gt;This &lt;a href="https://stackoverflow.com/a/48068714/8958801" rel="noopener noreferrer"&gt;helpful StackOverflow answer&lt;/a&gt; leads to the best answer, and the one recommended by the &lt;a href="https://w3c.github.io/ServiceWorker/#service-worker-script-response" rel="noopener noreferrer"&gt;w3c specifications&lt;/a&gt;. We should add a &lt;code&gt;Service-Worker-Allowed&lt;/code&gt; header to the service worker file's HTTP response to let the browser know the server is allowing this particular service worker script to use any scope it wants. In my case I used &lt;code&gt;Service-Worker-Allowed: /&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Importantly, we also need to explicitly state our scope even with the header set from the server. For good measure, our example manifest should include&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ... other members&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;start_url&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/app&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;scope&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/app&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and we should use the &lt;code&gt;scope&lt;/code&gt; parameter when we register the service worker:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nb"&gt;navigator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serviceWorker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/dist/app/sw.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,{&lt;/span&gt; &lt;span class="na"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/app&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We now have the ability to install the PWA with any route we'd like, as long as &lt;code&gt;start_url&lt;/code&gt;, &lt;code&gt;scope&lt;/code&gt;, and the service worker all agree on what that scope should be.&lt;/p&gt;

</description>
      <category>pwa</category>
      <category>todayilearned</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
