feat: 调整前端blog列表为服务端渲染模式

This commit is contained in:
2025-12-12 20:48:47 +08:00
parent 7d3a809fa7
commit e6f3459f81
2 changed files with 7 additions and 22 deletions

View File

@@ -1,9 +1,5 @@
"use client"
import { Skeleton } from "@/components/ui/skeleton";
import { BlogApi } from "@/lib/api"; import { BlogApi } from "@/lib/api";
import { useCallback } from "react" import { useCallback } from "react"
import useSWR from "swr";
import { import {
Alert, Alert,
AlertDescription, AlertDescription,
@@ -12,7 +8,7 @@ import {
import { AlertCircle } from "lucide-react"; import { AlertCircle } from "lucide-react";
import { base62 } from "@/lib/utils"; import { base62 } from "@/lib/utils";
export default function Blog() { export default async function Blog() {
const formatNumber = useCallback((num: number) => { const formatNumber = useCallback((num: number) => {
if (num >= 1000) { if (num >= 1000) {
return (num / 1000).toFixed(1) + 'K'; return (num / 1000).toFixed(1) + 'K';
@@ -22,29 +18,18 @@ export default function Blog() {
return num.toString(); return num.toString();
}, []); }, []);
const { data: blogs, error, isLoading } = useSWR( let errorMsg = '';
'/api/blogs', const blogs = await BlogApi.list().catch(e => { errorMsg = `${e}`; return null });
() => BlogApi.list(),
)
return ( return (
<div className="max-w-120 w-auto mx-auto my-10 flex flex-col gap-8"> <div className="max-w-120 w-auto mx-auto my-10 flex flex-col gap-8">
{ {
isLoading && ( errorMsg && (
<div className="w-full">
<Skeleton className="w-full h-5" />
<Skeleton className="w-full h-10 mt-1" />
<Skeleton className="w-full h-5 mt-5" />
</div>
)
}
{
error && (
<Alert variant="destructive" className="w-full"> <Alert variant="destructive" className="w-full">
<AlertCircle className="h-4 w-4" /> <AlertCircle className="h-4 w-4" />
<AlertTitle></AlertTitle> <AlertTitle></AlertTitle>
<AlertDescription> <AlertDescription>
{error.message} {errorMsg}
</AlertDescription> </AlertDescription>
</Alert> </Alert>
) )

View File

@@ -1,6 +1,6 @@
import { Blog } from "@/lib/types/blog"; import { Blog } from "@/lib/types/blog";
import fetcher from "../fetcher"; import { apiFetch } from "../client";
export async function list() { export async function list() {
return fetcher<Blog[]>('/api/blog'); return apiFetch<Blog[]>('/api/blog');
} }