Lê Phi Nam commited on
Commit
68af9ee
·
1 Parent(s): 3a9c68b

Fix API URL: use bun script for reliable placeholder replacement

Browse files
Files changed (1) hide show
  1. Dockerfile +28 -8
Dockerfile CHANGED
@@ -14,15 +14,35 @@ RUN bun install --frozen-lockfile
14
  # Build static files (output lands in /app/frontend/dist)
15
  COPY frontend/ ./frontend/
16
 
17
- # Build with a unique placeholder for VITE_API_URL. Vite statically replaces
18
- # import.meta.env.VITE_API_URL with this literal string at compile time.
19
- # After build, we swap the placeholder for `window.location.origin` so the
20
- # frontend dynamically resolves the API base from the browser's current URL.
21
- # This makes it work on any deployment (HF Spaces, self-hosted Docker, etc.)
22
- # without hardcoding hosts.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  RUN VITE_API_URL="__OMNIVOICE_RUNTIME_ORIGIN__" bun run --cwd frontend build && \
24
- find /app/frontend/dist -name '*.js' -exec \
25
- sed -i 's|"__OMNIVOICE_RUNTIME_ORIGIN__"|window.location.origin|g' {} +
26
 
27
  # ==========================================
28
  # Runtime Stage: Python & PyTorch Backend
 
14
  # Build static files (output lands in /app/frontend/dist)
15
  COPY frontend/ ./frontend/
16
 
17
+ # Write a patcher script that replaces the API URL placeholder with
18
+ # window.location.origin in the compiled JS output. Using bun to run
19
+ # the script ensures it works on Alpine (no sed/find compatibility issues).
20
+ RUN cat > /tmp/patch_api.mjs << 'PATCHEOF'
21
+ import { readdirSync, statSync, readFileSync, writeFileSync } from "fs";
22
+ import { join } from "path";
23
+
24
+ function walk(dir) {
25
+ for (const f of readdirSync(dir)) {
26
+ const p = join(dir, f);
27
+ if (statSync(p).isDirectory()) walk(p);
28
+ else if (f.endsWith(".js")) {
29
+ let c = readFileSync(p, "utf8");
30
+ if (c.includes("__OMNIVOICE_RUNTIME_ORIGIN__")) {
31
+ c = c.replaceAll('"__OMNIVOICE_RUNTIME_ORIGIN__"', "window.location.origin");
32
+ c = c.replaceAll("'__OMNIVOICE_RUNTIME_ORIGIN__'", "window.location.origin");
33
+ c = c.replaceAll("__OMNIVOICE_RUNTIME_ORIGIN__", "window.location.origin");
34
+ writeFileSync(p, c);
35
+ console.log("Patched:", p);
36
+ }
37
+ }
38
+ }
39
+ }
40
+ walk("/app/frontend/dist");
41
+ PATCHEOF
42
+
43
+ # Build with placeholder, then patch compiled JS to use window.location.origin
44
  RUN VITE_API_URL="__OMNIVOICE_RUNTIME_ORIGIN__" bun run --cwd frontend build && \
45
+ bun /tmp/patch_api.mjs
 
46
 
47
  # ==========================================
48
  # Runtime Stage: Python & PyTorch Backend