From a14d04edbe37a42abfaf92129bf42cd66b7f1865 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 22 Sep 2022 00:33:04 +0100 Subject: [PATCH] feat: prefetch subscriptions data --- package.json | 4 +- pnpm-lock.yaml | 73 +++++++++++++++--------------- src/components/site/SiteHeader.tsx | 20 ++++---- src/components/site/SiteLayout.tsx | 5 +- src/pages/_site/[site]/index.tsx | 14 +++++- src/queries/site.server.ts | 22 +++++++++ 6 files changed, 86 insertions(+), 52 deletions(-) diff --git a/package.json b/package.json index 1fe82ed1..77ddf3e4 100644 --- a/package.json +++ b/package.json @@ -49,7 +49,7 @@ "busboy": "^1.6.0", "clsx": "^1.1.1", "cookie": "^0.5.0", - "crossbell.js": "^0.10.27", + "crossbell.js": "^0.10.28", "dayjs": "^1.11.1", "dotenv": "^16.0.0", "emoji-mart": "^5.2.0", @@ -86,7 +86,7 @@ "remark-stringify": "^10.0.2", "sortablejs": "^1.15.0", "ua-parser-js": "^1.0.2", - "unidata.js": "^0.7.1", + "unidata.js": "^0.7.2", "unified": "^10.1.2", "unist-builder": "^3.0.0", "unist-util-visit": "^4.1.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 004eb155..161dc91d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -58,7 +58,7 @@ specifiers: clsx: ^1.1.1 cookie: ^0.5.0 cross-spawn: ^7.0.3 - crossbell.js: ^0.10.27 + crossbell.js: ^0.10.28 dayjs: ^1.11.1 dotenv: ^16.0.0 emoji-mart: ^5.2.0 @@ -106,7 +106,7 @@ specifiers: tailwindcss: ^3.0.24 typescript: ^4.5.5 ua-parser-js: ^1.0.2 - unidata.js: ^0.7.1 + unidata.js: ^0.7.2 unified: ^10.1.2 unist-builder: ^3.0.0 unist-util-visit: ^4.1.0 @@ -150,7 +150,7 @@ dependencies: busboy: 1.6.0 clsx: 1.1.1 cookie: 0.5.0 - crossbell.js: 0.10.27 + crossbell.js: 0.10.28 dayjs: 1.11.3 dotenv: 16.0.1 emoji-mart: 5.2.0 @@ -187,7 +187,7 @@ dependencies: remark-stringify: 10.0.2 sortablejs: 1.15.0 ua-parser-js: 1.0.2 - unidata.js: 0.7.1 + unidata.js: 0.7.2 unified: 10.1.2 unist-builder: 3.0.0 unist-util-visit: 4.1.0 @@ -777,10 +777,10 @@ packages: '@ethersproject/bignumber': 5.7.0 '@ethersproject/bytes': 5.7.0 '@ethersproject/logger': 5.7.0 - '@ethersproject/networks': 5.7.0 + '@ethersproject/networks': 5.7.1 '@ethersproject/properties': 5.7.0 '@ethersproject/transactions': 5.7.0 - '@ethersproject/web': 5.7.0 + '@ethersproject/web': 5.7.1 dev: false /@ethersproject/abstract-signer/5.6.2: @@ -1044,12 +1044,6 @@ packages: '@ethersproject/logger': 5.6.0 dev: false - /@ethersproject/networks/5.7.0: - resolution: {integrity: sha512-MG6oHSQHd4ebvJrleEQQ4HhVu8Ichr0RDYEfHzsVAVjHNM+w36x9wp9r+hf1JstMXtseXDtkiVoARAG6M959AA==} - dependencies: - '@ethersproject/logger': 5.7.0 - dev: false - /@ethersproject/networks/5.7.1: resolution: {integrity: sha512-n/MufjFYv3yFcUyfhnXotyDlNdFb7onmkSy8aQERi2PjNcnWQ66xXxa3XlS8nCcA8aJKJjIIMNJTC7tu80GwpQ==} dependencies: @@ -1336,16 +1330,6 @@ packages: '@ethersproject/strings': 5.6.1 dev: false - /@ethersproject/web/5.7.0: - resolution: {integrity: sha512-ApHcbbj+muRASVDSCl/tgxaH2LBkRMEYfLOLVa0COipx0+nlu0QKet7U2lEg0vdkh8XRSLf2nd1f1Uk9SrVSGA==} - dependencies: - '@ethersproject/base64': 5.7.0 - '@ethersproject/bytes': 5.7.0 - '@ethersproject/logger': 5.7.0 - '@ethersproject/properties': 5.7.0 - '@ethersproject/strings': 5.7.0 - dev: false - /@ethersproject/web/5.7.1: resolution: {integrity: sha512-Gueu8lSvyjBWL4cYsWsjh6MtMwM0+H4HvqFPZfB6dV8ctbP9zFAO73VG1cMWae0FLPCtz0peKPpZY8/ugJJX2w==} dependencies: @@ -2044,8 +2028,8 @@ packages: '@types/node': 17.0.45 dev: false - /@types/bn.js/5.1.0: - resolution: {integrity: sha512-QSSVYj7pYFN49kW77o2s9xTCwZ8F2xLbjLLSEVh8D2F4JUhZtPAGOFLTD+ffqksBx/u4cE/KImFjyhqCjn/LIA==} + /@types/bn.js/5.1.1: + resolution: {integrity: sha512-qNrYbZqMx0uJAfKnKclPh+dTwK33KfLHYqtyODwd5HnXOjnkhc4qgn3BrK6RWyGZm5+sIFE7Q7Vz6QQtJB7w7g==} dependencies: '@types/node': 17.0.45 dev: false @@ -3839,6 +3823,23 @@ packages: - utf-8-validate dev: false + /crossbell.js/0.10.28: + resolution: {integrity: sha512-rreZtNkcQsN2HTCPPQcul4lspjoJU1lu6leUXxjcLvoQ3pfW6VKpPef9wHnv7md1GDJgbmMY8ou1nuRmqJAN7Q==} + dependencies: + async-retry: 1.3.3 + ethers: 5.7.1 + fetch-undici: 3.0.1 + is-ipfs: 6.0.2 + query-string: 7.1.1 + ts-mixer: 6.0.1 + web3-providers-ws: 1.8.0 + transitivePeerDependencies: + - bufferutil + - node-fetch + - supports-color + - utf-8-validate + dev: false + /css-mediaquery/0.1.2: resolution: {integrity: sha512-COtn4EROW5dBGlE/4PiKnh6rZpAPxDeFLaEEwt4i10jpDMFt2EhQGS79QmmrO+iKCHv0PU/HrOWEhijFd1x99Q==} dev: false @@ -4268,7 +4269,7 @@ packages: resolution: {integrity: sha512-NJ6Yn3FuDinBaBRWl/q5X/s4koRHBrgKAu+yGI6JCBeiu3qrcbJhwT2GeR/EXVfylRk8dpQVJoLEFhK+Mu31NA==} dependencies: d: 1.0.1 - ext: 1.6.0 + ext: 1.7.0 dev: false /esbuild-android-64/0.14.47: @@ -4918,7 +4919,7 @@ packages: resolution: {integrity: sha512-SDl5kKrQAudFBUe5OJM9Ac6WmMyYmXX/6sTmLZ3ffG2eY6ZIGBes3pEDxNN6V72WyOw4CPD5RomKdsa8DAAwLg==} engines: {node: '>=10.0.0'} dependencies: - '@types/bn.js': 5.1.0 + '@types/bn.js': 5.1.1 bn.js: 5.2.1 create-hash: 1.2.0 ethereum-cryptography: 0.1.3 @@ -5071,8 +5072,8 @@ packages: strip-final-newline: 3.0.0 dev: true - /ext/1.6.0: - resolution: {integrity: sha512-sdBImtzkq2HpkdRLtlLWDa6w4DX22ijZLKx8BMPUuKe1c5lbN6xwQDQCxSfxBQnHZ13ls/FH0MQZx/q/gr6FQg==} + /ext/1.7.0: + resolution: {integrity: sha512-6hxeJYaL110a9b5TEJSj0gojyHQAmA2ch5Os+ySCiA1QGdS697XWY1pzsrSjqA9LDEEgdB/KypIlR59RcLuHYw==} dependencies: type: 2.7.2 dev: false @@ -5137,7 +5138,7 @@ packages: resolution: {integrity: sha512-UHHu1HqW22ZhK6C/1Zmjf7mQpOwPwLYZ+xcsOgpzistONU8QqvCop6Od29p/kw1GUVoq2Ihu6ItpKLtlojx4FQ==} engines: {node: '>=16'} dependencies: - undici: 5.9.1 + undici: 5.10.0 dev: false /file-entry-cache/6.0.1: @@ -9181,18 +9182,18 @@ packages: jiti: 1.14.0 dev: true + /undici/5.10.0: + resolution: {integrity: sha512-c8HsD3IbwmjjbLvoZuRI26TZic+TSEe8FPMLLOkN1AfYRhdjnKBU6yL+IwcSCbdZiX4e5t0lfMDLDCqj4Sq70g==} + engines: {node: '>=12.18'} + dev: false + /undici/5.5.1: resolution: {integrity: sha512-MEvryPLf18HvlCbLSzCW0U00IMftKGI5udnjrQbC5D4P0Hodwffhv+iGfWuJwg16Y/TK11ZFK8i+BPVW2z/eAw==} engines: {node: '>=12.18'} dev: true - /undici/5.9.1: - resolution: {integrity: sha512-6fB3a+SNnWEm4CJbgo0/CWR8RGcOCQP68SF4X0mxtYTq2VNN8T88NYrWVBAeSX+zb7bny2dx2iYhP3XHi00omg==} - engines: {node: '>=12.18'} - dev: false - - /unidata.js/0.7.1: - resolution: {integrity: sha512-TBqH1+5a6j7AV5AQgAhOvdPWctVaG9k2z+wHppB8+jr+Ju17PveBX5HTX8ISjhFbZ1hf+yKfl3Sg+i9xHPHiXA==} + /unidata.js/0.7.2: + resolution: {integrity: sha512-wjFrX7o5vkVhUyDstn2t5xMmvoEqcpGMKMayFz6EvSg5feNIqwYVDkdaRXB2U0qUIOekdKAYPjqk4tejAw8Gtw==} dependencies: '@types/lodash': 4.14.185 '@urql/core': 2.5.0_graphql@16.5.0 diff --git a/src/components/site/SiteHeader.tsx b/src/components/site/SiteHeader.tsx index a5e9c714..8970b70e 100644 --- a/src/components/site/SiteHeader.tsx +++ b/src/components/site/SiteHeader.tsx @@ -24,6 +24,7 @@ import { DotsHorizontalIcon, RssIcon } from "@heroicons/react/solid" import type { Link } from "unidata.js" import { getSiteSubscriptions } from "~/models/site.model" import { CharacterList } from "~/components/common/CharacterList" +import type { Links } from "unidata.js" export type HeaderLinkType = { icon?: React.ReactNode @@ -52,7 +53,8 @@ const HeaderLink: React.FC<{ link: HeaderLinkType }> = ({ link }) => { export const SiteHeader: React.FC<{ site?: Profile | undefined -}> = ({ site }) => { + subscriptions?: Links | null +}> = ({ site, subscriptions }) => { const { address } = useAccount() const subscribeToSite = useSubscribeToSite() const unsubscribeFromSite = useUnsubscribeFromSite() @@ -88,10 +90,6 @@ export const SiteHeader: React.FC<{ siteId: site?.username || "", }) - const siteSubscriptions = useGetSiteSubscriptions({ - siteId: site?.username || "", - }) - useEffect(() => { if ( followProgress && @@ -165,11 +163,11 @@ export const SiteHeader: React.FC<{ const [siteSubscriptionList, setSiteSubscriptionList] = useState([]) const [cursor, setCursor] = useState() useEffect(() => { - if (siteSubscriptions.isSuccess) { - setSiteSubscriptionList(siteSubscriptions.data?.list || []) - setCursor(siteSubscriptions.data?.cursor) + if (subscriptions && !siteSubscriptionList.length) { + setSiteSubscriptionList(subscriptions.list || []) + setCursor(subscriptions.cursor) } - }, [siteSubscriptions.isSuccess]) + }, [subscriptions]) const loadMoreSubscriptions = async () => { if (cursor) { @@ -226,13 +224,13 @@ export const SiteHeader: React.FC<{ > )}
- {siteSubscriptions.data ? ( + {subscriptions ? ( setIsFollowListOpen(true)} > - {siteSubscriptions.data.total} + {subscriptions.total} {" "} Followers diff --git a/src/components/site/SiteLayout.tsx b/src/components/site/SiteLayout.tsx index 2fb503b0..29509d71 100644 --- a/src/components/site/SiteLayout.tsx +++ b/src/components/site/SiteLayout.tsx @@ -8,6 +8,7 @@ import { SiteHeader } from "./SiteHeader" import { useRouter } from "next/router" import { useStore } from "~/lib/store" import { BlockchainInfo } from "~/components/common/BlockchainInfo" +import type { Links } from "unidata.js" export type SiteLayoutProps = { site?: Profile @@ -16,6 +17,7 @@ export type SiteLayoutProps = { title?: string | null ogDescription?: string | null page?: Note + subscriptions?: Links | null } export const SiteLayout: React.FC = ({ @@ -25,6 +27,7 @@ export const SiteLayout: React.FC = ({ title, ogDescription, page, + subscriptions, }) => { const router = useRouter() const setSubscribeModalOpened = useStore( @@ -48,7 +51,7 @@ export const SiteLayout: React.FC = ({ image={page?.cover || getUserContentsUrl(site?.avatars?.[0])} icon={getUserContentsUrl(site?.avatars?.[0])} /> - +
{ const domainOrSubdomain = ctx.params!.site as string @@ -20,6 +24,9 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => { visibility: PageVisibilityEnum.Published, render: true, }) + await prefetchGetSiteSubscriptions({ + siteId: domainOrSubdomain, + }) return { props: { @@ -38,9 +45,12 @@ function SiteIndexPage({ domainOrSubdomain }: { domainOrSubdomain: string }) { visibility: PageVisibilityEnum.Published, render: true, }) + const subscriptions = useGetSiteSubscriptions({ + siteId: domainOrSubdomain, + }) return ( - + ) diff --git a/src/queries/site.server.ts b/src/queries/site.server.ts index 76dc055f..e5379262 100644 --- a/src/queries/site.server.ts +++ b/src/queries/site.server.ts @@ -30,3 +30,25 @@ export const fetchGetSite = async (input: string) => { return data } } + +export const prefetchGetSiteSubscriptions = async ( + input: Parameters[0], +) => { + const key = ["getSite", input] + const data = queryClientServer.getQueryData(key) + if (!data) { + await queryClientServer.prefetchQuery( + ["getSiteSubscriptions", input], + async () => { + return siteModel.getSiteSubscriptions(input) + }, + ) + } else { + queryClientServer.prefetchQuery( + ["getSiteSubscriptions", input], + async () => { + return siteModel.getSiteSubscriptions(input) + }, + ) + } +}