Skip to content

Add recipe for passing Aspire parameters to SPA frontends (Vite/React runtime config) #394

@maddymontaquila

Description

@maddymontaquila

Type of issue

Missing information / new recipe needed

Description

When using AddViteApp with Aspire parameters (e.g., Google OAuth Client ID), there's no documented pattern for getting parameter values into the SPA at runtime.

The problem: Vite bakes VITE_* environment variables into the JavaScript bundle at build time. Aspire sets environment variables at runtime. This means .WithEnvironment("VITE_GOOGLE_CLIENT_ID", parameter) on a Vite resource doesn't work — the value isn't available when Vite compiles the frontend.

What we had to figure out ourselves:

  1. Pass the parameter to the API project as a regular env var: .WithEnvironment("GOOGLE_CLIENT_ID", googleClientId)
  2. Create a config endpoint in the API: GET /api/config/google-client-id
  3. Have the SPA fetch the config value at mount time instead of reading import.meta.env

This is a universal problem for anyone using AddViteApp / AddNpmApp with parameters. A recipe or guide would save a lot of confusion.

Suggested docs location: The JavaScript hosting docs or the external parameters page would be natural homes.

Alternatively: A first-class API like AddViteApp(...).WithFrontendConfig("GOOGLE_CLIENT_ID", parameter) that auto-generates the config endpoint would be even better (that would be a dotnet/aspire feature request).

Page URL

https://learn.microsoft.com/en-us/dotnet/aspire/get-started/build-aspire-apps-with-nodejs

Content source URL

https://github.com/microsoft/aspire.dev/blob/main/docs/get-started/build-aspire-apps-with-nodejs.md

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions