feat: render home page via api to reduce js size

This commit is contained in:
DIYgod 2022-11-17 20:22:34 +00:00
parent cd41e61363
commit cd2fdaf3da
No known key found for this signature in database
GPG Key ID: D159328F47A80DCA
10 changed files with 25 additions and 19 deletions

View File

@ -70,7 +70,6 @@ export const PagesManager: React.FC<{
site: subdomain!,
take: 100,
visibility,
render: true,
})
const tabItems: TabItem[] = [

View File

@ -59,7 +59,6 @@ export const getServerSideProps = async (
...(options?.take && { take: options.take }),
type: "post",
visibility: PageVisibilityEnum.Published,
render: true,
...(tag && { tags: [tag] }),
},
queryClient,

View File

@ -195,9 +195,6 @@ export async function getPagesBySite(
visibility?: PageVisibilityEnum | null
take?: number | null
cursor?: string | null
includeContent?: boolean
includeExcerpt?: boolean
render?: boolean
tags?: string[]
},
customUnidata?: Unidata,
@ -284,7 +281,7 @@ export async function getPagesBySite(
pages.total = pages.total - ((input.take || 10) - pages.list.length)
pages?.list.map((page) => {
expandPage(page, input.render || false)
expandPage(page, true)
delete page.body
return page
})

View File

@ -43,7 +43,7 @@ const expandSite = (site: Profile) => {
(a: any) => a.trait_type === "xlog_custom_domain",
)?.value || ""
site.name = site.name || site.username
site.description = renderPageContent(site.bio || "", true).contentHTML
site.description = site.bio
if (site.avatars) {
site.avatars = site.avatars.map((avatar) => toGateway(avatar))

View File

@ -38,7 +38,6 @@ function SiteArchivesPage({
take: 100,
type: "post",
visibility: PageVisibilityEnum.Published,
render: true,
})
return (

View File

@ -19,7 +19,6 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {
site: domainOrSubdomain,
type: "post",
visibility: PageVisibilityEnum.Published,
render: true,
},
queryClient,
)

View File

@ -25,7 +25,6 @@ function SiteIndexPage({ domainOrSubdomain }: { domainOrSubdomain: string }) {
site: domainOrSubdomain,
type: "post",
visibility: PageVisibilityEnum.Published,
render: true,
})
return (

View File

@ -43,7 +43,6 @@ function SiteTagPage({
take: 100,
type: "post",
visibility: PageVisibilityEnum.Published,
render: true,
tags: [tag],
})

13
src/pages/api/pages.ts Normal file
View File

@ -0,0 +1,13 @@
import { NextApiRequest, NextApiResponse } from "next"
import { getPagesBySite } from "~/models/page.model"
export default async function handler(
req: NextApiRequest,
res: NextApiResponse,
) {
const query = req.query
const result = await getPagesBySite(query as any)
res.status(200).json(result)
}

View File

@ -11,17 +11,19 @@ import { useContract } from "./crossbell"
export const useGetPagesBySite = (
input: Parameters<typeof pageModel.getPagesBySite>[0],
) => {
const unidata = useUnidata()
return useInfiniteQuery({
queryKey: ["getPagesBySite", input.site, input],
queryFn: async ({ pageParam }) => {
return pageModel.getPagesBySite(
{
...input,
cursor: pageParam,
},
unidata,
)
const result: ReturnType<typeof pageModel.getPagesBySite> = await (
await fetch(
"/api/pages?" +
new URLSearchParams({
...input,
...(pageParam && { cursor: pageParam }),
} as any),
)
).json()
return result
},
getNextPageParam: (lastPage) => lastPage.cursor,
})