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 reconnectTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
let reconnectAttempt = 0;
|
let reconnectAttempt = 0;
|
||||||
let patchVersion = 0;
|
let patchVersion = 0;
|
||||||
|
let useFallbackUrl = false;
|
||||||
|
|
||||||
function normalizeCityKey(city: string | null | undefined) {
|
function normalizeCityKey(city: string | null | undefined) {
|
||||||
return String(city || "").trim().toLowerCase();
|
return String(city || "").trim().toLowerCase();
|
||||||
@@ -50,8 +51,13 @@ function closeEventSource() {
|
|||||||
function connectSsePatches() {
|
function connectSsePatches() {
|
||||||
if (typeof window === "undefined" || eventSource) return;
|
if (typeof window === "undefined" || eventSource) return;
|
||||||
|
|
||||||
const url = resolveBackendApiUrl("/api/events");
|
let url = resolveBackendApiUrl("/api/events");
|
||||||
console.log("[SSE] Attempting to connect to:", url);
|
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 {
|
try {
|
||||||
closeEventSource();
|
closeEventSource();
|
||||||
@@ -74,10 +80,17 @@ function connectSsePatches() {
|
|||||||
eventSource.onerror = (err) => {
|
eventSource.onerror = (err) => {
|
||||||
console.error("[SSE] Connection error or stream closed:", err);
|
console.error("[SSE] Connection error or stream closed:", err);
|
||||||
closeEventSource();
|
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();
|
scheduleReconnect();
|
||||||
};
|
};
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("[SSE] Exception thrown while instantiating EventSource:", err);
|
console.error("[SSE] Exception thrown while instantiating EventSource:", err);
|
||||||
|
if (!useFallbackUrl && url !== "/api/events") {
|
||||||
|
useFallbackUrl = true;
|
||||||
|
}
|
||||||
scheduleReconnect();
|
scheduleReconnect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -37,7 +37,7 @@ app = FastAPI(title="PolyWeather Map", version="1.0")
|
|||||||
|
|
||||||
_cors_origins = os.getenv(
|
_cors_origins = os.getenv(
|
||||||
"WEB_CORS_ORIGINS",
|
"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(
|
app.add_middleware(
|
||||||
CORSMiddleware,
|
CORSMiddleware,
|
||||||
|
|||||||
Reference in New Issue
Block a user