Reduce scan terminal origin load
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { proxyBackendJsonGet } from "@/lib/api-proxy";
|
import { proxyBackendJsonGet } from "@/lib/api-proxy";
|
||||||
import { buildForceRefreshProxyCachePolicy } from "@/lib/proxy-cache-policy";
|
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 API_BASE = process.env.POLYWEATHER_API_BASE_URL;
|
||||||
const SCAN_TERMINAL_PROXY_TIMEOUT_MS = Number(
|
const SCAN_TERMINAL_PROXY_TIMEOUT_MS = Number(
|
||||||
@@ -41,7 +42,10 @@ export async function GET(req: NextRequest) {
|
|||||||
if (tradingRegion != null && tradingRegion !== "") {
|
if (tradingRegion != null && tradingRegion !== "") {
|
||||||
params.set("region", 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()}`;
|
const url = `${API_BASE}/api/scan/terminal?${params.toString()}`;
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,37 @@ export function runTests() {
|
|||||||
const scanForced = buildForceRefreshProxyCachePolicy("true", 10);
|
const scanForced = buildForceRefreshProxyCachePolicy("true", 10);
|
||||||
assert.equal(scanForced.fetchMode, "no-store");
|
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(
|
const overviewProxySource = fs.readFileSync(
|
||||||
path.join(
|
path.join(
|
||||||
process.cwd(),
|
process.cwd(),
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
import {
|
import {
|
||||||
buildBrowserBackendHeaders,
|
buildBrowserBackendHeaders,
|
||||||
fetchBackendApi,
|
fetchBackendApi,
|
||||||
|
hasDirectBackendApiBaseUrl,
|
||||||
} from "@/lib/backend-api";
|
} from "@/lib/backend-api";
|
||||||
import { DASHBOARD_REFRESH_POLICY_MS } from "@/lib/refresh-policy";
|
import { DASHBOARD_REFRESH_POLICY_MS } from "@/lib/refresh-policy";
|
||||||
import type {
|
import type {
|
||||||
@@ -144,11 +145,14 @@ async function getTerminal({
|
|||||||
if (forceRefresh) {
|
if (forceRefresh) {
|
||||||
params.set("_ts", String(Date.now()));
|
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<ScanTerminalResponse>(
|
return readJsonOrThrow<ScanTerminalResponse>(
|
||||||
`/api/scan/terminal?${params.toString()}`,
|
`/api/scan/terminal?${params.toString()}`,
|
||||||
{
|
{
|
||||||
cache: "no-store",
|
cache: forceRefresh || directBackend ? "no-store" : "default",
|
||||||
headers,
|
headers,
|
||||||
signal,
|
signal,
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user