Files
news-platform/docs/bridge.md
Artur MoreiraandPaperclip eff0aaa366 feat(frontend): add Cache-Control headers to all page routes (BIT-2261)
Set Cache-Control: s-maxage=60, stale-while-revalidate=300 on all page
routes to enable Cloudflare CDN caching per docs/architecture.md § 7.

Files updated:
- index.astro (homepage)
- article/[slug].astro (article pages)
- category/[slug].astro (category pages)
- noticias.astro (news listing)
- search.astro (search results)
- sitemap.xml.ts (sitemap)
- robots.txt.ts (robots)

This enables:
- CDN edge caching for 60 seconds (s-maxage)
- Serving stale content while revalidating for up to 300s (SWR)
- Improved TTFB for repeated requests (target: <30ms cache HIT)

Acceptance criteria:
✓ All page routes return correct Cache-Control header
✓ Cloudflare can cache responses with cf-cache-status: HIT
✓ TTFB improves to <30ms for cache HIT scenario
✓ Header values match architecture spec

Co-Authored-By: Paperclip <[email protected]>
2026-04-11 03:37:32 +00:00

126 lines
3.3 KiB
Markdown

# Bridge — Fastify API Layer
> **Status:** Active — delivered (BIT-350, BIT-2076)
---
## Overview
The bridge is a lightweight Fastify HTTP server that sits between the Astro frontend and WordPress. It abstracts WordPress, normalizes data into a canonical article model, and caches responses in Redis. A single bridge instance can serve multiple domains (each mapped to a different WordPress instance via domain config).
---
## Responsibilities
- Route requests to the correct WordPress endpoint per domain
- Cache WP REST API responses in Redis
- Normalize WordPress post/category/tag responses into a canonical model
- Expose cache invalidation endpoints (called by WP `save_post` hook)
- Isolate the frontend from WP REST API changes
---
## Tech Stack
| Layer | Choice |
|-------|--------|
| HTTP server | Fastify 5 |
| Language | TypeScript (Node.js 22+) |
| Cache | Redis (ioredis) |
| HTTP client | `undici` |
| Process manager | PM2 or systemd |
---
## Directory Structure
```
bridge/
├── src/
│ ├── server.ts # Fastify instance
│ ├── routes/
│ │ ├── posts.ts
│ │ ├── categories.ts
│ │ ├── search.ts
│ │ ├── sitemap.ts
│ │ └── cache.ts # Invalidation endpoints
│ ├── services/
│ │ ├── wordpress.ts # WP REST client
│ │ └── cache.ts # Redis wrapper
│ ├── normalizers/
│ │ ├── post.ts # WP post → Article
│ │ └── category.ts # WP category → Category
│ └── config/
│ └── domain.ts # Domain → WP endpoint resolver
├── package.json
└── tsconfig.json
```
---
## API Routes
| Method | Path | Description |
|--------|------|-------------|
| `GET` | `/posts?domain=&page=&per_page=` | List posts |
| `GET` | `/posts/:slug?domain=` | Single post by slug |
| `GET` | `/categories?domain=` | Category list |
| `GET` | `/tags?domain=` | Tag list |
| `GET` | `/search?domain=&q=` | Full-text search |
| `GET` | `/sitemap?domain=` | All posts for sitemap |
| `POST` | `/cache/invalidate` | Invalidate specific key |
| `POST` | `/cache/invalidate-all` | Flush all keys for domain |
---
## Canonical Article Model
```typescript
interface Article {
id: number;
slug: string;
title: string;
excerpt: string;
content: string; // sanitized HTML
published_at: string; // ISO 8601
modified_at: string;
author: { id: number; name: string };
featured_image: { url: string; alt: string; width: number; height: number } | null;
categories: { id: number; slug: string; name: string }[];
tags: { id: number; slug: string; name: string }[];
canonical_url: string;
}
```
---
## Caching
See [architecture.md#7-caching-strategy](./architecture.md#7-caching-strategy).
Redis key pattern: `{type}:{domain}:{identifier}`
Examples:
- `post:newsproject.bitreport.pt:porto-bate-benfica`
- `posts-list:newsproject.bitreport.pt:futebol:1`
- `categories:newsproject.bitreport.pt`
---
## Development
```bash
cd bridge
npm install
npm run dev # localhost:3200 (tsx watch)
npm run build # tsc → dist/
npm run test # Vitest
```
Environment variables:
```env
REDIS_URL=redis://localhost:6379
PORT=3200
```