-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolyfill.js
40 lines (35 loc) · 1.17 KB
/
polyfill.js
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
// Function to check if the code is running in a browser environment
function isRunningOnClient() {
// Checks if 'window' is defined, which indicates a browser environment
if (typeof window !== "undefined") {
return globalThis === window; // Validates that the global object is 'window'
}
return false; // Returns false if not running in a browser
}
// Only apply polyfills if running in a server environment
if (!isRunningOnClient()) {
// Polyfill for queueMicrotask if it doesn't exist
if (!globalThis.queueMicrotask) {
globalThis.queueMicrotask = function queueMicrotaskPolyfil(callback) {
// Uses Promise.resolve().then() to simulate queueMicrotask behavior
Promise.resolve().then(callback);
};
}
// Polyfill for Response object if it doesn't exist
if (!globalThis.Response) {
globalThis.Response = class Response {
// Static method to simulate Response.error()
static error() {
return {};
}
// Static method to simulate Response.json()
static json() {
return "{}";
}
// Static method to simulate Response.redirect()
static redirect() {
return {};
}
};
}
}