-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch-supabase.js
More file actions
70 lines (63 loc) · 2.27 KB
/
Copy pathpatch-supabase.js
File metadata and controls
70 lines (63 loc) · 2.27 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
// This script patches the Supabase URL to use the correct endpoint
(function() {
// Function to check if direct Supabase access is available
function checkDirectAccess() {
return new Promise((resolve) => {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
resolve(xhr.status !== 0 && xhr.status < 500);
}
};
xhr.open('GET', 'http://localhost:8000/auth/v1/health', true);
xhr.timeout = 2000; // 2 seconds timeout
xhr.send();
});
}
// Function to patch the Supabase client with the provided URL
function patchSupabaseClient(supabaseUrl) {
if (window.supabase && window.supabase.createClient) {
const originalCreateClient = window.supabase.createClient;
window.supabase.createClient = function(url, key, options) {
console.log('Patching Supabase client, replacing URL:', url, '->', supabaseUrl);
return originalCreateClient(supabaseUrl, key, options);
};
console.log('Supabase client patched successfully with URL:', supabaseUrl);
return true;
}
return false;
}
// Determine the best Supabase URL to use
async function setupSupabaseUrl() {
// Try direct access first
const directAccessWorks = await checkDirectAccess();
// Choose the appropriate URL
let supabaseUrl;
if (directAccessWorks) {
supabaseUrl = 'http://localhost:8000';
console.log('Direct Supabase access available, using:', supabaseUrl);
} else {
supabaseUrl = window.location.origin + '/supabase';
console.log('Using proxied Supabase access:', supabaseUrl);
}
// Store the URL globally
window.RUNTIME_SUPABASE_URL = supabaseUrl;
// Try to patch immediately
if (!patchSupabaseClient(supabaseUrl)) {
// If not available yet, try again on window load
window.addEventListener('load', function() {
patchSupabaseClient(supabaseUrl);
});
// Also try periodically
let attempts = 0;
const interval = setInterval(function() {
if (patchSupabaseClient(supabaseUrl) || attempts > 50) {
clearInterval(interval);
}
attempts++;
}, 100);
}
}
// Start the setup process
setupSupabaseUrl();
})();