fix: read browser locale and is debug on first event

`shared.ts` currently reads the default browser locale and is debug on import, which causes
issueswhen imported in a server component. This moves the reads for those to whenever the first
event issent and those values are actually required, which is typically on the client.

fix #7
This commit is contained in:
Noah Richardson 2024-07-19 09:37:35 -04:00
parent e4250b9352
commit 95f9710547
8 changed files with 40 additions and 14 deletions

View file

@ -1,6 +1,6 @@
{ {
"name": "@aptabase/angular", "name": "@aptabase/angular",
"version": "0.0.1", "version": "0.0.2",
"description": "Angular SDK for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps", "description": "Angular SDK for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps",
"scripts": { "scripts": {
"ng": "ng", "ng": "ng",

View file

@ -1,3 +1,7 @@
## 0.1.2
- Move browser locale and is debug reads from import to first event to better support SSR
## 0.1.1 ## 0.1.1
- Support for custom API path - Support for custom API path

View file

@ -1,6 +1,6 @@
{ {
"name": "@aptabase/browser", "name": "@aptabase/browser",
"version": "0.1.1", "version": "0.1.2",
"type": "module", "type": "module",
"description": "Browser Extension SDK for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps", "description": "Browser Extension SDK for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps",
"main": "./dist/index.cjs", "main": "./dist/index.cjs",

View file

@ -1,3 +1,7 @@
## 0.3.4
- Move browser locale and is debug reads from import to first event to better support SSR
## 0.3.3 ## 0.3.3
- Rename `apiPath` to `apiUrl` - Rename `apiPath` to `apiUrl`

View file

@ -1,6 +1,6 @@
{ {
"name": "@aptabase/react", "name": "@aptabase/react",
"version": "0.3.3", "version": "0.3.4",
"type": "module", "type": "module",
"description": "React SDK for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps", "description": "React SDK for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps",
"main": "./dist/index.cjs", "main": "./dist/index.cjs",

View file

@ -1,5 +1,5 @@
const defaultLocale = getBrowserLocale(); let defaultLocale: string | undefined;
const defaultIsDebug = getIsDebug(); let defaultIsDebug: boolean | undefined;
const isInBrowser = typeof window !== 'undefined' && typeof window.fetch !== 'undefined'; const isInBrowser = typeof window !== 'undefined' && typeof window.fetch !== 'undefined';
const isInBrowserExtension = typeof chrome !== 'undefined' && !!chrome.runtime?.id; const isInBrowserExtension = typeof chrome !== 'undefined' && !!chrome.runtime?.id;
@ -103,8 +103,8 @@ export async function sendEvent(opts: {
sessionId: opts.sessionId, sessionId: opts.sessionId,
eventName: opts.eventName, eventName: opts.eventName,
systemProps: { systemProps: {
locale: opts.locale ?? defaultLocale, locale: opts.locale ?? getBrowserLocale(),
isDebug: opts.isDebug ?? defaultIsDebug, isDebug: opts.isDebug ?? getIsDebug(),
appVersion: opts.appVersion ?? '', appVersion: opts.appVersion ?? '',
sdkVersion: opts.sdkVersion, sdkVersion: opts.sdkVersion,
}, },
@ -123,25 +123,39 @@ export async function sendEvent(opts: {
} }
function getBrowserLocale(): string | undefined { function getBrowserLocale(): string | undefined {
if (defaultLocale) {
return defaultLocale;
}
if (typeof navigator === 'undefined') { if (typeof navigator === 'undefined') {
return undefined; return undefined;
} }
if (navigator.languages.length > 0) { if (navigator.languages.length > 0) {
return navigator.languages[0]; defaultLocale = navigator.languages[0];
} else {
defaultLocale = navigator.language;
} }
return navigator.language; return defaultLocale;
} }
function getIsDebug(): boolean { function getIsDebug(): boolean {
if (defaultIsDebug !== undefined) {
return defaultIsDebug;
}
if (process.env['NODE_ENV'] === 'development') { if (process.env['NODE_ENV'] === 'development') {
return true; defaultIsDebug = true;
return defaultIsDebug;
} }
if (typeof location === 'undefined') { if (typeof location === 'undefined') {
return false; defaultIsDebug = false;
return defaultIsDebug;
} }
return location.hostname === 'localhost'; defaultIsDebug = location.hostname === 'localhost';
}
return defaultIsDebug;
}

View file

@ -1,3 +1,7 @@
## 0.4.3
- Move browser locale and is debug reads from import to first event to better support SSR
## 0.4.2 ## 0.4.2
- Fix error when running on chrome - Fix error when running on chrome

View file

@ -1,6 +1,6 @@
{ {
"name": "@aptabase/web", "name": "@aptabase/web",
"version": "0.4.2", "version": "0.4.3",
"type": "module", "type": "module",
"description": "JavaScript SDK for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps", "description": "JavaScript SDK for Aptabase: Open Source, Privacy-First and Simple Analytics for Mobile, Desktop and Web Apps",
"main": "./dist/index.cjs", "main": "./dist/index.cjs",