From c369cd30bdc73303a5fe78a92d344b0e4d4d62d6 Mon Sep 17 00:00:00 2001 From: Web-serfer Date: Tue, 28 Apr 2026 02:09:18 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=BE=D0=B2=D1=8B=D0=B5=20=D0=B8=D0=B7?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F=20=D0=B2=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=B5=D0=BA=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/layouts/ArticleLayout.astro | 7 ++++++- frontend/src/pages/api/increment-views.ts | 8 +++++++- 2 files changed, 13 insertions(+), 2 deletions(-) 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 {