|
@@ -1,5 +1,16 @@
|
|
|
-// API_BASE now uses relative path for proxy, or falls back to direct API URL
|
|
|
|
|
-export const API_BASE = process.env.NEXT_PUBLIC_WATCH_FINISHED_API || "/api";
|
|
|
|
|
|
|
+// Prefer explicit API host when provided, but avoid leaking localhost to remote clients
|
|
|
|
|
+const envApi = process.env.NEXT_PUBLIC_WATCH_FINISHED_API;
|
|
|
|
|
+const isBrowser = typeof window !== "undefined";
|
|
|
|
|
+const isLocalEnv =
|
|
|
|
|
+ envApi?.includes("localhost") || envApi?.includes("127.0.0.1");
|
|
|
|
|
+const isLocalHost = !isBrowser
|
|
|
|
|
+ ? true
|
|
|
|
|
+ : window.location.hostname === "localhost" ||
|
|
|
|
|
+ window.location.hostname === "127.0.0.1";
|
|
|
|
|
+
|
|
|
|
|
+// If env points at localhost but the page is served from a non-local host, fall back to relative /api
|
|
|
|
|
+export const API_BASE =
|
|
|
|
|
+ envApi && (!isLocalEnv || isLocalHost) ? envApi : "/api";
|
|
|
|
|
|
|
|
function buildUrl(path: string, params?: any) {
|
|
function buildUrl(path: string, params?: any) {
|
|
|
// If API_BASE starts with http, use it as absolute URL
|
|
// If API_BASE starts with http, use it as absolute URL
|
|
@@ -9,7 +20,10 @@ function buildUrl(path: string, params?: any) {
|
|
|
|
|
|
|
|
const url = base
|
|
const url = base
|
|
|
? new URL(fullPath, base)
|
|
? new URL(fullPath, base)
|
|
|
- : new URL(fullPath, window.location.origin);
|
|
|
|
|
|
|
+ : new URL(
|
|
|
|
|
+ fullPath,
|
|
|
|
|
+ isBrowser ? window.location.origin : "http://localhost:3000"
|
|
|
|
|
+ );
|
|
|
if (params && typeof params === "object") {
|
|
if (params && typeof params === "object") {
|
|
|
Object.entries(params).forEach(([k, v]) => {
|
|
Object.entries(params).forEach(([k, v]) => {
|
|
|
if (v !== undefined && v !== null) url.searchParams.append(k, String(v));
|
|
if (v !== undefined && v !== null) url.searchParams.append(k, String(v));
|