-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirebase.tsx
118 lines (105 loc) · 3.68 KB
/
firebase.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import firebase from "firebase/compat/app";
import "firebase/compat/auth";
import "firebase/compat/firestore";
import "firebase/compat/messaging";
import "firebase/compat/performance";
import "firebase/compat/storage";
import { getPerformance } from "firebase/performance";
import { local } from "@/config";
import { Environment, REACT_APP_ENV } from "@/shared/constants";
import config from "../../config";
const CACHE_SIZE_LIMIT = 104857600; // 100 MB
interface FirebaseError extends Error {
code: string;
}
const app = firebase.initializeApp(config.firebase);
let db = firebase.firestore();
// WARNING: This function is enabling local emulator. If removed, move the emulator config out of this function
enableUnlimitedCachePersistence();
// Function to handle Firestore persistence errors
function handlePersistenceError(err: any) {
if (err.code === "failed-precondition") {
console.log("Multiple tabs open or other conflict.");
} else if (err.code === "unimplemented") {
console.log("Persistence is not supported in this browser.");
} else if (err.name === "QuotaExceededError") {
console.log("Storage quota exceeded. Consider clearing cache.");
clearFirestoreCache();
} else {
console.error("Error enabling persistence:", err);
reinitializeFirestoreWithPersistence();
}
}
function reinitializeFirestoreWithPersistence() {
db = firebase.firestore(); // Reinitialize Firestore instance
const settings = { cacheSizeBytes: CACHE_SIZE_LIMIT };
db.settings(settings);
db.enablePersistence({ synchronizeTabs: true })
.then(() => {
console.log("Persistence re-enabled.");
return;
})
.catch(handlePersistenceError);
}
// Function to clear Firestore cache and re-enable persistence
export function clearFirestoreCache() {
db.terminate()
.then(() => {
console.log("Firestore instance terminated.");
return db.clearPersistence(); // Safe to clear persistence now
})
.then(() => {
console.log("Persistence cleared. Waiting before reinitializing...");
return new Promise((resolve) => setTimeout(resolve, 2000)); // Wait 2 second
})
.then(() => {
console.log("Cache cleared successfully.");
reinitializeFirestoreWithPersistence(); // Reinitialize Firestore
window.location.reload();
return;
})
.catch((err) => {
if (err.code === "failed-precondition") {
console.log("Cannot clear persistence: Firestore is still running.");
} else {
console.error("Error clearing persistence cache:", err);
}
});
}
// Enable Firestore persistence with unlimited cache size and error handling
function enableUnlimitedCachePersistence() {
const settings = {
cacheSizeBytes: CACHE_SIZE_LIMIT,
};
db.settings(settings);
// Enable Firestore emulator in the local environment
if (REACT_APP_ENV === Environment.Local) {
firebase.auth().useEmulator(local.firebase.authDomain);
firebase
.firestore()
.useEmulator(
"localhost",
Number(local.firebase.databaseURL.split(/:/g)[2]),
);
}
db.enablePersistence({ synchronizeTabs: true }).catch(handlePersistenceError);
}
let perf;
if (typeof window !== "undefined" && typeof window.fetch !== "undefined") {
perf = getPerformance(app);
} else {
perf = {
trace: () => ({
start: () => {},
stop: () => {},
}),
};
}
export { perf };
// firebase.firestore.setLogLevel("debug");
export const isFirebaseError = (error: any): error is FirebaseError =>
(error && error.code && error.code.startsWith("auth/")) ||
error.name === "FirebaseError";
export const isFirestoreCacheError = (error: any): boolean =>
isFirebaseError(error) && error.code === "unavailable";
export default firebase;