0

I cannot convinced why offline first not working in Polymer2. Here is my sw-precache-config.js and polymer.json

module.exports = {
  staticFileGlobs: [
    '/index.html',
    '/manifest.json',
    '/bower_components/webcomponentsjs/webcomponents-loader.js',
    '/images/*'
  ],
  navigateFallback: '/index.html',
  navigateFallbackWhitelist: [/^(?!.*\.html$|\/data\/).*/],
  runtimeCaching: [
    {
      urlPattern: /\/bower_components\/webcomponentsjs\/.*.js/,
      handler: 'fastest',
      options: {
        cache: {
          name: 'webcomponentsjs-polyfills-cache'
        }
      }
    },
    {
      urlPattern: /\/data\/images\/.*/,
      handler: 'cacheFirst',
      options: {
        cache: {
          maxEntries: 200,
          name: 'items-cache'
        }
      }
    },
    {
      urlPattern: /\/data\/.*json/,
      handler: 'fastest',
      options: {
        cache: {
          maxEntries: 100,
          name: 'data-cache'
        }
      }
    }
  ]
};

polymer.json

{
  "entrypoint": "index.html",
  "shell": "src/my-app.html",
  "fragments": [
    "src/my-home.html",
    "src/my-category.html",
    "src/my-company.html",
    "src/my-courses.html",
    "src/my-view404.html"
  ],
  "sources": [
    "src/**/*",
    "data/**/*",
    "images/**/*",
    "bower.json"
  ],
  "extraDependencies": [
    "manifest.json",
    "bower_components/webcomponentsjs/*.js"
  ],
  "lint": {
    "rules": ["polymer-2"]
  },
  "builds": [{
    "name": "bundled",
    "addServiceWorker": true,
    "bundle": true,
    "js": {"minify": false, "compile": false},
    "css": {"minify": false},
    "html": {"minify": false}
  },{
    "name": "unbundled",
    "addServiceWorker": true,
    "bundle": false,
    "js": {"minify": true, "compile": true},
    "css": {"minify": true},
    "html": {"minify": true}
  }]
}

Please let me know why it's not working at all?

PPShein
  • 13,309
  • 42
  • 142
  • 227
  • What do you mean by "not working"? Be specific. – Jordan Running May 11 '17 at 13:24
  • Why I off wifi or internet, it gone `page cannot be displayed` instead of displaying cache instead. https://13-dot-courses-polymer-166507.appspot.com/ – PPShein May 11 '17 at 13:34
  • Is that a link to your app? It doesn't look like a service-worker is being loaded and run. [These](https://github.com/PolymerElements/polymer-starter-kit/blob/master/index.html#L90-L94) are the lines in the starter kit that load the service worker if the browser supports it. – Ben Thomas May 12 '17 at 14:12

1 Answers1

0

you have to add "swPrecacheConfig": "sw-precache-config.js", to each 'build' array item you want to include your service-worker. Also, note that the dev (polymer serve) environment (not builded) bypass the service worker registration.

v1s10n_4
  • 598
  • 3
  • 18