Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename the google analytics script to avoid errors due to ad/content blockers #1646

Closed
philipwalton opened this issue Sep 18, 2018 · 6 comments
Assignees
Labels
Breaking Change Denotes a "major" semver change. workbox-google-analytics
Milestone

Comments

@philipwalton
Copy link
Member

philipwalton commented Sep 18, 2018

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

@gauntface
Copy link

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.

@mathiasbynens
Copy link
Member

FWIW, we’re hitting this on v8.dev: v8/v8.dev#32

@philipwalton
Copy link
Member Author

@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.

@gauntface
Copy link

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

@wodCZ
Copy link

wodCZ commented Oct 30, 2018

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.

@philipwalton
Copy link
Member Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaking Change Denotes a "major" semver change. workbox-google-analytics
Projects
None yet
Development

No branches or pull requests

5 participants