Skip to content
Proyek

Tentang

a web news focus

Proyek lainnya

Cara angka ini diukur

296 bintang9 Fork +2 dalam 24 jam

Yang 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)

  1. Clone or fork this repository:
git clone https://github.com/lbjxr/NewsFocus.git
cd NewsFocus
  1. Connect to Cloudflare Pages:
  1. Configure Build Settings:
  • Framework: None / Static Site
  • Build command: leave empty (no build step)
  • Build output directory: public
  1. Add Functions support:
  • KV namespace binding: bind the name NEWS_CACHE to a KV namespace you created (any name, e.g. newsfocus).
  1. Add Environment Variables (see table above); set NEWS_API_KEY as a secret.

  2. 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_SIZE for the Worker as needed (in wrangler.toml or via wrangler secret/vars).
  1. Visit your site:
  • Frontend fetches news via /api/news automatically
  • 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.css for card layout and night mode colors.
  • πŸ”„ Refreshing news: refresh=1 forces 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/news in 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.html if you want more visual flair.


Jelajahi proyek terkait

Memuat…