Workbox: Rename the google analytics script to avoid errors due to ad/content blockers

Created on 18 Sep 2018  路  6Comments  路  Source: GoogleChrome/workbox

Forking the discussion from #1186.

We should consider renaming the built workbox-google-analytics script because apparently some ad/content blockers will block it, which results in an error, and thus the entire service worker install fails.

(Note, this will probably require a breaking change.)

/cc @dumbmatter

Breaking Change P0 workbox-google-analytics

Most helpful comment

@mathiasbynens sorry, as @jeffposnick mentioned in that thread, you can use the v4 alpha. We're calling it an alpha because we expect the API to change a bit more, not because we think it's unstable.

@gauntface I think that's a cleaner option, but it requires people to know about the problem. I suspect the majority of developers using workbox-google-analytics don't realize this is even a possibility.

All 6 comments

Alternative approach (and just throwing it out there), we could not throw on a bad import: https://github.com/GoogleChrome/workbox/blob/master/packages/workbox-sw/controllers/WorkboxSW.mjs#L144

Then allow developers to check if workbox.googleAnalytics exists?

if (workbox.googleAnalytics) {
    workbox.googleAnalytics.initialize();
}

Not sure if there is a good idea, but would allow developers to handle a situation like this.

FWIW, we鈥檙e hitting this on v8.dev: https://github.com/v8/v8.dev/issues/32

@mathiasbynens sorry, as @jeffposnick mentioned in that thread, you can use the v4 alpha. We're calling it an alpha because we expect the API to change a bit more, not because we think it's unstable.

@gauntface I think that's a cleaner option, but it requires people to know about the problem. I suspect the majority of developers using workbox-google-analytics don't realize this is even a possibility.

@philipwalton agree - I was just putting it out there.

For anyone struggling with this issue, wrap the workbox.googleAnalytics.initialize() call with try-catch statement:

try {
    workbox.googleAnalytics.initialize();
} catch (e) {
    // fail silently
}

This way adblock doesn't break whole service-worker.

@wodCZ, yep, that works, but hopefully in v4 developers won't even have to worry about it.

Another options (since we're listing options), is to bundle the SW yourself via rollup, webpack, or something similar. That way you don't use importScripts at all, and the workbox-google-analytics code is just part of your SW bundle.

We're hoping to publish more guides on how to do this in the near future, but if you're interested in doing it now you can look at the SW source code used on my blog as an example.

Was this page helpful?
0 / 5 - 0 ratings