feat: 调整resource为服务端渲染
This commit is contained in:
@@ -5,6 +5,7 @@ import { ResponseInterceptor } from './common/interceptors/response.interceptor'
|
|||||||
|
|
||||||
async function bootstrap() {
|
async function bootstrap() {
|
||||||
const app = await NestFactory.create(AppModule);
|
const app = await NestFactory.create(AppModule);
|
||||||
|
app.setGlobalPrefix('api');
|
||||||
app.useGlobalPipes(
|
app.useGlobalPipes(
|
||||||
new ValidationPipe({
|
new ValidationPipe({
|
||||||
transform: true,
|
transform: true,
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
'use client';
|
|
||||||
import favicon from '../favicon.ico';
|
import favicon from '../favicon.ico';
|
||||||
import Image from 'next/image';
|
import Image from 'next/image';
|
||||||
|
|
||||||
|
|||||||
@@ -1,32 +1,20 @@
|
|||||||
import { ResourceBadge } from "@/components/resource";
|
import { ResourceBadge } from "@/components/resource";
|
||||||
import { Card, CardContent } from "@/components/ui/card";
|
import { Card, CardContent } from "@/components/ui/card";
|
||||||
import { Resource } from "@/lib/types/resource";
|
import { Resource } from "@/lib/types/resource";
|
||||||
import Image from "next/image";
|
import ResourceCardImage from "./ResourceCardImage";
|
||||||
import React from "react";
|
|
||||||
|
|
||||||
interface ResourceCardProps extends React.HTMLProps<HTMLAnchorElement> {
|
interface ResourceCardProps extends React.HTMLProps<HTMLAnchorElement> {
|
||||||
r: Resource;
|
r: Resource;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function ResourceCard({ r, ...props }: ResourceCardProps) {
|
export function ResourceCard({ r, ...props }: ResourceCardProps) {
|
||||||
const [imageError, setImageError] = React.useState(false);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<a href={r.link} target="_blank" {...props}>
|
<a href={r.link} target="_blank" {...props}>
|
||||||
<Card className="w-full md:w-92 lg:w-100 md:rounded-xl rounded-none duration-300">
|
<Card className="w-full md:w-92 lg:w-100 md:rounded-xl rounded-none duration-300">
|
||||||
<CardContent>
|
<CardContent>
|
||||||
<div className="flex gap-6">
|
<div className="flex gap-6">
|
||||||
<div>
|
<div>
|
||||||
{!imageError && <Image
|
<ResourceCardImage imageUrl={r.imageUrl} />
|
||||||
src={r.imageUrl}
|
|
||||||
alt="资源图片"
|
|
||||||
width={90}
|
|
||||||
height={90}
|
|
||||||
className="rounded-md shadow"
|
|
||||||
priority
|
|
||||||
quality={80}
|
|
||||||
onError={() => setImageError(true)}
|
|
||||||
/>}
|
|
||||||
</div>
|
</div>
|
||||||
<div className="flex-1 overflow-x-hidden">
|
<div className="flex-1 overflow-x-hidden">
|
||||||
<div className="font-bold text-2xl">{r.title}</div>
|
<div className="font-bold text-2xl">{r.title}</div>
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
'use client';
|
||||||
|
import React from "react";
|
||||||
|
import Image from "next/image";
|
||||||
|
|
||||||
|
interface ResourceCardImage {
|
||||||
|
imageUrl: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function ResourceCardImage({ imageUrl }: ResourceCardImage) {
|
||||||
|
const [imageError, setImageError] = React.useState(false);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{!imageError && <Image
|
||||||
|
src={imageUrl}
|
||||||
|
alt="资源图片"
|
||||||
|
width={90}
|
||||||
|
height={90}
|
||||||
|
className="rounded-md shadow"
|
||||||
|
priority
|
||||||
|
quality={80}
|
||||||
|
onError={() => setImageError(true)}
|
||||||
|
/>}
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,6 +1,3 @@
|
|||||||
"use client";
|
|
||||||
|
|
||||||
import useSWR from "swr";
|
|
||||||
import { ResourceCard } from "./components/ResourceCard";
|
import { ResourceCard } from "./components/ResourceCard";
|
||||||
import { ResourceApi } from "@/lib/api";
|
import { ResourceApi } from "@/lib/api";
|
||||||
import {
|
import {
|
||||||
@@ -9,13 +6,10 @@ import {
|
|||||||
AlertTitle,
|
AlertTitle,
|
||||||
} from "@/components/ui/alert"
|
} from "@/components/ui/alert"
|
||||||
import { AlertCircle } from "lucide-react";
|
import { AlertCircle } from "lucide-react";
|
||||||
import { Skeleton } from "@/components/ui/skeleton";
|
|
||||||
|
|
||||||
export default function Resources() {
|
export default async function Resources() {
|
||||||
const { data, isLoading, error } = useSWR(
|
let errorMsg = '';
|
||||||
'/api/resource',
|
const data = await ResourceApi.list().catch(e => { errorMsg = `${e}`; return null; });
|
||||||
() => ResourceApi.list(),
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="flex-1 flex flex-col items-center">
|
<div className="flex-1 flex flex-col items-center">
|
||||||
@@ -24,13 +18,13 @@ export default function Resources() {
|
|||||||
<a className="text-zinc-600">《使用条款和隐私政策》</a>
|
<a className="text-zinc-600">《使用条款和隐私政策》</a>
|
||||||
,继续使用或浏览表示您接受协议条款。</p>
|
,继续使用或浏览表示您接受协议条款。</p>
|
||||||
{
|
{
|
||||||
error && (
|
errorMsg && (
|
||||||
<div className="mt-10 mx-5">
|
<div className="mt-10 mx-5">
|
||||||
<Alert variant="destructive">
|
<Alert variant="destructive">
|
||||||
<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>
|
||||||
</div>
|
</div>
|
||||||
@@ -38,12 +32,6 @@ export default function Resources() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
<div className="mt-6 sm:mt-10 md:mt-15 w-full flex flex-col md:w-auto md:mx-auto md:grid grid-cols-2 2xl:gap-x-35 lg:gap-x-20 gap-x-10 lg:gap-y-10 gap-y-5 sm:mb-10 duration-300">
|
<div className="mt-6 sm:mt-10 md:mt-15 w-full flex flex-col md:w-auto md:mx-auto md:grid grid-cols-2 2xl:gap-x-35 lg:gap-x-20 gap-x-10 lg:gap-y-10 gap-y-5 sm:mb-10 duration-300">
|
||||||
{isLoading && (
|
|
||||||
[...Array(3).map((_, i) => (
|
|
||||||
<Skeleton key={i} className="h-35 w-full md:w-92 lg:w-100 md:rounded-xl rounded-none duration-300" />
|
|
||||||
))]
|
|
||||||
)}
|
|
||||||
|
|
||||||
{data && data.map((resource) => (
|
{data && data.map((resource) => (
|
||||||
<ResourceCard
|
<ResourceCard
|
||||||
key={resource.id}
|
key={resource.id}
|
||||||
|
|||||||
34
apps/frontend/lib/api/client.ts
Normal file
34
apps/frontend/lib/api/client.ts
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
// import { headers } from 'next/headers'
|
||||||
|
|
||||||
|
export async function apiFetch<T extends unknown>(
|
||||||
|
url: string,
|
||||||
|
options: RequestInit = {}
|
||||||
|
): Promise<T> {
|
||||||
|
// const nextHeaders = await headers()
|
||||||
|
|
||||||
|
const defaultHeaders: HeadersInit = {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
// ...nextHeaders,
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(new URL(url, process.env.API_BASE), {
|
||||||
|
...options,
|
||||||
|
headers: {
|
||||||
|
...defaultHeaders,
|
||||||
|
...options.headers,
|
||||||
|
},
|
||||||
|
cache: 'no-store',
|
||||||
|
});
|
||||||
|
if (res.status === 200) {
|
||||||
|
const data = await res.json();
|
||||||
|
if (data.statusCode === 200) {
|
||||||
|
return data.data;
|
||||||
|
}
|
||||||
|
throw new Error(data.message ?? '未知错误');
|
||||||
|
}
|
||||||
|
throw new Error('请求失败');
|
||||||
|
} catch (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { Resource } from "@/lib/types/resource";
|
import { Resource } from "@/lib/types/resource";
|
||||||
import fetcher from "../fetcher";
|
import { apiFetch } from "../client";
|
||||||
|
|
||||||
export async function list() {
|
export async function list() {
|
||||||
return fetcher<Resource[]>('/api/resource');
|
return apiFetch<Resource[]>('/api/resource');
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user