feat: implement SSE patch synchronization with frontend hooks and backend infrastructure
This commit is contained in:
@@ -20,6 +20,7 @@ let eventSource: EventSource | null = null;
|
||||
let reconnectTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
let reconnectAttempt = 0;
|
||||
let patchVersion = 0;
|
||||
let useFallbackUrl = false;
|
||||
|
||||
function normalizeCityKey(city: string | null | undefined) {
|
||||
return String(city || "").trim().toLowerCase();
|
||||
@@ -50,8 +51,13 @@ function closeEventSource() {
|
||||
function connectSsePatches() {
|
||||
if (typeof window === "undefined" || eventSource) return;
|
||||
|
||||
const url = resolveBackendApiUrl("/api/events");
|
||||
console.log("[SSE] Attempting to connect to:", url);
|
||||
let url = resolveBackendApiUrl("/api/events");
|
||||
if (useFallbackUrl) {
|
||||
url = "/api/events";
|
||||
console.log("[SSE] Falling back to same-origin BFF proxy URL:", url);
|
||||
} else {
|
||||
console.log("[SSE] Attempting to connect to direct URL:", url);
|
||||
}
|
||||
|
||||
try {
|
||||
closeEventSource();
|
||||
@@ -74,10 +80,17 @@ function connectSsePatches() {
|
||||
eventSource.onerror = (err) => {
|
||||
console.error("[SSE] Connection error or stream closed:", err);
|
||||
closeEventSource();
|
||||
if (!useFallbackUrl && url !== "/api/events") {
|
||||
console.warn("[SSE] Direct connection failed. Switching to same-origin BFF proxy fallback for next attempt.");
|
||||
useFallbackUrl = true;
|
||||
}
|
||||
scheduleReconnect();
|
||||
};
|
||||
} catch (err) {
|
||||
console.error("[SSE] Exception thrown while instantiating EventSource:", err);
|
||||
if (!useFallbackUrl && url !== "/api/events") {
|
||||
useFallbackUrl = true;
|
||||
}
|
||||
scheduleReconnect();
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -37,7 +37,7 @@ app = FastAPI(title="PolyWeather Map", version="1.0")
|
||||
|
||||
_cors_origins = os.getenv(
|
||||
"WEB_CORS_ORIGINS",
|
||||
"http://localhost:3000,http://127.0.0.1:3000,https://polyweather-pro.vercel.app",
|
||||
"http://localhost:3000,http://127.0.0.1:3000,https://polyweather-pro.vercel.app,https://polyweather.top,https://api.polyweather.top",
|
||||
)
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
|
||||
Reference in New Issue
Block a user