Skip to content

dynamic baseurl#6

Open
DocArmoryTech wants to merge 2 commits intoMISP:mainfrom
DocArmoryTech:dyn-base
Open

dynamic baseurl#6
DocArmoryTech wants to merge 2 commits intoMISP:mainfrom
DocArmoryTech:dyn-base

Conversation

@DocArmoryTech
Copy link
Copy Markdown

Use window.location.origin for dynamic API base URL in frontend

This commit fixes hardcoded http://127.0.0.1:5001 URLs in the frontend, which caused client browsers to attempt API calls to their local machine instead of the deployed server (e.g., http://command.example.org:5001). The changes update frontend to use window.location.origin for the API base URL, ensuring requests target the correct server in production.

Changes:

  • Modified web/src/api/apiConfig.js to set baseurl using typeof window !== 'undefined' ? window.location.origin : 'http://127.0.0.1:5001', ensuring compatibility with non-browser environments (e.g., during vue-cli-service build).
  • Updated web/src/components/ui/elements/GrafanaRenderedGraph.vue to use the same dynamic base URL in the getImageURL computed property.
  • Retained fallback to http://127.0.0.1:5001 for local development and build-time safety.

These changes enable reliable API and monitoring image requests in hosted deployments without requiring environment variables or reverse proxies, while maintaining build compatibility.

fixes: #5

Use window.location.origin for dynamic API base URL in frontend

This commit fixes hardcoded `http://127.0.0.1:5001` URLs in the frontend, which caused client browsers to attempt API calls to their local machine instead of the deployed server (e.g., `http://command.example.org:5001`). The changes update frontend to use `window.location.origin` for the API base URL, ensuring requests target the correct server in production.

Changes:
- Modified `web/src/api/apiConfig.js` to set `baseurl` using `typeof window !== 'undefined' ? window.location.origin : 'http://127.0.0.1:5001'`, ensuring compatibility with non-browser environments (e.g., during `vue-cli-service build`).
- Updated `web/src/components/ui/elements/GrafanaRenderedGraph.vue` to use the same dynamic base URL in the `getImageURL` computed property.
- Retained fallback to `http://127.0.0.1:5001` for local development and build-time safety.

These changes enable reliable API and monitoring image requests in hosted deployments without requiring environment variables or reverse proxies, while maintaining build compatibility.
This update corrects a logic error in how the frontend determines the `baseurl` for API calls.

**Original line:**

```js
const baseurl = typeof window !== 'undefined' ? window.location.origin || 'http://127.0.0.1:5001';
```

While this appears to provide a fallback, the fallback is only applied if `window.location.origin` is falsy — **but `window` is accessed regardless**, which can lead to errors in non-browser environments (e.g., SSR, tests) or unexpected behavior if `origin` is empty or malformed.

**Corrected version:**

```js
const baseurl = window?.location?.origin || 'http://127.0.0.1:5001';
```

This uses optional chaining to safely access `window.location.origin` only if `window` and `location` exist, and reliably falls back to `'http://127.0.0.1:5001'` otherwise.
@adulau adulau requested a review from mokaddem June 28, 2025 08:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hardcoded backend URL breaks hosted deployment

1 participant