import toolbox from 'sw-toolbox';

/* global process */
toolbox.options.debug = (process.env.NODE_ENV !== 'production');

toolbox.precache([
  '/service-worker.js',
  '/index.html',
  '/app.js',
  '/web-worker.js',
  '/manifest.json',
]);

/* global Request */
toolbox.router.get('/(.*)', (request, values, options) => {
  return toolbox.networkFirst(request, values, options).catch(() => {
    return toolbox.cacheOnly(new Request('/index.html'), values, options);
  });
}, {
  origin: /localhost:8080$/,
});

toolbox.router.any('/(.*)', toolbox.networkOnly, {
  origin: /localhost:3000$/,
});


toolbox.router.get('/(.*)', toolbox.cacheFirst, {
  origin: /\.(googleapis|gstatic)\.com$/,
  cache: {
    name: 'googleapis',
  },
});


(sw => {
  sw.addEventListener('activate', event => {
    event.waitUntil(self.clients.claim());
  });
})(self); /* global self */

import toolbox from 'sw-toolbox';

const { Request, Response } = self; /* global self */


const statics = [
  '/service-worker.js',
  '/index.html',
  '/app.js',
  '/web-worker.js',
  '/manifest.json',
];
const exts = statics.map(name => name.slice(name.lastIndexOf('.')));
const extRegex = new RegExp(`(${exts.join('|')})$`, 'i');


// Cache static assets.
toolbox.precache(statics);


// Enable debug mode and suppress sockjs-node request errors.
if (process.env.NODE_ENV !== 'production') { /* global process */
  toolbox.options.debug = true;

  toolbox.router.get('/sockjs-node/*', (request, values, options) => {
    return toolbox.networkOnly(request, values, options)
                  .catch(() => new Response());
  });
}


// Redirect SPA subroute requests to index.
toolbox.router.get('/*', (request, values, options) => {
  // Do not redirect static asset requests.
  if (request.url.match(extRegex)) {
    // TODO: Maybe use `fastest` when in production for faster app startup?
    return toolbox.networkFirst(request, values, options)
                  .catch(() => new Response());
  }

  // TODO: Maybe use `fastest` here? Why rely on the server for route redirects?
  return toolbox.networkFirst(request, values, options).catch(() => {
    return toolbox.cacheOnly(new Request('/index.html'), values, options)
                  .catch(() => new Response());
  });
}, {
  origin: /localhost:8080$/,
  cache: {
    name: 'statics',
  },
});


// Cache Google fonts and icons.
// TODO: Maybe use `cacheFirst` with additional `cache.maxAgeSeconds` here in
// order to not fire off network requests to Google on every startup?
toolbox.router.get('/*', toolbox.fastest, {
  origin: /\.(googleapis|gstatic)\.com$/,
  cache: {
    name: 'googleapis',
  },
});

Change some service worker caching behavior. * Ignore socket requests when in development. * Use fastest for google assets. * Add some todos to think about.
Change some service worker caching behavior.
* Ignore socket requests when in development.
* Use fastest for google assets.
* Add some todos to think about.
