mirror of
https://github.com/AderKonstantin/aderktech-chronark.com-.git
synced 2025-06-08 13:48:42 +03:00
Update route.ts and page.tsx
This commit is contained in:
parent
347a0c638c
commit
39edf4ea49
@ -1,26 +1,25 @@
|
|||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
import { getRedisClient } from "../../../lib/redis";
|
import { getRedisClient } from "../../../lib/redis";
|
||||||
|
|
||||||
export const dynamic = 'force-dynamic'; // Отключаем кеширование
|
export const dynamic = 'force-dynamic';
|
||||||
|
export const fetchCache = 'force-no-store';
|
||||||
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
try {
|
try {
|
||||||
const redis = getRedisClient();
|
const redis = getRedisClient();
|
||||||
const projects = await redis.keys("projects:*:views");
|
const keys = await redis.keys('projects:*:views');
|
||||||
const viewsEntries = await redis.mget(...projects);
|
if (keys.length === 0) return NextResponse.json({});
|
||||||
|
|
||||||
const viewsData = projects.reduce((acc, key, index) => {
|
const values = await redis.mget(...keys);
|
||||||
const slug = key.split(":")[1];
|
const views = keys.reduce((acc, key, i) => {
|
||||||
acc[slug] = parseInt(viewsEntries[index] as string) || 0;
|
const slug = key.split(':')[1];
|
||||||
return acc;
|
acc[slug] = parseInt(values[i] as string) || 0;
|
||||||
}, {} as Record<string, number>);
|
return acc;
|
||||||
|
}, {} as Record<string, number>);
|
||||||
|
|
||||||
return NextResponse.json(viewsData);
|
return NextResponse.json(views);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Redis error:", error);
|
console.error('Redis error:', error);
|
||||||
return NextResponse.json(
|
return NextResponse.json({}, { status: 500 });
|
||||||
{ error: "Failed to fetch views" },
|
}
|
||||||
{ status: 500 }
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
'use client'; // Превращаем компонент в клиентский
|
'use client';
|
||||||
|
|
||||||
import Link from "next/link";
|
import Link from "next/link";
|
||||||
import React, { useEffect, useState } from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
@ -8,8 +8,6 @@ import { Card } from "../components/card";
|
|||||||
import { Article } from "./article";
|
import { Article } from "./article";
|
||||||
import { Eye } from "lucide-react";
|
import { Eye } from "lucide-react";
|
||||||
|
|
||||||
export const revalidate = 60;
|
|
||||||
|
|
||||||
type ViewsData = Record<string, number>;
|
type ViewsData = Record<string, number>;
|
||||||
|
|
||||||
export default function ProjectsPage() {
|
export default function ProjectsPage() {
|
||||||
@ -17,12 +15,11 @@ export default function ProjectsPage() {
|
|||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
// Загружаем данные через клиентский запрос
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchViews = async () => {
|
const fetchViews = async () => {
|
||||||
try {
|
try {
|
||||||
const response = await fetch('/api/views');
|
const response = await fetch('/api/views');
|
||||||
if (!response.ok) throw new Error('Failed to fetch views');
|
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
setViews(data);
|
setViews(data);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user