# Homepage dynamic routes via environment variables

LLMS index: [llms.txt](/en/llms.txt)

---

Lead paragraph: Homepage consolidates service status pages into a single interface, but hardcoded URLs limit flexibility when working across multiple environments. Using environment variables with the `{{ .Env.VAR }}` templating variable allows dynamic route substitution without manually editing `homepage.yml` on every deployment. This note covers a practical setup and ENV switching via docker-compose.

## 1. Why dynamic routes needed in Homepage

Homepage gathers service status pages into one dashboard, yet hardcoded URLs strip flexibility in self-hosted setups where the same services must be reachable at different addresses in dev, staging, and prod. Homepage lacks built-in environment switching logic, but its YAML config supports environment variable substitution at render time. This means a single `homepage.yml` can serve different links depending on which ENV is loaded into the container. Additionally, this approach simplifies CI/CD: change the variable in the compose file or `.env`, and homepage updates without touching the config repo.

## 2. Setting environment variables in compose

Configuring environment variables in compose starts with the proper `environment` block in `docker-compose.yml`. Homepage reads exported container variables and makes them available in templates via the `{{ .Env. }}` prefix. Minimal compose fragment:

```yaml
services:
  homepage:
    image: gethomepage/homepage:latest
    environment:
      - HOMEPAGE_URL={{ .Env.HOMEPAGE_URL }}
      - ENVIRONMENT=prod
    volumes:
      - ./homepage.yml:/homepage/homepage.yml:ro
      - ./data:/data
```

Important: variables in the `environment` block can be defined inline or pulled from an external `.env` file with `docker compose up`. To inspect how variables resolve before starting the container, run `docker compose config` — it displays the final rendered configuration.

> [!TIP]
> Keep routing-affecting variables in separate `.env.prod` / `.env.dev` files and load them via `docker compose -f docker-compose.yml -f docker-compose.${ENV}.yml up`. This isolates settings and avoids conflicts when switching environments.

## 3. Example homepage.yml configuration with templating

The Homepage `homepage.yml` config is a YAML list of services. To have a service URL pulled from an environment variable, use the templating syntax `{{ .Env.VAR_NAME }}`. Below is a config fragment where the service URL is defined by the `SERVICE_URL` variable:

```yaml
- title: "API Service"
  url: "{{ .Env.SERVICE_URL }}"
  tagline: "Dynamically substituted route"
```

When creating or editing a service via CLI the variable can be set immediately: `homepage add --env SERVICE_URL=https://prod-api.example.com`. If the `--env` flag is absent in your CLI version, add the variable manually in `homepage.yml` or via `homepage edit`. After deployment, verify correct substitution by restarting the container: `docker compose restart homepage`.

> [!WARNING]
> Flag `--env` accuracy depends on the installed Homepage CLI version. Always check available flags with `homepage --help` before embedding them in automated deployment scripts.

## 4. Practical scenario: switching ENV=prod and ENV=dev

Imagine the same service accessible at different addresses depending on the environment. In `docker-compose.yml` we define a global `ENV` variable and inject the URL via `SERVICE_URL`. File structure:

```yaml
services:
  homepage:
    image: gethomepage/homepage:latest
    environment:
      - ENV=${ENV:-dev}
      - SERVICE_URL={{ .Env.SERVICE_URL }}
    volumes:
      - ./homepage.yml:/homepage/homepage.yml:ro
```

In `.env.prod`:

```
ENV=prod
SERVICE_URL=https://api-prod.example.com
```

In `.env.dev`:

```
ENV=dev
SERVICE_URL=https://api-dev.example.com
```

Switching is done with one command:

```bash
docker compose -f docker-compose.yml -f docker-compose.${ENV}.yml up -d
```

After switching, verify the updated page at `http://host:3000`. If the route does not refresh, run `docker compose restart homepage` and `homepage clear` to flush the route cache.

## 5. Common errors and typical breakdowns when changing environment

Working with dynamic routes through env vars in Homepage repeats several frequent mistakes. The table below maps each issue to its root cause and fix:

| Error | Cause | Fix |
|---|---|---|
| Link does not change after `.env` swap | Homepage cache or compose not reloaded | `docker compose restart homepage` + `homepage clear` |
| `{{ .Env.VAR }}` resolves to empty string | Variable not exported into container | Inspect `environment` block in compose, `.env` file presence, run `docker compose env` |
| YAML parse error in homepage.yml | Extra spaces in template `{{ .Env.VAR }}` | Ensure template sits on the same line as other fields; check indentation |
| Port or host hardcoded in service | URL baked directly into service description, overriding variable | Check `homepage.yml` for direct URL entries; remove or comment them out |

Additional trap: using `homepage add --env` in one CLI version, then editing `homepage.yml` in another, can create conflicts on next run. Always validate the final config before deploying, preferably via `docker compose config` and a manual YAML structure check.
