-
Notifications
You must be signed in to change notification settings - Fork 104
Provide way to access client initialization state #188
Copy link
Copy link
Closed
Labels
good first issueGood for newcomersGood for newcomers
Description
Currently if the Counterscale.init() function is run twice, it throws and error.
Sometimes code runs twice - like in react strict mode. So if it's already initialized, it would be nice to do one of these:
- It just returns early. Maybe silently?
- Provide the a function that will return the client, or a boolean from the GLOBALS variable here: https://github.com/benvinegar/counterscale/blob/main/packages/tracker/src/index.ts#L7 so ew can conditionally run init()
Also while we're talking about the client code - is it possible to also return early if history doesn't exist? Helpful for not breaking server rendered code. Currently doing this:
let isInitialized = false;
const isClient = typeof window !== 'undefined';
export function analytics() {
if (isClient && !isInitialized) {
Counterscale.init({
siteId: `wesbos-${import.meta.env.DEV ? '-dev' : ''}`,
reporterUrl: 'https://nunyabiznass.wesbos.com/collect',
});
isInitialized = true;
}
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
good first issueGood for newcomersGood for newcomers