Files
news-platform/docs/frontend.md
738f847d36 feat: theme system + institutional theme + andebol.net domain config
Add domain-aware theme system (BIT-494, BIT-495, BIT-496, BIT-497):

- Theme architecture: move shared components into src/themes/sports/,
  create src/themes/institutional/ with handball-oriented design
- Domain config: andebol.net.json (institutional theme, pt-PT locale,
  handball categories, WP endpoint port 8081)
- theme.ts library: resolves theme components per domain config
- Updated layouts/pages to use theme-aware component loading
- Updated docs: domains.md, frontend.md with theme system docs
- Smoke test script for andebol.net
- WordPress .env.example with andebol env vars

Note: this work was completed in BIT-494/495/496/497 but was
never committed — committing now to unblock andebol.net deploy.

Co-Authored-By: Paperclip <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
2026-03-27 08:38:41 +00:00

163 lines
6.8 KiB
Markdown

# Frontend — Astro 5 SSR
> **Status:** Active — SSR, multi-domain config, pluggable themes under `src/themes/`.
---
## Overview
The frontend is an Astro 5 SSR application. It renders all pages server-side, resolves the active domain config at request time, fetches data from the Fastify bridge, and returns complete HTML to the client.
**Themes** swap header, footer, and homepage shells (hero, trending, category blocks, article cards) without duplicating routes. Domain JSON field `theme` selects which component set loads; see [domains.md — Choosing a Theme](./domains.md#choosing-a-theme).
---
## Tech Stack
| Layer | Choice |
|-------|--------|
| Framework | Astro 5 (SSR mode, `output: 'server'`) |
| Adapter | `@astrojs/node` |
| UI components | Astro components + minimal React islands |
| Styling | Tailwind CSS v4 |
| Fonts | Self-hosted (no Google Fonts CDN) |
| Build | `astro build` → Node.js server |
---
## Directory Structure
```
frontend/
├── src/
│ ├── layouts/
│ │ └── Base.astro # HTML shell, OG/meta, theme CSS hooks, Header/Footer from theme
│ ├── pages/
│ │ ├── index.astro # Homepage (uses themed blocks + LatestGrid)
│ │ ├── category/[slug].astro
│ │ ├── article/[slug].astro
│ │ ├── sitemap.xml.ts
│ │ └── robots.txt.ts
│ ├── themes/ # One subfolder per theme id (must match domain JSON + theme.ts)
│ │ ├── sports/
│ │ │ ├── Header.astro
│ │ │ ├── Footer.astro
│ │ │ ├── HeroSection.astro
│ │ │ ├── ArticleCard.astro
│ │ │ ├── CategorySection.astro
│ │ │ └── TrendingBar.astro
│ │ ├── institutional/
│ │ │ ├── … (same six components)
│ │ │ └── theme.css # optional token overrides
│ │ └── README.md # short in-repo contract summary
│ ├── components/ # shared, non-themed pieces
│ │ ├── home/
│ │ │ └── LatestGrid.astro # picks ArticleCard via getThemeComponents(theme)
│ │ └── article/
│ │ ├── ArticleHeader.astro
│ │ └── RelatedArticles.astro
│ ├── lib/
│ │ ├── domain.ts # Domain config loader (config/domains/*.json)
│ │ ├── theme.ts # Theme registry + getThemeComponents()
│ │ ├── bridge.ts # Bridge API client
│ │ └── format.ts
│ ├── types/
│ │ └── index.ts # DomainConfig, ArticleListItem, etc.
│ └── styles/
│ └── global.css
├── astro.config.mjs
├── tailwind.config.mjs
└── package.json
```
---
## Theme Authoring
Use this when adding a third theme or adjusting an existing one. The canonical one-page summary also lives in `frontend/src/themes/README.md`.
### Directory layout
Create `frontend/src/themes/<theme-id>/` with **exactly these six** Astro modules at the theme root (names matter — `theme.ts` imports them by path):
| File | Responsibility | Props (contract) |
|------|----------------|------------------|
| `Header.astro` | Site header and primary nav | `config: DomainConfig` |
| `Footer.astro` | Site footer | `config: DomainConfig` |
| `HeroSection.astro` | Homepage hero | `articles: ArticleListItem[]` (bridge `homepage.hero`) |
| `TrendingBar.astro` | Trending strip | `articles: ArticleListItem[]` (`homepage.trending`) |
| `CategorySection.astro` | One homepage category block | `category: CategoryConfig`, `articles: ArticleListItem[]` |
| `ArticleCard.astro` | Article teaser | `article: ArticleListItem`, optional `size?: 'sm' \| 'md' \| 'lg'`, optional `show_excerpt?: boolean` (default `true`) |
Types are defined in `frontend/src/types/index.ts`. Callers (e.g. `pages/index.astro`, `components/home/LatestGrid.astro`) assume this shape; keep field usage aligned with `ArticleListItem` (`slug`, `title`, `excerpt`, `featured_image`, `featured_image_alt`, `author`, `category`, `published_at`, `reading_time`, flags, etc.).
**Self-contained themes:** inside a theme folder, import sibling components with relative paths (e.g. `./ArticleCard.astro`), not `~/themes/other/…`.
### CSS tokens and `theme.css`
`layouts/Base.astro` injects **global** custom properties from domain config:
- `--color-primary` ← `config.primary_color`
- `--color-secondary` ← `config.secondary_color`
- `--color-accent` ← `config.accent_color`
Themes should use these variables in CSS where possible so domains can rebrand without code changes.
Optional **`theme.css`** in the theme folder can override or extend tokens. Example from `institutional/theme.css`:
- `:root` — `--color-primary`, `--color-secondary`, `--color-accent`, `--font-heading`
- `[data-theme="dark"]` — dark-mode tweaks for the same variables
**Wiring:** `Base.astro` currently loads `institutional/theme.css` only when `config.theme === 'institutional'`. If you add another theme that needs its own sheet, add a matching `<link>` (or shared import) in `Base.astro` for that theme id.
### Registering a theme in `src/lib/theme.ts`
Astro cannot `import()` a `.astro` file from a runtime string. Every theme must be:
1. **Imported** at the top of `theme.ts` (one import per component, or grouped by theme).
2. **Declared** on the `ThemeId` union type (e.g. `'sports' | 'institutional' | 'mytheme'`).
3. **Mapped** in `THEME_BY_ID: Record<ThemeId, ThemeComponents>` with all six components.
`getThemeComponents(theme: string)` normalizes the string to a `ThemeId` and falls back to **`sports`** if the id is unknown.
**Checklist for a new theme `mytheme`:**
1. Add folder `src/themes/mytheme/` with the six required `.astro` files (and optional `theme.css`).
2. Extend `ThemeId` and add `mytheme` to `THEME_BY_ID`.
3. If using `theme.css`, load it from `Base.astro` when `config.theme === 'mytheme'`.
4. Set `"theme": "mytheme"` in the domain JSON for sites that should use it.
---
## Domain Resolution
See [architecture.md#4-multi-domain-routing](./architecture.md#4-multi-domain-routing) and [domains.md](./domains.md).
---
## Performance Notes
- All pages are SSR — no client-side data fetching waterfalls
- Interactive components are Astro Islands (only hydrate when needed)
- Hero images use `fetchpriority="high"` and `<Image />` for WebP/AVIF
- Tailwind output is purged — CSS < 20 KB per page
- `Cache-Control: s-maxage=60, stale-while-revalidate=300` set on article pages
---
## Development
```bash
cd frontend
npm install
npm run dev # localhost:4321
npm run build # production build
npm run preview # preview production build
```
Environment variables:
```env
BRIDGE_URL=http://localhost:3200
```