Skip to content

Commit b3d3cc6

Browse files
authored
fix: frigade has not been initialized error (#437)
* fix: frigade has not been initialized error * simplify fix
1 parent 2396de0 commit b3d3cc6

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

.changeset/itchy-plums-destroy.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@frigade/js": patch
3+
"@frigade/react": patch
4+
---
5+
6+
Fixes an edge case where in some cases, the error `Frigade has not yet been initialized` can occur.

packages/js-api/src/core/frigade.ts

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -92,30 +92,32 @@ export class Frigade extends Fetchable {
9292
apiKey,
9393
...config,
9494
})
95-
this.init(this.config)
96-
if (isWeb() && this.config.syncOnWindowUpdates !== false) {
97-
document.addEventListener('visibilitychange', this.visibilityChangeHandler)
98-
// @ts-ignore
99-
if (window.navigation) {
95+
this.init(this.config).then(() => {
96+
if (isWeb() && this.config.syncOnWindowUpdates !== false) {
97+
document.addEventListener('visibilitychange', this.visibilityChangeHandler)
10098
// @ts-ignore
101-
window.navigation.addEventListener('navigate', async (event) => {
102-
try {
103-
if (this.getGlobalState().currentUrl === event.destination.url) {
104-
return
105-
}
106-
// if the new base url is the same but with a hashtag, don't refresh the state as the page has not changed.
107-
if (
108-
event.destination.url &&
109-
this.getGlobalState().currentUrl &&
110-
event.destination.url.split('#')[0] === this.getGlobalState().currentUrl.split('#')[0]
111-
) {
112-
return
113-
}
114-
this.queueRefreshStateFromAPI()
115-
} catch (e) {}
116-
})
99+
if (window.navigation) {
100+
// @ts-ignore
101+
window.navigation.addEventListener('navigate', async (event) => {
102+
try {
103+
if (this.getGlobalState().currentUrl === event.destination.url) {
104+
return
105+
}
106+
// if the new base url is the same but with a hashtag, don't refresh the state as the page has not changed.
107+
if (
108+
event.destination.url &&
109+
this.getGlobalState().currentUrl &&
110+
event.destination.url.split('#')[0] ===
111+
this.getGlobalState().currentUrl.split('#')[0]
112+
) {
113+
return
114+
}
115+
this.queueRefreshStateFromAPI()
116+
} catch (e) {}
117+
})
118+
}
117119
}
118-
}
120+
})
119121
}
120122

121123
/**
@@ -280,7 +282,12 @@ export class Frigade extends Fetchable {
280282
* @ignore
281283
*/
282284
public isReady(): boolean {
283-
return Boolean(this.config.__instanceId && this.config.apiKey && this.initPromise)
285+
return Boolean(
286+
this.config.__instanceId &&
287+
this.config.apiKey &&
288+
this.initPromise &&
289+
frigadeGlobalState[getGlobalStateKey(this.config)]
290+
)
284291
}
285292

286293
/**

0 commit comments

Comments
 (0)