Visitor Map Worker
Visitor Map Worker
This worker turns the homepage visitor map into a live service:
- every homepage visit calls
/api/visit - total visits increase in real time
- the recent visitor list keeps the latest 5 entries
- only city/country/time are stored for recent visits
- no raw IP address is stored in KV
Architecture
Frontend:
- homepage card in
_pages/about.md - browser script in
assets/js/visitor-map.js
Backend:
- Cloudflare Worker in
worker/src/index.js - Cloudflare KV namespace bound as
VISITOR_STATS
Setup
- Create a KV namespace in Cloudflare.
- Put the namespace id into
worker/wrangler.toml:
kv_namespaces = [
{ binding = "VISITOR_STATS", id = "YOUR_KV_NAMESPACE_ID" }
]
- Install Wrangler if needed:
npm install -g wrangler
- Login and deploy:
cd worker
wrangler login
wrangler deploy
- Copy the deployed Worker endpoint, for example:
https://linpeiran-visitor-map.<subdomain>.workers.dev/api/visit
- Put that URL into
_config.yml:
visitor_map:
endpoint: "https://linpeiran-visitor-map.<subdomain>.workers.dev/api/visit"
- Rebuild/redeploy the GitHub Pages site.
Local behavior before deployment
If visitor_map.endpoint is empty, the homepage will fall back to:
assets/data/visitor-map.json
That means the page still renders locally even before the Worker is online.
Notes
- The Worker counts one visit each time the homepage script successfully calls the endpoint.
- If you later want to reduce repeated refresh counting, we can add a short cookie-based cooldown.
- If you want a custom domain instead of
workers.dev, we can bind one later.
