Skip to content

Commit 269189e

Browse files
committed
feat: some ui optimize and next cache hit
1 parent 06a53a9 commit 269189e

File tree

12 files changed

+68
-47
lines changed

12 files changed

+68
-47
lines changed

src/app/(home)/layout.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { apiClient } from '~/lib/request'
88

99
import { queryKey } from './query'
1010

11+
export const revalidate = 60
12+
1113
export default async function HomeLayout(props: PropsWithChildren) {
1214
const queryClient = getQueryClient()
1315
await queryClient.fetchQuery(queryKey, async () => {

src/app/(home)/page.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ const FriendScreen = () => {
494494
),
495495
).slice(0, 20)
496496
}, []),
497-
staleTime: 1000 * 60,
497+
staleTime: 1000 * 60 * 10,
498498
})
499499
return (
500500
<Screen className="flex h-auto min-h-[100vh] center">

src/app/api/note/latest/route.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { NextResponse } from 'next/server'
2+
import type { NoteWrappedPayload } from '@mx-space/api-client'
3+
4+
import { apiClient } from '~/lib/request'
5+
6+
export const revalidate = 60
7+
8+
export const GET = async () => {
9+
const data = await fetch(apiClient.note.proxy.latest.toString(true), {
10+
next: {
11+
revalidate: 30,
12+
},
13+
}).then((res) => res.json() as Promise<NoteWrappedPayload>)
14+
15+
return NextResponse.json({
16+
nid: data.data.nid,
17+
})
18+
}

src/app/layout.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import { SonnerContainer } from './SonnerContainer'
2626
const { version } = PKG
2727
init()
2828

29+
export const revalidate = 60
30+
2931
const getAppConfig = cache(() => {
3032
return get('config') as Promise<AppConfig>
3133
})

src/app/notes/page.tsx

+19-29
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,37 @@
11
'use client'
22

3-
import { useQuery, useQueryClient } from '@tanstack/react-query'
4-
import { useEffect, useRef } from 'react'
3+
import { useLayoutEffect } from 'react'
54
import { useRouter } from 'next/navigation'
65

76
import { Loading } from '~/components/ui/loading'
8-
import { apiClient } from '~/lib/request'
97
import { routeBuilder, Routes } from '~/lib/route-builder'
10-
import { queries } from '~/queries/definition'
118

129
import { Paper } from '../../components/layout/container/Paper'
1310

11+
export const revalidate = 60
12+
1413
export default function Page() {
15-
const { data } = useQuery(
16-
['note', 'latest'],
17-
async () => (await apiClient.note.getLatest()).$serialized,
18-
{
19-
cacheTime: 1,
20-
},
21-
)
22-
const queryClient = useQueryClient()
2314
const router = useRouter()
24-
const onceRef = useRef(false)
25-
useEffect(() => {
26-
if (!data) return
27-
if (onceRef.current) return
15+
useLayoutEffect(() => {
16+
;(async () => {
17+
const { nid } = await fetch('/api/note/latest', {
18+
next: {
19+
revalidate: 60,
20+
},
21+
}).then(
22+
(res) =>
23+
res.json() as Promise<{
24+
nid: number
25+
}>,
26+
)
2827

29-
queryClient.setQueryData(
30-
queries.note.byNid(data.data.nid.toString()).queryKey,
31-
data,
32-
)
33-
onceRef.current = true
34-
const id = setTimeout(() => {
35-
router.replace(
28+
router.push(
3629
routeBuilder(Routes.Note, {
37-
id: data.data.nid.toString(),
30+
id: nid,
3831
}),
3932
)
40-
}, 1)
41-
return () => {
42-
clearTimeout(id)
43-
}
44-
}, [data])
33+
})()
34+
}, [])
4535

4636
return (
4737
<Paper>

src/components/layout/header/internal/MenuPopover.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ export const MenuPopover: Component<{
1414
return (
1515
<FloatPopover
1616
strategy="fixed"
17-
headless
1817
placement="bottom"
1918
offset={10}
2019
popoverWrapperClassNames="z-[19] relative"

src/components/layout/header/internal/SiteOwnerAvatar.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ export const SiteOwnerAvatar: Component = ({ className }) => {
1717
const { data: isLiving } = useQuery({
1818
queryKey: ['live-check'],
1919
queryFn: () =>
20-
fetch(`/api/bilibili/check_live?liveId=${liveId}`)
20+
fetch(`/api/bilibili/check_live?liveId=${liveId}`, {
21+
next: {
22+
revalidate: 1,
23+
},
24+
})
2125
.then((res) => res.json())
2226
.catch(() => null),
2327
select: useCallback((data: any) => {

src/components/ui/float-popover/FloatPopover.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -236,9 +236,13 @@ export function FloatPopover<T extends {}>(
236236
role="dialog"
237237
aria-modal="true"
238238
className={clsxm(
239-
'!shadow-out-sm focus:!shadow-out-sm focus-visible:!shadow-out-sm',
240-
'rounded-xl border border-zinc-400/20 p-4 shadow-lg outline-none backdrop-blur-lg dark:border-zinc-500/30',
241-
'bg-slate-50/80 dark:bg-neutral-900/80',
239+
!headless && [
240+
'!shadow-out-sm focus:!shadow-out-sm focus-visible:!shadow-out-sm',
241+
'rounded-xl border border-zinc-400/20 p-4 shadow-lg outline-none backdrop-blur-lg dark:border-zinc-500/30',
242+
'bg-slate-50/80 dark:bg-neutral-900/80',
243+
],
244+
245+
'relative z-[2]',
242246

243247
headless && styles['headless'],
244248
animate && styles['animate'],

src/components/ui/float-popover/index.module.css

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
.popover-root {
2-
@apply relative z-[2] overflow-hidden p-4 shadow-out-sm;
3-
}
4-
51
.popover-root,
62
.headless {
73
opacity: 0;
84
transform: translateY(-10px);
9-
transition: opacity 0.2s ease-in-out, transform 0.2s ease-in-out;
5+
transition:
6+
opacity 0.2s ease-in-out,
7+
transform 0.2s ease-in-out;
108

119
@apply delay-100;
1210
}

src/components/widgets/comment/CommentBox/UniversalTextArea.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export const UniversalTextArea = () => {
6767
placeholder={placeholder}
6868
>
6969
<CommentBoxSlotPortal>
70-
<FloatPopover trigger="click" TriggerComponent={EmojiButton}>
70+
<FloatPopover trigger="click" TriggerComponent={EmojiButton} headless>
7171
<EmojiPicker onEmojiSelect={handleInsertEmoji} />
7272
</FloatPopover>
7373
</CommentBoxSlotPortal>

src/components/widgets/shared/EmojiPicker.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ export const EmojiPicker: FC<{
1111
const { data, isLoading } = useQuery({
1212
queryKey: ['emojidata'],
1313
queryFn: () =>
14-
fetch('https://cdn.jsdelivr.net/npm/@emoji-mart/data').then((response) =>
15-
response.json(),
16-
),
14+
fetch('https://cdn.jsdelivr.net/npm/@emoji-mart/data', {
15+
next: {
16+
revalidate: 60 * 60 * 24 * 7,
17+
},
18+
}).then((response) => response.json()),
1719

1820
staleTime: Infinity,
1921
})

src/components/widgets/xlog/XLogSummary.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ const XLogSummary: FC<{
1616
[`getSummary`, cid],
1717
async ({ queryKey }) => {
1818
const [, cid] = queryKey
19-
const data = await fetch(`/api/xlog/summary?cid=${cid}`).then((res) =>
20-
res.json(),
21-
)
19+
const data = await fetch(`/api/xlog/summary?cid=${cid}`, {
20+
next: {
21+
revalidate: 60 * 10,
22+
},
23+
}).then((res) => res.json())
2224
if (!data) throw new Error('请求错误')
2325
return data
2426
},

0 commit comments

Comments
 (0)