benjiann/NewsFocus
À propos
a web news focus
Autres projetsWhat you can do
- 🗞 Fetches news from NewsAPI Top Headlines
- 🏷 Supports tag-based filtering: Top, Technology, Business, Entertainment, Science, Health
- 🖼 Masonry / grid layout cards with image, title, description, source, and link
- 🔄 Infinite scroll to load more news
Progression mesurée des étoiles
- in 24h
- +2
- in 7 days
- +1
- in 15 days
- +5
- in 1 month
- Historique en cours
Variations nettes sur des périodes qui se chevauchent, pas des totaux quotidiens. Les mesures absentes restent vides. Les étoiles indiquent l’intérêt, pas la qualité.
README.md
Original📰 News Aggregator – Cloudflare Pages
🌐 English Version | 中文版
A simple, fast, and visually appealing news aggregator built with Cloudflare Pages.
Fetches news from NewsAPI and displays them in a masonry-style card layout, supporting tag filtering, infinite scroll, and night mode.
✨ Features
- 🗞 Fetches news from NewsAPI Top Headlines
- 🏷 Supports tag-based filtering: Top, Technology, Business, Entertainment, Science, Health
- 🖼 Masonry / grid layout cards with image, title, description, source, and link
- 🔄 Infinite scroll to load more news
- 🌙 Night mode toggle with theme persistence
- 🛡 XSS-safe rendering of all article fields
- 🔒 Same-origin API protection (cross-origin requests are rejected)
- 🖼 Lazy-loaded images with fade-in and default placeholder for broken images
- 🕒 Relative timestamps (e.g. "3h ago")
- ⚡ Cloudflare Pages serverless deployment (no backend server required)
- 🔁 Optional manual refresh button
- 💾 API responses are cached in KV to avoid exceeding NewsAPI limits
📁 Project Structure
functions/
api/
news.js # API proxy: NewsAPI fetch + KV cache + same-origin protection
config.js # Exposes the tag list (PUBLIC_TAGS)
shared/
kv-key.js # Shared KV key builder used by both the Pages Function and the Worker
public/
index.html # Main frontend page
style.css # Styles for cards and night mode
app.js # Frontend JS: fetch, render, infinite scroll, tag switching
_headers # Security headers for static assets (CSP/nosniff/Referrer-Policy/...)
worker-refresh/ # Scheduled Worker that pre-warms the KV cache (deployed separately)
tests/
kv-key.test.mjs # Unit tests for shared/kv-key.js
🛠 Environment Variables
Set the following in Cloudflare Pages → Settings → Environment Variables (and, for the Worker, via wrangler):
| Variable Name | Used by | Description |
|---|---|---|
NEWS_API_KEY |
Pages + Worker | Secret. Your NewsAPI API key. Shared by both runtimes. |
NEWS_SOURCES |
Pages + Worker | Optional comma-separated NewsAPI source list. When set, top-headlines is queried by sources and no language is sent. |
PUBLIC_TAGS |
Pages + Worker | Comma-separated tag list. Default: Top,Technology,Business,Entertainment,Science,Health. Powers /api/config and Worker pre-warm. |
CACHE_TTL_SECONDS |
Pages + Worker | KV cache TTL in seconds. Default: 600. |
PAGE_SIZE |
Worker | Page size for the Worker pre-warm fetch. Default: 40. |
⚠️ Do not hardcode the API key in frontend code. It is a secret consumed only by the Function proxy and the Worker.
🚀 Deployment Steps
Pages (frontend + API)
- Clone or fork this repository:
git clone https://github.com/lbjxr/NewsFocus.git
cd NewsFocus
- Connect to Cloudflare Pages:
- Go to Cloudflare Pages
- Click Create a Project → Connect your GitHub repo
- Configure Build Settings:
- Framework: None / Static Site
- Build command: leave empty (no build step)
- Build output directory:
public
- Add Functions support:
- KV namespace binding: bind the name
NEWS_CACHEto a KV namespace you created (any name, e.g.newsfocus).
Add Environment Variables (see table above); set
NEWS_API_KEYas a secret.Deploy:
- Click Save and Deploy → Pages serves the static frontend and the
/api/*Functions.
Worker (scheduled cache pre-warm — deploy separately)
The Worker in worker-refresh/ runs on a cron trigger and pre-warms the KV cache. It is not part of the Pages deployment.
cd worker-refresh
wrangler secret put NEWS_API_KEY
wrangler deploy
- The Worker must bind the same KV namespace (
NEWS_CACHE) as Pages — otherwise the pre-warmed entries will never be hit by/api/news. - Set
NEWS_SOURCES/PUBLIC_TAGS/CACHE_TTL_SECONDS/PAGE_SIZEfor the Worker as needed (inwrangler.tomlor viawrangler secret/vars).
- Visit your site:
- Frontend fetches news via
/api/newsautomatically - Infinite scroll and tag switching work out of the box
💡 Notes
- 🛡 KV caching prevents hitting NewsAPI request limits. KV is the single source of truth; the frontend always reads through the Function.
- 🖼 Image loading: broken images automatically show a placeholder.
- 🎨 Styling: customize
style.cssfor card layout and night mode colors. - 🔄 Refreshing news:
refresh=1forces a re-fetch, and only takes effect for same-origin requests. - ⚠️ NewsAPI free/developer plan does not allow requests from production domains (localhost development only). Production deployment requires a paid plan or a different data source.
- ⚠️ Configure WAF Rate Limiting for
/api/newsin the Cloudflare Dashboard. The in-code same-origin check is not a substitute for rate limiting.
🧑💻 Development
Run the shared KV-key unit tests (pure Node, no dependencies):
node tests/kv-key.test.mjs
🖼 Example Tags and Icons
| Tag | Icon |
|---|---|
| Top | 🏆 |
| Technology | 💻 |
| Business | 💼 |
| Entertainment | 🎬 |
| Science | 🔬 |
| Health | 🩺 |
You can also integrate Font Awesome icons in
index.htmlif you want more visual flair.
Explorer des projets similaires
Chargement…