Skip to content

Commit 1ad9d7f

Browse files
committed
Make CDA local vite proxy catch any cda endpoint and ensure if a FQDN is included in the path that only the pathname root is set for the regex in the proxy
1 parent 86d4399 commit 1ad9d7f

1 file changed

Lines changed: 30 additions & 19 deletions

File tree

cda-gui/vite.config.js

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,49 @@
11
import { defineConfig, loadEnv } from "vite";
22
import react from "@vitejs/plugin-react";
33

4+
const normalizePath = (value, fallback = "/cwms-data") => {
5+
/* Returns a normalized path segment from a URL or path string. */
6+
7+
const candidate = value || fallback;
8+
let pathname = candidate;
9+
10+
try {
11+
pathname = new URL(candidate).pathname;
12+
} catch {
13+
// Use as-is when candidate is already a path segment.
14+
}
15+
// remove trailing slashes and ensure leading slash
16+
if (!pathname.startsWith("/")) pathname = `/${pathname}`;
17+
return pathname.replace(/\/+$/, "") || "/";
18+
};
19+
420
// https://vitejs.dev/config/
521
export default defineConfig(({ mode }) => {
622
const env = loadEnv(mode, ".", "");
723
const BASE_PATH = env.VITE_BASE_PATH || "/cwms-data";
8-
const CDA_PATH = env.VITE_CDA_URL || BASE_PATH;
24+
const CDA_PATH = normalizePath(env.VITE_CDA_URL, BASE_PATH);
925
const proxyTarget = env.VITE_CDA_PROXY_TARGET || "https://cwms-data.usace.army.mil";
1026

1127
return {
1228
base: BASE_PATH,
1329
plugins: [react()],
1430
server: {
1531
proxy: {
16-
[`^${CDA_PATH}/timeseries/.*`]: {
17-
target: proxyTarget,
18-
changeOrigin: true,
19-
secure: false,
20-
},
21-
[`^${CDA_PATH}/catalog/.*`]: {
32+
[`^${CDA_PATH}/.*`]: {
2233
target: proxyTarget,
2334
changeOrigin: true,
2435
secure: false,
2536
},
2637
},
27-
},
28-
experimental: {
29-
renderBuiltUrl(filename, { hostType }) {
30-
if (hostType === "js" || hostType === "css") {
31-
return { runtime: `window.__toCdnUrl(${JSON.stringify(filename)})` };
32-
} else {
33-
return { relative: true };
34-
}
35-
},
36-
},
37-
};
38-
});
38+
},
39+
experimental: {
40+
renderBuiltUrl(filename, { hostType }) {
41+
if (hostType === "js" || hostType === "css") {
42+
return { runtime: `window.__toCdnUrl(${JSON.stringify(filename)})` };
43+
} else {
44+
return { relative: true };
45+
}
46+
},
47+
},
48+
};
49+
});

0 commit comments

Comments
 (0)