ssr.jsx 1.95 KB
Newer Older
wuhao's avatar
wuhao committed
1 2 3 4 5 6
import "@/app/globals.css";
import prisma from "@/lib/prisma";
import dayjs from "dayjs";
import React, { memo } from "react";
import { Card, CardBody, CardFooter, Image, Button } from "@nextui-org/react";
import { difftime } from "@/lib/time";
wyuer's avatar
wyuer committed
7 8 9

// 服务器端渲染页面的组件
function ServerSidePage({ folders }) {
wuhao's avatar
wuhao committed
10

wyuer's avatar
wyuer committed
11
  return (
wuhao's avatar
wuhao committed
12
    <div className="p-4">
wuhao's avatar
wuhao committed
13
      <span className="bg-clip-text text-transparent bg-gradient-to-r from-pink-500 to-violet-500">
wuhao's avatar
wuhao committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
        服务端渲染页面
      </span>
      <div className="gap-2 grid xl:grid-cols-8 lg:grid-cols-6 md:grid-cols-4 sm:grid-cols-2 mt-4">
        {folders?.map((item) => {
          return (
            <Card shadow="sm" key={item.id} isPressable>
              <CardBody className="overflow-visible p-0">
                <Image
                  isZoomed
                  shadow="sm"
                  radius="lg"
                  width="100%"
                  alt={item.name}
                  className="w-full object-cover h-[140px]"
                  src={item.poster}
                />
              </CardBody>
              <CardFooter className="text-small justify-between">
                <b>{item.name}</b>
                <p className="text-default-500">{difftime(item.createdAt)}</p>
              </CardFooter>
            </Card>
          );
        })}
      </div>
wyuer's avatar
wyuer committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
    </div>
  );
}

// 服务器端渲染方法,获取数据并将其作为 props 传递给组件

export const getServerSideProps = async () => {
  const folders = await prisma.folder.findMany({
    // where: { published: true },
    // include: {
    //   author: {
    //     select: { name: true },
    //   },
    // },
  });
wuhao's avatar
wuhao committed
54 55


wyuer's avatar
wyuer committed
56
  return {
wuhao's avatar
wuhao committed
57 58 59 60 61 62 63
    props: {
      folders: folders?.map((it) => ({
        ...it,
        createdAt: dayjs(it?.createdAt)?.format("YYYY-MM-DD HH:mm:ss"),
        updatedAt: dayjs(it?.updatedAt)?.format("YYYY-MM-DD HH:mm:ss"),
      })),
    },
wyuer's avatar
wyuer committed
64 65 66 67
    // revalidate: 10, //轮询
  };
};

wuhao's avatar
wuhao committed
68
export default ServerSidePage;