Skip to content

Commit 77724e0

Browse files
committed
feat: cache init
1 parent 16adc18 commit 77724e0

File tree

8 files changed

+61
-25
lines changed

8 files changed

+61
-25
lines changed

app/(protected)/admin/books/page.tsx

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,24 @@ export default async function Books({
4949
const cookieName = process.env.LIBRARY_COOKIE_NAME as string
5050
const libID = cookieStore.get(cookieName)?.value
5151

52-
const res = await getListBooks({
53-
sort_by: 'created_at',
54-
sort_in: 'desc',
55-
limit: limit,
56-
skip: skip,
57-
title: sp?.title,
58-
library_id: libID,
59-
include_stats: 'true',
60-
})
52+
const res = await getListBooks(
53+
{
54+
sort_by: 'created_at',
55+
sort_in: 'desc',
56+
limit: limit,
57+
skip: skip,
58+
title: sp?.title,
59+
library_id: libID,
60+
include_stats: 'true',
61+
},
62+
{
63+
cache: 'force-cache',
64+
next: {
65+
tags: ['books'],
66+
revalidate: 300,
67+
},
68+
}
69+
)
6170

6271
if ('error' in res) {
6372
console.log(res)

app/(protected)/admin/borrows/page.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ export default async function Borrows({
9191
},
9292
{
9393
headers,
94+
cache: 'force-cache',
95+
next: {
96+
tags: ['borrows'],
97+
revalidate: 60,
98+
},
9499
}
95100
)
96101

app/(protected)/books/page.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ export default async function UserBooks({
5656

5757
await Verify({ from: '/books' })
5858

59-
const res = await getListBooks(query)
59+
const res = await getListBooks(query, {
60+
cache: 'force-cache',
61+
next: {
62+
tags: ['books'],
63+
revalidate: 300,
64+
},
65+
})
6066

6167
if ('error' in res) {
6268
console.log(res)

app/(protected)/borrows/page.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ export default async function Borrows({
8181
},
8282
{
8383
headers,
84+
cache: 'force-cache',
85+
next: {
86+
tags: ['borrows'],
87+
revalidate: 60,
88+
},
8489
}
8590
)
8691

app/explore/books/page.tsx

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,23 @@ export default async function ExploreBooks({
4141
const limit = Number(sp?.limit ?? 20)
4242
const library_id = sp?.library_id
4343

44-
const res = await getListBooks({
45-
sort_by: 'created_at',
46-
sort_in: 'desc',
47-
limit: limit,
48-
skip: skip,
49-
title: sp?.title,
50-
...(library_id ? { library_id } : {}),
51-
})
44+
const res = await getListBooks(
45+
{
46+
sort_by: 'created_at',
47+
sort_in: 'desc',
48+
limit: limit,
49+
skip: skip,
50+
title: sp?.title,
51+
...(library_id ? { library_id } : {}),
52+
},
53+
{
54+
cache: 'force-cache',
55+
next: {
56+
tags: ['books'],
57+
revalidate: 300,
58+
},
59+
}
60+
)
5261

5362
if ('error' in res) {
5463
console.log(res)

lib/actions/return-borrow.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use server'
22

3-
import { revalidatePath } from 'next/cache'
3+
import { revalidatePath, revalidateTag } from 'next/cache'
44
import { returnBorrow } from '../api/borrow'
55
import { Verify } from '../firebase/firebase'
66

@@ -39,6 +39,7 @@ export async function returnBorrowAction({
3939
} finally {
4040
revalidatePath(`/admin/borrows/${id}`)
4141
revalidatePath(`/admin/borrows`)
42+
revalidateTag('borrows', 'max')
4243
// revalidatePath(`/borrows/${id}`)
4344
// revalidatePath('/borrows')
4445
// revalidatePath('/admin/books')

lib/actions/undo-return.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use server'
22

3-
import { revalidatePath } from 'next/cache'
3+
import { revalidatePath, revalidateTag } from 'next/cache'
44
import { deleteReturn } from '../api/borrow'
55
import { Verify } from '../firebase/firebase'
66

@@ -28,6 +28,7 @@ export async function undoReturnAction(id: string) {
2828
} finally {
2929
revalidatePath(`/admin/borrows/${id}`)
3030
revalidatePath(`/admin/borrows`)
31+
revalidateTag('borrows', 'max')
3132
// revalidatePath(`/borrows/${id}`)
3233
// revalidatePath('/borrows')
3334
// revalidatePath('/admin/books')

lib/api/book.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ type GetListBooksQuery = QueryParams<
1313
}
1414
type GetListBooksResponse = Promise<ResList<Book>>
1515

16-
export const getListBooks = async ({
17-
ids,
18-
...query
19-
}: GetListBooksQuery): GetListBooksResponse => {
16+
export const getListBooks = async (
17+
{ ids, ...query }: GetListBooksQuery,
18+
init?: RequestInit
19+
): GetListBooksResponse => {
2020
const url = new URL(BOOKS_URL)
2121
Object.entries(query).forEach(([key, value]) => {
2222
if (value) {
@@ -27,7 +27,7 @@ export const getListBooks = async ({
2727
url.searchParams.append('ids', ids.join(','))
2828
}
2929

30-
const response = await fetch(url.toString())
30+
const response = await fetch(url.toString(), init)
3131
return response.json()
3232
}
3333

0 commit comments

Comments
 (0)