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

RFC: Make the library a singleton in the window context #49

Open
ukutaht opened this issue Nov 15, 2022 · 1 comment
Open

RFC: Make the library a singleton in the window context #49

ukutaht opened this issue Nov 15, 2022 · 1 comment
Labels
enhancement New feature or request

Comments

@ukutaht
Copy link
Contributor

ukutaht commented Nov 15, 2022

There is a specific motivator for this RFC. When the enableAutoPageviews is called multiple times inadvertently by the library client, it registers multiple listeners on the pushState API and starts sending duplicate events.

It would be easy to avoid this mistake by storing some information on the window object and making any further calls to enableAutoPageviews act as a no-op.

Nice to have: in development mode, we should console.warn about using the library in an unintended way.

This begs two questions:

  1. Is there any concern with making the library act as a singleton? Put in other words, can registering multiple listeners for pushState and popState by this library ever be considered a feature not a bug?
  2. Is there some sort of common API to tell bundlers to elide some warnings or code in production mode as opposed to development mode? Can NODE_ENV be used for that?
@ukutaht ukutaht added the enhancement New feature or request label Nov 15, 2022
@sandstrom
Copy link

sandstrom commented Nov 30, 2022

Instead of making the library a singleton, I think effort should be put into something like this instead:

plausible/analytics#2414

That way, the library would be just that, a library (and a class), of which you could have many instances.

Then, the wrapper itself would act as a singleton, to make sure that it (by default) wouldn't setup multiple listeners.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants