forked from SkateHive/skatehive3.0
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstrumentation.ts
More file actions
126 lines (118 loc) · 3.93 KB
/
Copy pathinstrumentation.ts
File metadata and controls
126 lines (118 loc) · 3.93 KB
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
119
120
121
122
123
124
125
126
/**
* Next.js Instrumentation — runs once when the server starts.
*
* We polyfill `indexedDB` on the server so that libraries like
* `idb-keyval` (used by @farcaster/auth-kit) don't crash with
* "ReferenceError: indexedDB is not defined" during SSR.
*
* The polyfill is intentionally minimal: every operation resolves
* to `undefined` / no-ops. Auth-kit will simply find no persisted
* state on the server and fall through to its defaults, which is
* the correct behaviour.
*/
function noopRequest(result: any = undefined): IDBRequest {
const req = {
result,
error: null,
source: null,
transaction: null,
readyState: "done" as IDBRequestReadyState,
onsuccess: null as any,
onerror: null as any,
onupgradeneeded: null as any,
addEventListener: () => {},
removeEventListener: () => {},
dispatchEvent: () => true,
oncomplete: null as any,
onabort: null as any,
};
// Fire onsuccess asynchronously so callers that set handlers after
// the call still work.
queueMicrotask(() => {
req.onsuccess?.({ target: req } as any);
req.oncomplete?.({ target: req } as any);
});
return req as unknown as IDBRequest;
}
function noopObjectStore(): IDBObjectStore {
return {
get: () => noopRequest(undefined),
getAll: () => noopRequest([]),
put: () => noopRequest(undefined),
add: () => noopRequest(undefined),
delete: () => noopRequest(undefined),
clear: () => noopRequest(undefined),
count: () => noopRequest(0),
openCursor: () => noopRequest(null),
openKeyCursor: () => noopRequest(null),
getKey: () => noopRequest(undefined),
getAllKeys: () => noopRequest([]),
index: () => ({}) as any,
createIndex: () => ({}) as any,
deleteIndex: () => {},
name: "",
keyPath: "",
indexNames: { length: 0, contains: () => false, item: () => null } as any,
transaction: {} as any,
autoIncrement: false,
} as unknown as IDBObjectStore;
}
function noopTransaction(): IDBTransaction {
return {
objectStore: () => noopObjectStore(),
abort: () => {},
commit: () => {},
oncomplete: null,
onerror: null,
onabort: null,
db: {} as any,
durability: "default",
error: null,
mode: "readonly" as IDBTransactionMode,
objectStoreNames: { length: 0, contains: () => false, item: () => null } as any,
addEventListener: () => {},
removeEventListener: () => {},
dispatchEvent: () => true,
} as unknown as IDBTransaction;
}
function noopDB(): IDBDatabase {
return {
createObjectStore: () => noopObjectStore(),
deleteObjectStore: () => {},
transaction: () => noopTransaction(),
close: () => {},
name: "",
version: 1,
objectStoreNames: { length: 0, contains: () => false, item: () => null } as any,
onabort: null,
onclose: null,
onerror: null,
onversionchange: null,
addEventListener: () => {},
removeEventListener: () => {},
dispatchEvent: () => true,
} as unknown as IDBDatabase;
}
export async function register() {
// Polyfill localStorage for SSR/prerender — Chakra UI color mode
// accesses localStorage at module level which crashes static generation.
if (typeof globalThis.localStorage === "undefined") {
const store: Record<string, string> = {};
(globalThis as any).localStorage = {
getItem: (key: string) => store[key] ?? null,
setItem: (key: string, value: string) => { store[key] = String(value); },
removeItem: (key: string) => { delete store[key]; },
clear: () => { for (const k in store) delete store[k]; },
get length() { return Object.keys(store).length; },
key: (index: number) => Object.keys(store)[index] ?? null,
};
}
if (typeof globalThis.indexedDB === "undefined") {
(globalThis as any).indexedDB = {
open: (_name?: string, _version?: number) => noopRequest(noopDB()),
deleteDatabase: () => noopRequest(undefined),
databases: async () => [],
cmp: () => 0,
};
}
}