workbox-routing#registerRoute JavaScript Examples

The following examples show how to use workbox-routing#registerRoute. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: service-worker.js    From pack11ty with MIT License 6 votes vote down vote up
// Pages
// Try to get fresh HTML from network, but don't wait for more than 2 seconds
registerRoute(
  ({ request }) => request.destination === 'document',
  new NetworkFirst({
    cacheName: 'pages',
    networkTimeoutSeconds: 2,
    plugins: [new BroadcastUpdatePlugin()],
  })
);
Example #2
Source File: service-worker.js    From ReactCookbook-source with MIT License 6 votes vote down vote up
registerRoute(
    // Return false to exempt requests from being fulfilled by index.html.
    ({request, url}) => {
        // If this isn't a navigation, skip.
        if (request.mode !== 'navigate') {
            return false;
        } // If this is a URL that starts with /_, skip.

        if (url.pathname.startsWith('/_')) {
            return false;
        } // If this looks like a URL for a resource, because it contains // a file extension, skip.

        if (url.pathname.match(fileExtensionRegexp)) {
            return false;
        } // Return true to signal that we want to use the handler.

        return true;
    },
    createHandlerBoundToURL(process.env.PUBLIC_URL + '/index.html')
);
Example #3
Source File: service-worker.js    From covid with GNU General Public License v3.0 6 votes vote down vote up
registerRoute(
  // Return false to exempt requests from being fulfilled by index.html.
  ({ request, url }) => {
    // If this isn't a navigation, skip.
    if (request.mode !== 'navigate') {
      return false;
    }
    if (url.pathname.startsWith('/_')) {
      return false;
    }
    if (url.pathname.endsWith('/index.html')) {
      return false;
    }
    if (url.pathname.match(fileExtensionRegexp)) {
      return false;
    }
    return true;
  },
  createHandlerBoundToURL(process.env.PUBLIC_URL + '/index.html')
);
Example #4
Source File: service-worker.js    From rahat-vendor with GNU Affero General Public License v3.0 6 votes vote down vote up
registerRoute(
	// Return false to exempt requests from being fulfilled by index.html.
	({ request, url }) => {
		// If this isn't a navigation, skip.
		if (request.mode !== 'navigate') {
			return false;
		} // If this is a URL that starts with /_, skip.

		if (url.pathname.startsWith('/_')) {
			return false;
		} // If this looks like a URL for a resource, because it contains // a file extension, skip.

		if (url.pathname.match(fileExtensionRegexp)) {
			return false;
		} // Return true to signal that we want to use the handler.

		return true;
	},
	createHandlerBoundToURL(process.env.PUBLIC_URL + '/index.html')
);
Example #5
Source File: custom-sw.js    From tclone with MIT License 6 votes vote down vote up
registerRoute(
  ({ request }) => request.destination === "document",
  async ({ event }) => {
    const cache = await caches.open("index");
    const cachedResponse = await cache.match("/index.html");
    const networkResponsePromise = fetch("/index.html");

    event.waitUntil(
      (async function () {
        const networkResponse = await networkResponsePromise;
        await cache.put("/index.html", networkResponse.clone());
      })()
    );

    // Returned the cached response if we have one, otherwise return the network response.
    return cachedResponse || networkResponsePromise;
  }
);
Example #6
Source File: service-worker.js    From pack11ty with MIT License 6 votes vote down vote up
// Google Analytics
registerRoute(
  ({ request }) =>
    request.url === 'https://www.google-analytics.com/analytics.js',
  new CacheFirst({
    cacheName: 'shell',
    plugins: [
      new ExpirationPlugin({
        maxAgeSeconds: 10 * 24 * 60 * 60, // 10 Days
      }),
    ],
  })
);
Example #7
Source File: service-worker.js    From covid with GNU General Public License v3.0 6 votes vote down vote up
registerRoute(
  // Return false to exempt requests from being fulfilled by index.html.
  ({ request, url }) => {
    // If this isn't a navigation, skip.
    if (request.mode !== 'navigate') {
      return false
    } // If this is a URL that starts with /_, skip.

    if (url.pathname.startsWith('/_')) {
      return false
    } // If this looks like a URL for a resource, because it contains // a file extension, skip.

    if (url.pathname.match(fileExtensionRegexp)) {
      return false
    } // Return true to signal that we want to use the handler.

    return true
  },
  createHandlerBoundToURL(process.env.PUBLIC_URL + '/index.html')
)
Example #8
Source File: service-worker.js    From pack11ty with MIT License 6 votes vote down vote up
// Images
registerRoute(
  ({ request }) => request.destination === 'image',
  new StaleWhileRevalidate({
    cacheName: 'images',
    plugins: [
      new CacheableResponsePlugin({
        statuses: [0, 200],
      }),
      new ExpirationPlugin({
        maxEntries: 200,
        maxAgeSeconds: 90 * 24 * 60 * 60, // 90 Days
      }),
    ],
  })
);
Example #9
Source File: service-worker.js    From generator-webapp-rocket with MIT License 6 votes vote down vote up
registerRoute(
    // Return false to exempt requests from being fulfilled by index.html.
    ({ request, url }) => {
        // If this isn't a navigation, skip.
        if (request.mode !== 'navigate') {
            return false;
        } // If this is a URL that starts with /_, skip.

        if (url.pathname.startsWith('/_')) {
            return false;
        } // If this looks like a URL for a resource, because it contains // a file extension, skip.

        if (url.pathname.match(fileExtensionRegexp)) {
            return false;
        } // Return true to signal that we want to use the handler.

        return true;
    },
    createHandlerBoundToURL(process.env.PUBLIC_URL + '/index.html')
);
Example #10
Source File: service-worker.js    From portfolio-react with MIT License 6 votes vote down vote up
registerRoute(
	// Return false to exempt requests from being fulfilled by index.html.
	({request, url}) => {
		// If this isn't a navigation, skip.
		if (request.mode !== 'navigate') {
			return false
		} // If this is a URL that starts with /_, skip.

		if (url.pathname.startsWith('/_')) {
			return false
		} // If this looks like a URL for a resource, because it contains // a file extension, skip.

		if (url.pathname.match(fileExtensionRegexp)) {
			return false
		} // Return true to signal that we want to use the handler.

		return true
	},
	createHandlerBoundToURL(process.env.PUBLIC_URL + '/index.html')
)
Example #11
Source File: service-worker.js    From esc-configurator with GNU Affero General Public License v3.0 6 votes vote down vote up
registerRoute(
  // Return false to exempt requests from being fulfilled by index.html.
  ({
    request, url,
  }) => {
    // If this isn't a navigation, skip.
    if (request.mode !== 'navigate') {
      return false;
    } // If this is a URL that starts with /_, skip.

    if (url.pathname.startsWith('/_')) {
      return false;
    } // If this looks like a URL for a resource, because it contains // a file extension, skip.

    if (url.pathname.match(fileExtensionRegexp)) {
      return false;
    } // Return true to signal that we want to use the handler.

    return true;
  },
  createHandlerBoundToURL(`${process.env.PUBLIC_URL}/index.html`)
);
Example #12
Source File: sw.js    From gk6x_gui with MIT License 6 votes vote down vote up
registerRoute(
    new RegExp('\\.(woff|woff2|eot|ttf)(\\?.+)?$'),
    new CacheFirst()
);
Example #13
Source File: service-worker.js    From ResoBin with MIT License 6 votes vote down vote up
// Set up App Shell-style routing, so that all navigation requests
// are fulfilled with your index.html shell.
// Learn more at https://developers.google.com/web/fundamentals/architecture/app-shell
registerRoute(({ request, url }) => {
  // Return true to signal that we want to use the handler.
  // Return false to exempt requests from being fulfilled by index.html.
  // If this isn't a navigation, skip.
  if (request.mode !== 'navigate') {
    return false
  }

  // Skip urls that lie in the ignored routes.
  if (ignoredRoutes.some((route) => url.pathname.startsWith(route))) {
    return false
  }

  // Skip resource URLs (contain a file extension)
  if (url.pathname.match('/[^/?]+\\.[^/]+$')) {
    return false
  }

  return true
}, createHandlerBoundToURL(`${process.env.PUBLIC_URL}/index.html`))
Example #14
Source File: service-worker.js    From zero-neko with MIT License 6 votes vote down vote up
registerRoute(
  // Return false to exempt requests from being fulfilled by index.html.
  ({ request, url }) => {
    // If this isn't a navigation, skip.
    if (request.mode !== 'navigate') {
      return false;
    } // If this is a URL that starts with /_, skip.

    if (url.pathname.startsWith('/_')) {
      return false;
    } // If this looks like a URL for a resource, because it contains // a file extension, skip.

    if (url.pathname.match(fileExtensionRegexp)) {
      return false;
    } // Return true to signal that we want to use the handler.

    return true;
  },
  createHandlerBoundToURL(process.env.PUBLIC_URL + '/index.html')
);
Example #15
Source File: service-worker.js    From covid with GNU General Public License v3.0 6 votes vote down vote up
registerRoute(
  // Add in any other file extensions or routing criteria as needed.
  ({ url }) => url.origin === self.location.origin && url.pathname.endsWith('.geojson'), // Customize this strategy as needed, e.g., by changing to CacheFirst.
  new StaleWhileRevalidate({
    cacheName: 'geojson',
    plugins: [
      new ExpirationPlugin({ maxEntries: 15 }),
    ],
  })
);
Example #16
Source File: service-worker.js    From Queen with MIT License 6 votes vote down vote up
registerRoute(
  // Return false to exempt requests from being fulfilled by index.html.
  ({ request, url }) => {
    // If this isn't a navigation, skip.
    if (request.mode !== 'navigate') {
      return false;
    } // If this is a URL that starts with /_, skip.

    if (url.pathname.startsWith('/_')) {
      return false;
    } // If this looks like a URL for a resource, because it contains // a file extension, skip.

    if (url.pathname.match(fileExtensionRegexp)) {
      return false;
    } // Return true to signal that we want to use the handler.

    return true;
  },
  createHandlerBoundToURL(`${process.env.PUBLIC_URL}/index.html`)
);
Example #17
Source File: sw-template.js    From Pf2eTools with MIT License 6 votes vote down vote up
/*
the base case route - for images that have fallen through every other route
this is external images, for homebrew as an example
*/
registerRoute(({request}) => request.destination === "image", new NetworkFirst({
	cacheName: "external-image-cache",
	plugins: [
		// this is a safeguard against an utterly massive cache - these numbers may need tweaking
		new ExpirationPlugin({maxAgeSeconds: 7 /* days */ * 24 * 60 * 60, maxEntries: 100, purgeOnQuotaError: true}),
	],
}));
Example #18
Source File: service-worker.js    From ReactCookbook-source with MIT License 6 votes vote down vote up
registerRoute(
    ({url}) => url.origin === 'https://fonts.gstatic.com',
    new CacheFirst({
        cacheName: 'fonts',
        plugins: [
            new CacheableResponsePlugin({
                statuses: [0, 200],
            }),
            new ExpirationPlugin({
                maxAgeSeconds: 60 * 60 * 7,
                maxEntries: 5
            }),
        ],
    })
);
Example #19
Source File: service-worker.js    From zero-neko with MIT License 6 votes vote down vote up
// An example runtime caching route for requests that aren't handled by the
// precache, in this case same-origin .png requests like those from in public/
registerRoute(
  // Add in any other file extensions or routing criteria as needed.
  ({ url }) => url.origin === self.location.origin && url.pathname.endsWith('.png'), // Customize this strategy as needed, e.g., by changing to CacheFirst.
  new StaleWhileRevalidate({
    cacheName: 'images',
    plugins: [
      // Ensure that once this runtime cache reaches a maximum size the
      // least-recently used images are removed.
      new ExpirationPlugin({ maxEntries: 50 }),
    ],
  })
);
Example #20
Source File: service-worker.js    From WhatSend with MIT License 6 votes vote down vote up
registerRoute(
  // Return false to exempt requests from being fulfilled by index.html.
  ({ request, url }) => {
    // If this isn't a navigation, skip.
    if (request.mode !== 'navigate') {
      return false;
    } // If this is a URL that starts with /_, skip.

    if (url.pathname.startsWith('/_')) {
      return false;
    } // If this looks like a URL for a resource, because it contains // a file extension, skip.

    if (url.pathname.match(fileExtensionRegexp)) {
      return false;
    } // Return true to signal that we want to use the handler.

    return true;
  },
  createHandlerBoundToURL(`${process.env.PUBLIC_URL}/index.html`)
);
Example #21
Source File: service-worker.js    From ReactCookbook-source with MIT License 6 votes vote down vote up
registerRoute(
  ({ request, url }) => {
    if (request.mode !== 'navigate') {
      return false;
    }
    if (url.pathname.startsWith('/_')) {
      return false;
    }
    if (url.pathname.match(fileExtensionRegexp)) {
      return false;
    }
    return true;
  },
  createHandlerBoundToURL(process.env.PUBLIC_URL + '/index.html')
);
Example #22
Source File: service-worker.js    From SauceKudasai with MIT License 6 votes vote down vote up
// An example runtime caching route for requests that aren't handled by the
// precache, in this case same-origin .png requests like those from in public/
registerRoute(
	// Add in any other file extensions or routing criteria as needed.
	({ url }) => url.origin === self.location.origin && url.pathname.endsWith('.png'), // Customize this strategy as needed, e.g., by changing to CacheFirst.
	new CacheFirst({
		cacheName: 'images',
		plugins: [
			// Ensure that once this runtime cache reaches a maximum size the
			// least-recently used images are removed.
			new ExpirationPlugin({ maxEntries: 10 }),
		],
	})
);
Example #23
Source File: service-worker.js    From WhatSend with MIT License 6 votes vote down vote up
// An example runtime caching route for requests that aren't handled by the
// precache, in this case same-origin .png requests like those from in public/
registerRoute(
  // Add in any other file extensions or routing criteria as needed.
  ({ url }) => url.origin === self.location.origin && url.pathname.endsWith('.png'), // Customize this strategy as needed, e.g., by changing to CacheFirst.
  new StaleWhileRevalidate({
    cacheName: 'images',
    plugins: [
      // Ensure that once this runtime cache reaches a maximum size the
      // least-recently used images are removed.
      new ExpirationPlugin({ maxEntries: 50 }),
    ],
  })
);
Example #24
Source File: sw-template.js    From Pf2eTools with MIT License 5 votes vote down vote up
registerRoute(
	({request}) => runtimeManifest.has(request.url),
	revisionCacheFirst,
);
Example #25
Source File: sw.js    From iris-messenger with MIT License 5 votes vote down vote up
registerRoute(
	'/peer_id',
	new NetworkOnly()
);
Example #26
Source File: service-worker.js    From todo-pwa with MIT License 5 votes vote down vote up
registerRoute(
  new RegExp(/\.(?:png|gif|jpg|svg|ico|webp)$/),
  new CacheFirst({
    cacheName: 'image-cache-vue',
  }),
  'GET'
);
Example #27
Source File: sw.js    From rctf with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
registerRoute(
  ({ url }) => url.pathname === '/api/v1/challs',
  new NetworkFirst()
)
Example #28
Source File: sw-template.js    From Pf2eTools with MIT License 5 votes vote down vote up
/*
this tells workbox to cache fonts, and serve them cache first after first load
this works on the assumption that fonts are static assets and won't change
 */
registerRoute(({request}) => request.destination === "font", new CacheFirst({
	cacheName: "font-cache",
}));
Example #29
Source File: sw.js    From iris-messenger with MIT License 5 votes vote down vote up
registerRoute(
  () => true,
  new StaleWhileRevalidate()
);