opt: упростить счетчики - одна запись в БД вместо тысяч

- site_stats: 1 запись с полями today/total вместо site_visitors
- posts: today_views + last_reset вместо post_views коллекции
- Удалены старые коллекции site_visitors и post_views
This commit is contained in:
Web-serfer 2026-05-07 16:55:35 +05:00
parent 62a6453a1a
commit e7700a3391
6 changed files with 65 additions and 622 deletions

View file

@ -28,20 +28,10 @@ const { today = 0, total = 0 } = Astro.props;
const valueEls = root.querySelectorAll('.counter-value');
if (valueEls.length < 2) return;
const todayStr = new Date().toDateString();
const lastVisit = localStorage.getItem('site_visited');
// Если уже посещали сегодня - помечаем как повторный визит
const isRepeat = lastVisit === todayStr;
const apiUrl = isRepeat ? '/api/visitors?repeat=true' : '/api/visitors';
fetch(apiUrl).then(r => r.json()).then(d => {
if (typeof d.todayVisitors === 'number') {
if (d.isNewVisitor) {
localStorage.setItem('site_visited', todayStr);
}
if (valueEls[0]) valueEls[0].textContent = d.todayVisitors + ' сегодня';
if (valueEls[1]) valueEls[1].textContent = d.totalVisitors + ' всего';
fetch('/api/visitors').then(r => r.json()).then(d => {
if (typeof d.today === 'number') {
if (valueEls[0]) valueEls[0].textContent = d.today + ' сегодня';
if (valueEls[1]) valueEls[1].textContent = d.total + ' всего';
}
}).catch(() => {});
})();