benjiann/NewsFocus
Tentang
a web news focus
Proyek lainnyaYang dapat Anda lakukan
- π 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
Pertumbuhan bintang terukur
- dalam 24 jam
- +2
- dalam 7 hari
- +1
- dalam 15 hari
- +5
- dalam 1 bulan
- Mengumpulkan riwayat
Perubahan bersih pada periode yang tumpang tindih, bukan total harian. Pengukuran yang belum tersedia dibiarkan kosong. Bintang menunjukkan perhatian, bukan kualitas.
README.md
Asliπ° 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.
Jelajahi proyek terkait
Memuatβ¦