diff --git a/README.md b/README.md index ed927ed38..adfd841b2 100644 --- a/README.md +++ b/README.md @@ -42,24 +42,54 @@ There are several ways to deploy the UI publicly. Here are two common options: 1. Fork this repository to create your own version 2. Configure your Vercel account to deploy the forked repository + ## Configuration -There's two ways to configure the UI: +There are two ways to configure the UI: **at build time** and **at run time**. -**Create a `local.json` config file** +### Build-Time Configuration -1. Copy [default.json][def] to a new file in the same folder called +This method is recommended when deploying to static hosting platforms such +as Vercel. + +#### Option 1: Create a `local.json` file + +1. Copy [`default.json`][def] to a new file in the same directory called `local.json`. -2. Modify the properties in `local.json` to your preference. +2. Modify the properties in `local.json` to suit your setup. -**Environment variables** +#### Option 2: Use environment variables -1. Check [custom-environment-variables.json][env] for all available environment - variables. -2. Set the desired environment variables when building the UI. +1. Refer to [`custom-environment-variables.json`][env] for a list of supported + environment variables. +2. Set the desired variables in your environment before building the UI. + +> For advanced configuration options, refer to the [`node-config`][nco] +> documentation. + + +### Run-Time Configuration + +This method is useful when the app is distributed as a precompiled static +JS/HTML bundle, such as when installed via a package manager. + +You can build the app in a mode that loads configuration dynamically from the +server it's deployed to, instead of bundling it at build time. + +To enable this behavior, set the environment variable `VITE_RUNTIME_CONFIG=true` +during the build: + +```bash +VITE_RUNTIME_CONFIG=true npm run build +``` + +This will inject a blocking script into the `index.html` that attempts to load +the configuration from a pre-defined location (`/config.json`) on the server. + +The config file must be served as static content and must be publicly accessible. +The structure of the runtime `config.json` must match the shape of the +application's base configuration defined in `config/default.json`. -> For advanced configuration options, have a look at the [`node-config`][nco] -> package. ## Contributing diff --git a/global.d.ts b/global.d.ts new file mode 100644 index 000000000..252d60211 --- /dev/null +++ b/global.d.ts @@ -0,0 +1,8 @@ +import type { Config } from "./module.d.ts"; + +declare global { + // eslint-disable-next-line no-var, @typescript-eslint/naming-convention + var __CONFIG__: Config; +} + +export {}; diff --git a/http-client/lib/fetcher.ts b/http-client/lib/fetcher.ts index 81f9bbb3e..2b6baa790 100644 --- a/http-client/lib/fetcher.ts +++ b/http-client/lib/fetcher.ts @@ -3,7 +3,7 @@ import type { ZodIssue, ZodType, TypeOf } from "zod"; -import config from "virtual:config"; +import config from "@app/lib/config"; import { satisfies } from "compare-versions"; export interface BaseUrl { diff --git a/src/components/ErrorMessage.svelte b/src/components/ErrorMessage.svelte index bad68ceb0..fa8d9a0b9 100644 --- a/src/components/ErrorMessage.svelte +++ b/src/components/ErrorMessage.svelte @@ -2,7 +2,7 @@ import type { ComponentProps } from "svelte"; import type { ErrorParam } from "@app/lib/router/definitions"; - import config from "virtual:config"; + import config from "@app/lib/config"; import Command from "./Command.svelte"; import ExternalLink from "./ExternalLink.svelte"; import IconLarge from "./IconLarge.svelte"; diff --git a/src/lib/config.ts b/src/lib/config.ts new file mode 100644 index 000000000..7a1bea865 --- /dev/null +++ b/src/lib/config.ts @@ -0,0 +1,19 @@ +import buildTimeConfig from "virtual:config"; +import mergeWith from "lodash/mergeWith"; +import { isArray } from "lodash"; + +const customMerge = (objValue: T, srcValue: T): T | undefined => { + if (isArray(objValue)) { + return srcValue; + } + return undefined; +}; + +const config = mergeWith( + {}, + buildTimeConfig, + globalThis.__CONFIG__ || {}, + customMerge, +) as typeof buildTimeConfig; + +export default config; diff --git a/src/lib/router.ts b/src/lib/router.ts index 5437f1531..e0864c4ab 100644 --- a/src/lib/router.ts +++ b/src/lib/router.ts @@ -5,7 +5,7 @@ import { get, writable } from "svelte/store"; import * as mutexExecutor from "@app/lib/mutexExecutor"; import * as utils from "@app/lib/utils"; -import config from "virtual:config"; +import config from "@app/lib/config"; import { repoRouteToPath, repoTitle, diff --git a/src/modals/ErrorModal.svelte b/src/modals/ErrorModal.svelte index fe602971f..d78a2df4b 100644 --- a/src/modals/ErrorModal.svelte +++ b/src/modals/ErrorModal.svelte @@ -1,5 +1,5 @@