perf: refactor global map as client side render

This commit is contained in:
hamster1963 2024-12-05 14:45:58 +08:00
parent 07f02f5da8
commit f8bfbde9b5
3 changed files with 29 additions and 13 deletions

View File

@ -1,11 +1,30 @@
import { GetNezhaData } from "@/lib/serverFetch"; "use client";
import { ServerApi } from "@/app/types/nezha-api";
import { nezhaFetcher } from "@/lib/utils";
import useSWR from "swr";
import { geoJsonString } from "../../../lib/geo-json-string"; import { geoJsonString } from "../../../lib/geo-json-string";
import GlobalInfo from "./GlobalInfo"; import GlobalInfo from "./GlobalInfo";
import GlobalLoading from "./GlobalLoading";
import { InteractiveMap } from "./InteractiveMap"; import { InteractiveMap } from "./InteractiveMap";
export default async function ServerGlobal() { export default function ServerGlobal() {
const nezhaServerList = await GetNezhaData(); const { data: nezhaServerList, error } = useSWR<ServerApi>(
"/api/server",
nezhaFetcher,
);
if (error)
return (
<div className="flex flex-col items-center justify-center">
<p className="text-sm font-medium opacity-40">{error.message}</p>
</div>
);
if (!nezhaServerList) {
return <GlobalLoading />;
}
const countryList: string[] = []; const countryList: string[] = [];
const serverCounts: { [key: string]: number } = {}; const serverCounts: { [key: string]: number } = {};

View File

@ -202,9 +202,7 @@ export default function ServerOverviewClient() {
</p> </p>
<p className="text-[11px] flex items-center text-nowrap font-semibold"> <p className="text-[11px] flex items-center text-nowrap font-semibold">
<ArrowDownCircleIcon className="size-3 mr-0.5" /> <ArrowDownCircleIcon className="size-3 mr-0.5" />
{formatBytes( {formatBytes(data?.total_in_speed)}/s
data?.total_in_speed,
)}/s
</p> </p>
</section> </section>
</> </>

View File

@ -1,10 +1,13 @@
import ServerList from "@/components/ServerList"; import ServerList from "@/components/ServerList";
import ServerOverview from "@/components/ServerOverview"; import ServerOverview from "@/components/ServerOverview";
import { Suspense } from "react"; import dynamic from "next/dynamic";
import ServerGlobal from "./ClientComponents/Global";
import GlobalLoading from "./ClientComponents/GlobalLoading"; import GlobalLoading from "./ClientComponents/GlobalLoading";
const ServerGlobal = dynamic(() => import("./ClientComponents/Global"), {
loading: () => <GlobalLoading />,
});
export default async function Home({ export default async function Home({
searchParams, searchParams,
}: { }: {
@ -15,11 +18,7 @@ export default async function Home({
<div className="mx-auto grid w-full max-w-5xl gap-4 md:gap-6"> <div className="mx-auto grid w-full max-w-5xl gap-4 md:gap-6">
<ServerOverview /> <ServerOverview />
{!global && <ServerList />} {!global && <ServerList />}
{global && ( {global && <ServerGlobal />}
<Suspense fallback={<GlobalLoading />}>
<ServerGlobal />
</Suspense>
)}
</div> </div>
); );
} }