7

I'm trying to set up a PWA with Angular 6 and having trouble to make the service worker serve content offline. I tried a lot of configurations and stuff for a couple of days now but with no success.

The service worker seems to be registered and running lighthouse in audits tab in Chrome passes the pwa test.

Service Worker

PWA Scoring

PWA Test Details

I'm using Contentful to serve texts/assets for this site. Service worker is fetching the content from dataGroups but it returns a 204 code when the service worker is doing the fetching. I can only assume that this results in the cache storage being empty.

Network Tab

Cache Storage

But then again, I would expect the lighthouse test to fail, since it doesn't serve content offline. I think this happens because the page works offline as long as the session is not changed. Meaning if I turn off the network connection it will work when I navigate from a route to another route, but will fail with a 504 code if I reload/open a new tab/re-open the browser.

I'm hosting the Angular 6 app under local IIS.

index.html:

<base href="/servicemanager/">

web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

<system.webServer>
  <rewrite>
    <rules>
      <rule name="Angular Routes" stopProcessing="true">
        <match url=".*" />
        <conditions logicalGrouping="MatchAll">
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        </conditions>
        <action type="Rewrite" url="/servicemanager/index.html" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

</configuration>

ngsw-config.json:

"dataGroups": [
{
  "name": "contentful-api",
  "urls": [
    "https://cdn.contentful.com/**"
  ],
  "cacheConfig": {
    "maxSize": 20,
    "maxAge": "1h",
    "timeout": "5s",
    "strategy": "freshness"
  }
}

]

Rest of it like assetGroups is just default.

manifest.json:

    {
  "name": "Service Manager",
  "short_name": "ASM",
  "theme_color": "#03A753",
  "background_color": "#1b2a32",
  "display": "standalone",
  "scope": "/",
  "start_url": "/servicemanager/index.html",
  "icons": [
    {
      "src": "assets/icons/icon-72x72.png",
      "sizes": "72x72",
      "type": "image/png"
    },
    {
      "src": "assets/icons/icon-96x96.png",
      "sizes": "96x96",
      "type": "image/png"
    },
    {
      "src": "assets/icons/icon-128x128.png",
      "sizes": "128x128",
      "type": "image/png"
    },
    {
      "src": "assets/icons/icon-144x144.png",
      "sizes": "144x144",
      "type": "image/png"
    },
    {
      "src": "assets/icons/icon-152x152.png",
      "sizes": "152x152",
      "type": "image/png"
    },
    {
      "src": "assets/icons/icon-192x192.png",
      "sizes": "192x192",
      "type": "image/png"
    },
    {
      "src": "assets/icons/icon-384x384.png",
      "sizes": "384x384",
      "type": "image/png"
    },
    {
      "src": "assets/icons/icon-512x512.png",
      "sizes": "512x512",
      "type": "image/png"
    }
  ]
}

Generated ngsw.json:

    {
  "configVersion": 1,
  "index": "/index.html",
  "assetGroups": [
    {
      "name": "app",
      "installMode": "prefetch",
      "updateMode": "prefetch",
      "urls": [
        "/0.05ca3f943f64d55f5fa7.js",
        "/favicon.ico",
        "/index.html",
        "/main.053796e1e3924671dbee.js",
        "/polyfills.5fa68b41d92ab3b795c7.js",
        "/runtime.69ef21bf8bcc49e08c9e.js",
        "/scripts.64e0c48f62b83802d584.js",
        "/styles.e43a5292c1af99ea721e.css"
      ],
      "patterns": []
    },
    {
      "name": "assets",
      "installMode": "lazy",
      "updateMode": "prefetch",
      "urls": [
        "/assets/icons/icon-128x128.png",
        "/assets/icons/icon-144x144.png",
        "/assets/icons/icon-152x152.png",
        "/assets/icons/icon-192x192.png",
        "/assets/icons/icon-384x384.png",
        "/assets/icons/icon-512x512.png",
        "/assets/icons/icon-72x72.png",
        "/assets/icons/icon-96x96.png"
      ],
      "patterns": []
    }
  ],
  "dataGroups": [
    {
      "name": "contentful-api",
      "patterns": [
        "https:\\/\\/cdn\\.contentful\\.com\\/spaces\\/MY-SPACE-ID\\/environments\\/master\\/entries\\?content_type=[^/]*[^/]*"
      ],
      "strategy": "freshness",
      "maxSize": 20,
      "maxAge": 3600000,
      "timeoutMs": 5000,
      "version": 1
    }
  ],
  "hashTable": {
    "/0.05ca3f943f64d55f5fa7.js": "dd54298a2e628e75609193708728c5712ced700c",
    "/assets/icons/icon-128x128.png": "dae3b6ed49bdaf4327b92531d4b5b4a5d30c7532",
    "/assets/icons/icon-144x144.png": "b0bd89982e08f9bd2b642928f5391915b74799a7",
    "/assets/icons/icon-152x152.png": "7479a9477815dfd9668d60f8b3b2fba709b91310",
    "/assets/icons/icon-192x192.png": "1abd80d431a237a853ce38147d8c63752f10933b",
    "/assets/icons/icon-384x384.png": "329749cd6393768d3131ed6304c136b1ca05f2fd",
    "/assets/icons/icon-512x512.png": "559d9c4318b45a1f2b10596bbb4c960fe521dbcc",
    "/assets/icons/icon-72x72.png": "c457e56089a36952cd67156f9996bc4ce54a5ed9",
    "/assets/icons/icon-96x96.png": "3914125a4b445bf111c5627875fc190f560daa41",
    "/favicon.ico": "64a8e578c637632445eb27146bdace22a0a67d3f",
    "/index.html": "25109496c7fb90a5405bfcfc6e9dd6cf58ec3944",
    "/main.053796e1e3924671dbee.js": "5c36b96118d14a3359f5ee1fa0e6364039800813",
    "/polyfills.5fa68b41d92ab3b795c7.js": "8bec897f2edb48724e3ee0be629023a4341f11fe",
    "/runtime.69ef21bf8bcc49e08c9e.js": "f8968c9729551f87114304197d9024d845442965",
    "/scripts.64e0c48f62b83802d584.js": "a5da6a77894682a4392ff3cf708be911dd595c12",
    "/styles.e43a5292c1af99ea721e.css": "d7f76e3762b515c48a214df68fea7775d5095085"
  },
  "navigationUrls": [
    {
      "positive": true,
      "regex": "^\\/.*$"
    },
    {
      "positive": false,
      "regex": "^\\/(?:.+\\/)?[^/]*\\.[^/]*$"
    },
    {
      "positive": false,
      "regex": "^\\/(?:.+\\/)?[^/]*__[^/]*$"
    },
    {
      "positive": false,
      "regex": "^\\/(?:.+\\/)?[^/]*__[^/]*\\/.*$"
    }
  ]
}

In my app.component.ts constructor I'm using SwUpdate:

updates.available.subscribe(() => {
  updates.activateUpdate().then(() => {
    document.location.reload()
  })
})

I ran out of ideas. I would like to be able to get the page to reload on refresh or when opening new tab/browser.

If anyone with greater service worker knowledge has some ideas about what I could do, I would really appreciate it.

Thanks!

Jeff Posnick
  • 53,580
  • 14
  • 141
  • 167
Rolly
  • 163
  • 3
  • 10
  • localhost HTTPS? Testing PWAs with localhost has many challenges. You may want to try uploading everything in your build folder to a HTTPS free server like Glitch.me to test. It's much easier than spending the time with localhost issues. Make sure your url is HTTPS when your test there. Example of one of mine there. https://mypets.glitch.me/ – Mathias Sep 04 '18 at 12:40
  • Are you using HTTPS? It's a mandatory requirement for service workers. Also try the [PWA Google codelab](https://codelabs.developers.google.com/codelabs/your-first-pwapp/#0). You might have been missing important steps along the way. – ReyAnthonyRenacia Sep 04 '18 at 13:50
  • 1
    Thanks both of you for suggestions, I will have a look tomorrow. I just assumed since it works with http-server, that it should work on IIS as well, cause it's localhost over http, and some requirements are ignored on localhost, just like lighthouse is doing. – Rolly Sep 04 '18 at 19:43
  • I've deployed the app on githubpages(which uses https) and indeed the fetches are ok and the cache is being filled, so thanks for pointing that out. But when I run the audits I get "Service worker does not successfully serve the manifest's start_url, Timed out waiting for fetched start_url." I tried setting up start_url in manifest.json to "/", "/index.html" and "https://ACCOUNT.github.io/PROJECT/index.html" but I get the same audit failure. Any ideas ? – Rolly Sep 05 '18 at 08:52
  • May be a false error. Do you get the Add To Home Screen pop up on Android? https://github.com/GoogleChrome/lighthouse/issues/4541 – Mathias Sep 05 '18 at 14:51
  • I did manage to get it to work on gh pages. start_url had to be set to "." Page loads when offline, but I get a lighthouse failure stating "does not respond with a 200 when offline" and 81 points. Details for this failure says "If you're building a Progressive Web App, consider using a service worker so that your app can work offline". Well isn't that ironic ? :) – Rolly Sep 06 '18 at 12:01
  • I should probably mention that it works only on that session. On new tab/browser window it doesn't load the content. This is extremely frustrating I must say. – Rolly Sep 06 '18 at 12:18
  • I don't get a add to homescreen popup on Android. – Rolly Sep 07 '18 at 09:34
  • 1
    @Rolly Funny reading this 3 years later and having the same issue. What was your solution? My PWA works; everything except when it's offline. – Furkan Öztürk Jun 03 '21 at 18:16

0 Answers0