Skip to content

Conversation

jaiganeshs21
Copy link

Summary

  • Fixes hardcoded localhost:8000 API URLs that caused upload failures in self-hosted environments
  • Implements dynamic hostname detection to automatically use the browser's current hostname
  • Maintains backward compatibility with localhost development setups

Changes Made

  • contexts/morphik-context.tsx: Dynamic default API base URL
  • components/MorphikUI.tsx: Dynamic apiBaseUrl prop default
  • components/pdf/PDFViewer.tsx: Dynamic API endpoints for PDF chat (2 locations)
  • lib/utils.ts: Dynamic default in getApiBaseUrlFromUri utility
  • README.md: Updated documentation to reflect dynamic behavior

Technical Details

// Before: Hardcoded localhost
const DEFAULT_API_BASE_URL = "http://localhost:8000";

// After: Dynamic hostname with SSR fallback
const DEFAULT_API_BASE_URL = typeof window !== 'undefined'
  ? `${window.location.protocol}//${window.location.hostname}:8000`
  : "http://localhost:8000";

Resolves #208

@CLAassistant
Copy link

CLAassistant commented Jul 27, 2025

CLA assistant check
All committers have signed the CLA.

Copy link

jazzberry-ai bot commented Jul 27, 2025

Bug Report

Name: NEXT_PUBLIC_API_BASE_URL environment variable overrides dynamic hostname

Severity: Medium

Example test case:

  1. Set NEXT_PUBLIC_API_BASE_URL environment variable to "http://localhost:8000".
  2. Access the application from a remote machine using the machine's hostname or IP address.
  3. Open a PDF document.
  4. Observe that the API calls related to the PDF viewer (e.g., chat) are being made to localhost instead of the remote machine's hostname/IP.

Description:
The PDFViewer component prioritizes the NEXT_PUBLIC_API_BASE_URL environment variable over the dynamically generated apiBaseUrl based on the current hostname. This can lead to issues when the application is accessed remotely, as the API calls might still be directed to localhost if the environment variable is set, even though the user is accessing the application from a different machine. The intended behavior should be to use the dynamic hostname when available, falling back to NEXT_PUBLIC_API_BASE_URL only when the hostname cannot be determined (e.g., during SSR or in environments without a window object).

Comments? Email us.

@jaiganeshs21
Copy link
Author

@Adityav369 Can you please review this PR?

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.

uploading document fails
2 participants