diff --git a/frontend/app/api/scan/terminal/route.ts b/frontend/app/api/scan/terminal/route.ts index a94bc491..719d10e7 100644 --- a/frontend/app/api/scan/terminal/route.ts +++ b/frontend/app/api/scan/terminal/route.ts @@ -1,6 +1,7 @@ import { NextRequest, NextResponse } from "next/server"; import { proxyBackendJsonGet } from "@/lib/api-proxy"; import { buildForceRefreshProxyCachePolicy } from "@/lib/proxy-cache-policy"; +import { DASHBOARD_REFRESH_POLICY_SEC } from "@/lib/refresh-policy"; const API_BASE = process.env.POLYWEATHER_API_BASE_URL; const SCAN_TERMINAL_PROXY_TIMEOUT_MS = Number( @@ -41,7 +42,10 @@ export async function GET(req: NextRequest) { if (tradingRegion != null && tradingRegion !== "") { params.set("region", tradingRegion); } - const cachePolicy = buildForceRefreshProxyCachePolicy(forceRefresh, 10); + const cachePolicy = buildForceRefreshProxyCachePolicy( + forceRefresh, + DASHBOARD_REFRESH_POLICY_SEC.scanRows, + ); const url = `${API_BASE}/api/scan/terminal?${params.toString()}`; diff --git a/frontend/components/dashboard/scan-terminal/__tests__/proxyCachePolicy.test.ts b/frontend/components/dashboard/scan-terminal/__tests__/proxyCachePolicy.test.ts index 44306c24..8a074cb2 100644 --- a/frontend/components/dashboard/scan-terminal/__tests__/proxyCachePolicy.test.ts +++ b/frontend/components/dashboard/scan-terminal/__tests__/proxyCachePolicy.test.ts @@ -25,6 +25,37 @@ export function runTests() { const scanForced = buildForceRefreshProxyCachePolicy("true", 10); assert.equal(scanForced.fetchMode, "no-store"); + const scanTerminalProxySource = fs.readFileSync( + path.join(process.cwd(), "app", "api", "scan", "terminal", "route.ts"), + "utf8", + ); + assert.match( + scanTerminalProxySource, + /DASHBOARD_REFRESH_POLICY_SEC\.scanRows/, + "scan terminal proxy cache TTL should match the dashboard scan refresh cadence instead of a short literal TTL", + ); + assert.doesNotMatch( + scanTerminalProxySource, + /buildForceRefreshProxyCachePolicy\(forceRefresh,\s*10\)/, + "scan terminal proxy must not use the old 10 second edge cache because it over-drives the slow scan endpoint", + ); + + const scanTerminalClientSource = fs.readFileSync( + path.join( + process.cwd(), + "components", + "dashboard", + "scan-terminal", + "scan-terminal-client.ts", + ), + "utf8", + ); + assert.match( + scanTerminalClientSource, + /hasDirectBackendApiBaseUrl/, + "public scan terminal requests should only attach user auth in direct-backend mode so CDN caches can be shared through the Next proxy", + ); + const overviewProxySource = fs.readFileSync( path.join( process.cwd(), diff --git a/frontend/components/dashboard/scan-terminal/scan-terminal-client.ts b/frontend/components/dashboard/scan-terminal/scan-terminal-client.ts index fae501cb..c46bfcf9 100644 --- a/frontend/components/dashboard/scan-terminal/scan-terminal-client.ts +++ b/frontend/components/dashboard/scan-terminal/scan-terminal-client.ts @@ -3,6 +3,7 @@ import { buildBrowserBackendHeaders, fetchBackendApi, + hasDirectBackendApiBaseUrl, } from "@/lib/backend-api"; import { DASHBOARD_REFRESH_POLICY_MS } from "@/lib/refresh-policy"; import type { @@ -144,11 +145,14 @@ async function getTerminal({ if (forceRefresh) { params.set("_ts", String(Date.now())); } - const headers = await buildBrowserBackendHeaders({ Accept: "application/json" }); + const directBackend = hasDirectBackendApiBaseUrl(); + const headers = directBackend + ? await buildBrowserBackendHeaders({ Accept: "application/json" }) + : new Headers({ Accept: "application/json" }); return readJsonOrThrow( `/api/scan/terminal?${params.toString()}`, { - cache: "no-store", + cache: forceRefresh || directBackend ? "no-store" : "default", headers, signal, },