diff --git a/frontend/src/layouts/ArticleLayout.astro b/frontend/src/layouts/ArticleLayout.astro index 446a09c..67440df 100644 --- a/frontend/src/layouts/ArticleLayout.astro +++ b/frontend/src/layouts/ArticleLayout.astro @@ -312,17 +312,22 @@ const { // 5. СЧЁТЧИК ПРОСМОТРОВ const viewsEl = document.querySelector('.meta-views') as HTMLElement & { dataset: { postId: string } }; + console.log('[Views] Element found:', viewsEl, 'postId:', viewsEl?.dataset?.postId); if (viewsEl?.dataset?.postId) { const postId = viewsEl.dataset.postId; + console.log('[Views] Fetching for post:', postId); fetch(`/api/increment-views?postId=${postId}`) .then(res => res.json()) .then(data => { + console.log('[Views] Response:', data); if (data.views !== undefined) { viewsEl.textContent = formatViews(data.views); } }) - .catch(() => {}); + .catch(err => { + console.error('[Views] Error:', err); + }); } function formatViews(n: number): string { diff --git a/frontend/src/pages/api/increment-views.ts b/frontend/src/pages/api/increment-views.ts index b68d74a..d892ecd 100644 --- a/frontend/src/pages/api/increment-views.ts +++ b/frontend/src/pages/api/increment-views.ts @@ -10,7 +10,13 @@ function getClientIp(request: Request): string { if (forwarded) { return forwarded.split(',')[0].trim(); } - return request.headers.get('x-real-ip') || 'unknown'; + const realIp = request.headers.get('x-real-ip'); + if (realIp) { + return realIp; + } + // Для localhost использовать заголовок Host или default + const host = request.headers.get('host') || 'localhost'; + return host.includes('localhost') || host.includes('127.0.0.1') ? 'localhost' : 'unknown'; } function generateVisitorHash(ip: string, userAgent: string): string {