question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use a single fetch handler for all Router instances

See original GitHub issue

Library Affected: 5.0.0 workbox-google-analytics, workbox-routing, etc.

Browser & Platform: “all browsers”

Issue or Feature Request Description:

I’m using setDefaultHandler with offline-fallback page “sw-offline” to handle non-matching requests along with offline googleAnalytics.

However, the fetch routes added by googleAnalytics is not preferred over setDefaultHandler. For all “google analytics” requests, default route set by setDefaultHandler is used.

	// few registerRoute are used here

	workbox.googleAnalytics.initialize(); 
	const defaultNetworkOnlyHandler = async (args) => {
		console.log('Default:'+args.url);
		try {
			var defaultNetworkOnly = new NetworkOnly({
				fetchOptions: args.event.request.mode=='navigate'?{}:{credentials:'include'}
			});
			const response = await defaultNetworkOnly.handle(args);
			return response || await caches.match('sw-offline');
		} catch(error) {
			return await caches.match('sw-offline');
		}
	};
	setDefaultHandler(defaultNetworkOnlyHandler);

As it can be seen in console, “https://www.google-analytics.com/analytics.js” always uses default route of “NetworkOnly” instead of “NetworkFirst” route defined in workbox-google-analytics. Due to which analytics.js is not cached and throws an error in offline mode.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jeffposnickcommented, Apr 28, 2020

Actually, I forgot that workbox-precaching in v6 creates it own Router instead of using the default Router, so forget what I said about it being similar.

0reactions
nhoizeycommented, Dec 1, 2020

@jeffposnick ok, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

express.js - single routing handler for multiple routes in a ...
I came across this question while looking for the same functionality. @Jonathan Ong mentioned in a comment above that using arrays for paths...
Read more >
workbox-routing - Chrome Developers
Routes requests in your service worker to specific caching strategies or callback functions.
Read more >
FetchEvent - Web APIs | MDN
Chrome Edge FetchEvent Full support. Chrome40. Toggle history Full support. Edge17. Tog... FetchEvent() constructor Full support. Chrome40. Toggle history Full support. Edge17. Tog... clientId Full support....
Read more >
Using Express.js Routes for Promise-based Error Handling
status(200).send(data); // routes/users.js router.get('/', function(req, ...
Read more >
Easier Service Worker caching with Routing - ITNEXT
One, it uses a middleware pattern so you can chain handlers. ... you can use a default route at the end to handle...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found