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]>
3.3 KiB
3.3 KiB
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_posthook) - 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
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.
Redis key pattern: {type}:{domain}:{identifier}
Examples:
post:newsproject.bitreport.pt:porto-bate-benficaposts-list:newsproject.bitreport.pt:futebol:1categories:newsproject.bitreport.pt
Development
cd bridge
npm install
npm run dev # localhost:3200 (tsx watch)
npm run build # tsc → dist/
npm run test # Vitest
Environment variables:
REDIS_URL=redis://localhost:6379
PORT=3200