diff --git a/packages/website/api/addViewer.ts b/packages/website/api/addViewer.ts deleted file mode 100644 index 4e9f9ce6f..000000000 --- a/packages/website/api/addViewer.ts +++ /dev/null @@ -1,46 +0,0 @@ -export async function addViewer(pathname: string): Promise { - let isNew = true; - if (window.localStorage.getItem("visited")) { - isNew = false; - } else { - window.localStorage.setItem("visited", "true"); - } - let isNewByPath = true; - if (window.localStorage.getItem(`visited-${pathname}`)) { - isNewByPath = false; - } else { - window.localStorage.setItem(`visited-${pathname}`, "true"); - } - - try { - const url = `/api/public/viewer?isNew=${isNew}&isNewByPath=${isNewByPath}`; - const res = await fetch(url, { - method: "POST", - }); - const { statusCode, data } = await res.json(); - if (statusCode == 233) { - return { viewer: 0, visited: 0 }; - } - return data; - } catch (err) { - console.log(err); - throw err; - } -} -export async function addViewerWithApiRoute() { - let isNew = true; - if (window.localStorage.getItem("visited")) { - isNew = false; - } else { - window.localStorage.setItem("visited", "true"); - } - try { - const url = `/api/viewer?isNew=${isNew}`; - const res = await fetch(url); - const data = await res.json(); - return data; - } catch (err) { - console.log(err); - throw err; - } -} diff --git a/packages/website/api/pageview.ts b/packages/website/api/pageview.ts new file mode 100644 index 000000000..fd30611d8 --- /dev/null +++ b/packages/website/api/pageview.ts @@ -0,0 +1,53 @@ +const DEFAULT_PAGEVIEW_RESPONSE = { viewer: 0, visited: 0 }; + +export interface PageViewData { + viewer: number; + visited: number; +} + +export const updatePageview = async ( + pathname: string +): Promise => { + const hasVisited = window.localStorage.getItem("visited"); + const hasVisitedCurrentPath = window.localStorage.getItem( + `visited-${pathname}` + ); + + if (!hasVisited) { + window.localStorage.setItem("visited", "true"); + } + + if (!hasVisitedCurrentPath) { + window.localStorage.setItem(`visited-${pathname}`, "true"); + } + + try { + const { statusCode, data } = await fetch( + `/api/public/viewer?isNew=${!hasVisited}&isNewByPath=${!hasVisitedCurrentPath}`, + { method: "POST" } + ).then((res) => res.json()); + + return statusCode === 233 ? DEFAULT_PAGEVIEW_RESPONSE : data; + } catch (err) { + console.log(err); + throw err; + } +}; + +export const addViewerWithApiRoute = async () => { + let isNew = true; + if (window.localStorage.getItem("visited")) { + isNew = false; + } else { + window.localStorage.setItem("visited", "true"); + } + try { + const url = `/api/viewer?isNew=${isNew}`; + const res = await fetch(url); + const data = await res.json(); + return data; + } catch (err) { + console.log(err); + throw err; + } +}; diff --git a/packages/website/pages/_app.tsx b/packages/website/pages/_app.tsx index 24f05f5bc..513107467 100644 --- a/packages/website/pages/_app.tsx +++ b/packages/website/pages/_app.tsx @@ -11,7 +11,7 @@ import type { AppProps } from "next/app"; import { GlobalContext, GlobalState } from "../utils/globalContext"; import { useCallback, useEffect, useRef, useState } from "react"; import { useRouter } from "next/router"; -import { addViewer } from "../api/addViewer"; +import { updatePageview } from "../api/pageview"; import Head from "next/head"; function MyApp({ Component, pageProps }: AppProps) { @@ -30,7 +30,7 @@ function MyApp({ Component, pageProps }: AppProps) { } const pathname = window.location.pathname; console.log("[更新访客]", reason, pathname); - const { viewer, visited } = await addViewer(pathname); + const { viewer, visited } = await updatePageview(pathname); setGlobalState({ ...globalState, viewer: viewer, visited: visited }); }, [globalState, setGlobalState]