Skip to content

Commit d0c630b

Browse files
committed
feat: cache tag
1 parent 77724e0 commit d0c630b

File tree

27 files changed

+185
-75
lines changed

27 files changed

+185
-75
lines changed

app/(protected)/admin/books/[id]/page.tsx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ import { Avatar, AvatarFallback } from '@/components/ui/avatar'
1717
import { Button } from '@/components/ui/button'
1818
import { Verify } from '@/lib/firebase/firebase'
1919
import { DataRecentBorrows } from '@/components/books/DataRecentBorrows'
20+
import {
21+
CACHE_KEY_BORROWS,
22+
CACHE_KEY_REVIEWS,
23+
CACHE_TTL_SECONDS,
24+
} from '@/lib/consts'
2025

2126
export default async function BookDetailsPage({
2227
params,
@@ -31,8 +36,28 @@ export default async function BookDetailsPage({
3136

3237
const [bookRes, reviewsRes, borrowsRes] = await Promise.all([
3338
getBook({ id, include_stats: 'true' }),
34-
getListReviews({ book_id: id, limit: 3 }, { headers }),
35-
getListBorrows({ book_id: id, limit: 3 }, { headers }),
39+
getListReviews(
40+
{ book_id: id, limit: 3 },
41+
{
42+
headers,
43+
cache: 'force-cache',
44+
next: {
45+
tags: [CACHE_KEY_REVIEWS, id],
46+
revalidate: CACHE_TTL_SECONDS,
47+
},
48+
}
49+
),
50+
getListBorrows(
51+
{ book_id: id, limit: 3 },
52+
{
53+
headers,
54+
cache: 'force-cache',
55+
next: {
56+
tags: [CACHE_KEY_BORROWS, id],
57+
revalidate: CACHE_TTL_SECONDS,
58+
},
59+
}
60+
),
3661
])
3762

3863
if ('error' in bookRes) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
import { getListBooks } from '@/lib/api/book'
1818
import Link from 'next/link'
1919
import type { Metadata } from 'next'
20-
import { SITE_NAME } from '@/lib/consts'
20+
import { CACHE_KEY_BOOKS, CACHE_TTL_SECONDS, SITE_NAME } from '@/lib/consts'
2121
import { Plus, Settings, Upload } from 'lucide-react'
2222
import { Badge } from '@/components/ui/badge'
2323
import { ListBook } from '@/components/books/ListBook'
@@ -62,8 +62,8 @@ export default async function Books({
6262
{
6363
cache: 'force-cache',
6464
next: {
65-
tags: ['books'],
66-
revalidate: 300,
65+
tags: [CACHE_KEY_BOOKS],
66+
revalidate: CACHE_TTL_SECONDS,
6767
},
6868
}
6969
)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { Verify } from '@/lib/firebase/firebase'
2121
import { BookUser, Scan } from 'lucide-react'
2222
import Link from 'next/link'
2323
import type { Metadata } from 'next'
24-
import { SITE_NAME } from '@/lib/consts'
24+
import { CACHE_KEY_BORROWS, CACHE_TTL_SECONDS, SITE_NAME } from '@/lib/consts'
2525
import { DropdownMenuBorrow } from '@/components/borrows/DropdownMenuBorrow'
2626
import { BtnScanReturnBorrow } from '@/components/borrows/ModalReturnBorrow'
2727
import { TabLink } from '@/components/borrows/TabLink'
@@ -93,8 +93,8 @@ export default async function Borrows({
9393
headers,
9494
cache: 'force-cache',
9595
next: {
96-
tags: ['borrows'],
97-
revalidate: 60,
96+
tags: [CACHE_KEY_BORROWS],
97+
revalidate: CACHE_TTL_SECONDS,
9898
},
9999
}
100100
)

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export default async function DashboardPage({
5151
}>
5252
}) {
5353
const claims = await IsLoggedIn()
54-
if (!claims) redirect('/login?from=/admin/dashboard')
54+
if (!claims) redirect('/refresh?from=/admin/dashboard')
5555

5656
if (
5757
claims.librarease.role === 'USER' &&
@@ -119,9 +119,9 @@ export default async function DashboardPage({
119119
</BreadcrumbList>
120120
</Breadcrumb>
121121

122-
<div className="grid my-4 grid-cols-1 gap-4 md:grid-cols-2">
122+
<div className="grid my-4 grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-3">
123123
<DateRangeSelector
124-
className="col-span-2"
124+
className="col-span-2 lg:col-span-3"
125125
range={{ from: fromDate, to: toDate }}
126126
onChangeAction={onDateRangeChange}
127127
/>

app/(protected)/admin/page.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,12 @@ const menuItems = [
5454
export default async function LibraryDashboard() {
5555
const claim = await IsLoggedIn()
5656

57+
if (!claim) {
58+
redirect('/refresh?from=/admin')
59+
}
60+
5761
// TODO: remove after the custom claim is set
5862
if (
59-
!claim ||
6063
!claim.librarease ||
6164
(claim.librarease.role == 'USER' &&
6265
claim.librarease.admin_libs.length === 0 &&

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { cookies } from 'next/headers'
1717
import Image from 'next/image'
1818
import Link from 'next/link'
1919
import clsx from 'clsx'
20+
import { CACHE_KEY_REVIEWS, CACHE_TTL_SECONDS } from '@/lib/consts'
2021

2122
export default async function ReviewsPage({
2223
searchParams,
@@ -52,7 +53,11 @@ export default async function ReviewsPage({
5253
comment,
5354
limit,
5455
},
55-
{ headers }
56+
{
57+
headers,
58+
cache: 'force-cache',
59+
next: { tags: [CACHE_KEY_REVIEWS], revalidate: CACHE_TTL_SECONDS },
60+
}
5661
)
5762

5863
if ('error' in res) {

app/(protected)/books/[id]/page.tsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { IsLoggedIn, Verify } from '@/lib/firebase/firebase'
1818
import { Review } from '@/components/reviews/Review'
1919
import { DataRecentBorrows } from '@/components/books/DataRecentBorrows'
2020
import { DateTime } from '@/components/common/DateTime'
21+
import { CACHE_KEY_REVIEWS, CACHE_TTL_SECONDS } from '@/lib/consts'
2122

2223
export default async function BookDetailsPage({
2324
params,
@@ -34,10 +35,27 @@ export default async function BookDetailsPage({
3435

3536
const [bookRes, reviewsRes, myReviewsRes] = await Promise.all([
3637
getBook({ id, include_stats: 'true' }),
37-
getListReviews({ book_id: id, limit: 3 }, { headers }),
38+
getListReviews(
39+
{ book_id: id, limit: 3 },
40+
{
41+
headers,
42+
cache: 'force-cache',
43+
next: {
44+
tags: [CACHE_KEY_REVIEWS, id],
45+
revalidate: CACHE_TTL_SECONDS,
46+
},
47+
}
48+
),
3849
getListReviews(
3950
{ book_id: id, limit: 3, user_id: claim?.librarease.id },
40-
{ headers }
51+
{
52+
headers,
53+
cache: 'force-cache',
54+
next: {
55+
tags: [CACHE_KEY_REVIEWS, id],
56+
revalidate: CACHE_TTL_SECONDS,
57+
},
58+
}
4159
),
4260
])
4361

app/(protected)/books/[id]/reviews/page.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
PaginationPrevious,
1414
} from '@/components/ui/pagination'
1515
import { Route } from 'next'
16+
import { CACHE_KEY_REVIEWS, CACHE_TTL_SECONDS } from '@/lib/consts'
1617

1718
export default async function BookReviewsPage({
1819
params,
@@ -43,7 +44,14 @@ export default async function BookReviewsPage({
4344
comment,
4445
limit,
4546
},
46-
{ headers }
47+
{
48+
headers,
49+
cache: 'force-cache',
50+
next: {
51+
tags: [CACHE_KEY_REVIEWS, id],
52+
revalidate: CACHE_TTL_SECONDS,
53+
},
54+
}
4755
)
4856

4957
if ('error' in res) {

app/(protected)/books/page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
import { getListBooks } from '@/lib/api/book'
1717
import Link from 'next/link'
1818
import type { Metadata } from 'next'
19-
import { SITE_NAME } from '@/lib/consts'
19+
import { CACHE_KEY_BOOKS, CACHE_TTL_SECONDS, SITE_NAME } from '@/lib/consts'
2020
import { BellRing } from 'lucide-react'
2121
import { Badge } from '@/components/ui/badge'
2222
import { ListBook } from '@/components/books/ListBook'
@@ -59,8 +59,8 @@ export default async function UserBooks({
5959
const res = await getListBooks(query, {
6060
cache: 'force-cache',
6161
next: {
62-
tags: ['books'],
63-
revalidate: 300,
62+
tags: [CACHE_KEY_BOOKS],
63+
revalidate: CACHE_TTL_SECONDS,
6464
},
6565
})
6666

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
} from '@/components/ui/pagination'
1616
import Link from 'next/link'
1717
import type { Metadata } from 'next'
18-
import { SITE_NAME } from '@/lib/consts'
18+
import { CACHE_KEY_WATCHLIST, CACHE_TTL_SECONDS, SITE_NAME } from '@/lib/consts'
1919
import { Badge } from '@/components/ui/badge'
2020
import { ListBook } from '@/components/books/ListBook'
2121
import { Verify } from '@/lib/firebase/firebase'
@@ -53,7 +53,14 @@ export default async function UserBooks({
5353

5454
const headers = await Verify({ from: '/books/watchlist' })
5555

56-
const res = await getListWatchlist(query, { headers })
56+
const res = await getListWatchlist(query, {
57+
headers,
58+
cache: 'force-cache',
59+
next: {
60+
tags: [CACHE_KEY_WATCHLIST],
61+
revalidate: CACHE_TTL_SECONDS,
62+
},
63+
})
5764

5865
if ('error' in res) {
5966
console.log(res)

0 commit comments

Comments
 (0)