Skip to content

Vite runtime varables add default values for backward compatibility - #166

Merged
iFurySt merged 14 commits into
AmoyLab:mainfrom
boston008:VITE_runtime_varables
Jun 27, 2025
Merged

iFurySt merged 14 commits into
AmoyLab:mainfrom
boston008:VITE_runtime_varables

Conversation

@boston008

@boston008 boston008 commented Jun 26, 2025 •

Copy link
Copy Markdown
Contributor

Vite runtime varables add default values for backward compatibility

Removed vite build time variable from allinone Dockerfile

Summary by Sourcery

Fetch runtime configuration at startup with default values for backward compatibility, replace build-time env variables with a dynamic window.RUNTIME_CONFIG, and refactor gateway URL construction and CORS headers; also remove obsolete VITE_* declarations from Dockerfiles

New Features:

  • Serve and fetch frontend runtime configuration from a new /api/runtime-config endpoint with default values and deep merging for backward compatibility
  • Show a loading screen component immediately before application initialization

Enhancements:

  • Refactor all direct import.meta.env usages to use window.RUNTIME_CONFIG and add a getGatewayBaseUrl() helper for unified URL resolution
  • Extend CORS and proxy configurations to include the mcp-protocol-version header across YAML, Go converter, and React components
  • Unify and enhance ImportMetaEnv typings to include built-in Vite variables and allow arbitrary keys

Build:

  • Remove build-time VITE_* ARG and ENV declarations from multi and all-in-one Dockerfiles

Chores:

  • Add TypeScript modules for static asset imports and JSX intrinsic element declarations

Kris Rane and others added 5 commits June 24, 2025 18:52
VITE_runtime_var and mcp-protocol-version header
Avoid double slash

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
Consider adding a loading indicator or fallback UI while fetchRuntimeConfig is in flight to avoid showing a blank screen before the app renders.

Remove or conditionally suppress the console.log calls in fetchRuntimeConfig so debug output doesn’t clutter production logs.

Instead of declaring window.RUNTIME_CONFIG as any, define and use a proper TypeScript interface for the runtime config to improve type safety.
Add default values for backward  compatibility
Remove build parameter from all in one dockerfile
@sourcery-ai

sourcery-ai Bot commented Jun 26, 2025 •

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Introduces a client-side runtime configuration system with defaults for backward compatibility, removes build-time VITE variables from Dockerfiles, and updates various components and services to consume the new runtime config while adding header support and static asset typings.

Sequence diagram for frontend runtime config loading and app rendering

sequenceDiagram
    actor User
    participant Browser
    participant FrontendApp as React App
    participant API as /api/runtime-config

    User->>Browser: Load web page
    Browser->>FrontendApp: Execute main.tsx
    FrontendApp->>API: GET /api/runtime-config
    API-->>FrontendApp: Return runtime config JSON
    FrontendApp->>Browser: Merge config with defaults
    FrontendApp->>Browser: Set window.RUNTIME_CONFIG
    FrontendApp->>Browser: Render main React app
    Browser->>User: Show application UI
Loading

Class diagram for new and updated runtime config types

classDiagram
    class RuntimeConfig {
      +string apiBaseUrl
      +boolean debugMode
      +string version
      +features: Features
      +[key: string]: any
    }
    class Features {
      +boolean enableExperimental
      +[key: string]: boolean
    }
    class Window {
      +RuntimeConfig RUNTIME_CONFIG
    }
    RuntimeConfig o-- Features
    Window --> RuntimeConfig : RUNTIME_CONFIG
Loading

Class diagram for new LoadingScreen component

classDiagram
    class LoadingScreen {
      <<React.FC>>
      +render(): JSX.Element
    }
Loading

Class diagram for static asset type declarations

classDiagram
    class PNGModule {
      +string default
    }
    class JPGModule {
      +string default
    }
    class JPEGModule {
      +string default
    }
    class SVGModule {
      +string default
    }
    class GIFModule {
      +string default
    }
Loading

Class diagram for updated ImportMetaEnv type

classDiagram
    class ImportMetaEnv {
      +string MODE
      +boolean DEV
      +boolean PROD
      +boolean SSR
      +string VITE_MCP_GATEWAY_BASE_URL
      +string VITE_API_BASE_URL
      +string VITE_WS_BASE_URL
      +[key: string]: string | boolean | undefined
    }
    class ImportMeta {
      +ImportMetaEnv env
    }
    ImportMeta --> ImportMetaEnv : env
Loading

File-Level Changes

Change Details Files
Initialize app after fetching runtime config with loading screen and default values
  • Render LoadingScreen immediately
  • Define RuntimeConfig interface and defaultRuntimeConfig
  • Implement fetchRuntimeConfig to merge defaults with API data
  • Defer final ReactDOM.render until after config load
web/src/main.tsx
Replace import-time env access with runtime config in services and app
  • Use window.RUNTIME_CONFIG for API and gateway base URLs
  • Update axios baseURL to runtime config
  • Switch App basename to runtime config
web/src/services/api.ts
web/src/services/mcp.ts
web/src/App.tsx
Encapsulate gateway URL logic and adjust view mode default
  • Add getGatewayBaseUrl helper using runtime config or origin
  • Replace repetitive import.meta.env logic with helper
  • Change default viewMode from 'card' to 'table'
web/src/pages/gateway/gateway-manager.tsx
Expose experimental protocol header in CORS and UI router configs
  • Add 'mcp-protocol-version' to allowHeaders and exposeHeaders across YAML configs
  • Include header option in RouterConfig component datalists
configs/proxy-mcp-exp.yaml
configs/mcp-gateway.yaml
configs/proxy-mock-server.yaml
web/src/pages/gateway/components/RouterConfig.tsx
pkg/openapi/converter.go
Remove VITE_* ARGs and ENV settings from multi/allinone Dockerfiles
  • Delete build-time ARG declarations
  • Remove ENV exports for VITE variables
deploy/docker/multi/web/Dockerfile
deploy/docker/allinone/Dockerfile
Add backend endpoint to serve runtime configuration JSON
  • Register /api/runtime-config route in apiserver
  • Implement HandleRuntimeConfig to read env vars and serve JSON
cmd/apiserver/main.go
internal/apiserver/handler/runtime_config.go
Enhance TypeScript ambient typing support
  • Extend ImportMetaEnv with built-in flags and index signature
  • Add static asset modules for common image types
  • Allow arbitrary JSX intrinsic elements
web/src/env.d.ts
web/src/types/images.d.ts
web/src/types/react-app.d.ts
Introduce LoadingScreen component
  • Create LoadingScreen with logo and spinner
  • Use Heroui Spinner and flex layout
web/src/components/LoadingScreen.tsx

Possibly linked issues

  • #0: PR implements runtime VITE config via API endpoint and adds mcp-protocol-version header as described in the issue.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @boston008 - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments

### Comment 1
<location> `web/src/main.tsx:47` </location>
<code_context>
-    </HeroUIProvider>
-  </React.StrictMode>,
-);
+// Show loading screen immediately
+const rootElement = document.getElementById("root");
+if (rootElement) {
+  ReactDOM.createRoot(rootElement).render(
+    <React.StrictMode>
+      <HeroUIProvider>
+        <LoadingScreen />
+      </HeroUIProvider>
+    </React.StrictMode>
+  );
+}
</code_context>

<issue_to_address>
Potential for double React root creation on the same DOM node.

Creating multiple React roots on the same DOM node can cause warnings or unexpected behavior. Use a single root and conditionally render the loading screen or main app to avoid this issue.
</issue_to_address>

### Comment 2
<location> `web/src/services/api.ts:14` </location>
<code_context>

 // Create an axios instance with default config
 const api = axios.create({
-  baseURL: import.meta.env.VITE_API_URL || '/api',
+  baseURL: window.RUNTIME_CONFIG?.VITE_API_BASE_URL || '/api',	
   timeout: 10000,
   headers: {
</code_context>

<issue_to_address>
Potential typo: VITE_API_URL should likely be VITE_API_BASE_URL for consistency.

The runtime config and Dockerfiles use VITE_API_BASE_URL, so using a different variable here may cause misconfiguration unless intentional.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
const api = axios.create({
  baseURL: window.RUNTIME_CONFIG?.VITE_API_BASE_URL || '/api',	
  timeout: 10000,
  headers: {
=======
const api = axios.create({
  baseURL: window.RUNTIME_CONFIG?.VITE_API_BASE_URL || import.meta.env.VITE_API_BASE_URL || '/api',
  timeout: 10000,
  headers: {
>>>>>>> REPLACE

</suggested_fix>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread web/src/main.tsx
Comment on lines +47 to +55
// Show loading screen immediately
const rootElement = document.getElementById("root");
if (rootElement) {
ReactDOM.createRoot(rootElement).render(
<React.StrictMode>
<HeroUIProvider>
<LoadingScreen />
</HeroUIProvider>
</React.StrictMode>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Potential for double React root creation on the same DOM node.

Creating multiple React roots on the same DOM node can cause warnings or unexpected behavior. Use a single root and conditionally render the loading screen or main app to avoid this issue.

Comment thread web/src/services/api.ts Resolved
@iFurySt

iFurySt commented Jun 26, 2025

Copy link
Copy Markdown
Member

@boston008 can you pls handle the conflicts?

@boston008

Copy link
Copy Markdown
Contributor Author

@boston008 can you pls handle the conflicts?

Done

@iFurySt
iFurySt merged commit c6271ea into AmoyLab:main Jun 27, 2025
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.

2 participants