Skip to content

add support for VITE_API_URL environment variable with sensible defaults #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

arioko
Copy link

@arioko arioko commented Jun 25, 2025

This update introduces a flexible mechanism to configure the backend API URL used by the frontend, removing the need to hardcode or manually edit the API endpoint within App.tsx. The main improvements are as follows:

  • Environment Variable Integration:
    The frontend now reads the API URL from an environment variable (VITE_API_URL) at build time. This variable can be defined in a .env file, passed as a build argument in Docker, or set via deployment configuration, making it easy to change the API endpoint for different environments (development, staging, production) without modifying the source code.

  • Build Process Adaptation:
    The Dockerfile and build scripts have been updated to accept the VITE_API_URL argument and expose it as an environment variable during the frontend build process. This ensures that the correct API URL is embedded into the frontend bundle according to the environment or deployment context.

  • Docker Compose Integration:
    The docker-compose.yaml file has been updated to support building the entire project directly with Docker Compose. It now passes the VITE_API_URL environment variable as a build argument, enabling seamless configuration of the API endpoint during the build process without manual intervention.

  • Fallback Logic:
    In the application code, the API URL is now resolved using the following logic:

    • Use the value of import.meta.env.VITE_API_URL if defined and non-empty.
    • Otherwise, fallback to sensible defaults based on the build environment (e.g., localhost for development, a production URL otherwise).
      This allows for robust configuration with minimal manual intervention.
  • No Need to Edit Source Files:
    With this approach, updating the API endpoint only requires changing the environment variable or build argument, not the application source code. This streamlines deployments and reduces the risk of manual errors.

In summary:
The frontend’s API URL is now fully configurable via environment variables and can be set per environment using Docker Compose. The project can be built directly from Docker Compose with the appropriate API endpoint, supporting per-environment deployments and simplifying the process of targeting different backend endpoints—all without requiring changes to App.tsx or other source files.

Copy link

google-cla bot commented Jun 25, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@arioko
Copy link
Author

arioko commented Jun 25, 2025

The CLA has already been confirmed for me. Regards.

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.

1 participant