Skip to content

Commit a5a884e

Browse files
committed
perf: align caching with next-sanity defineLive best practices
Reverts the custom sanityFetch/fetch.ts approach in favor of defineLive's built-in caching. defineLive automatically: - Sets cache tags (opaque sanity: prefixed tags) - Revalidates via SanityLive component listening to Live Content API - Integrates with ISR so all visitors see fresh content Changes: - Revert sanity/lib/live.ts to standard defineLive pattern - Delete sanity/lib/fetch.ts (not needed) - SanityLive always renders (required for cache revalidation) - Remove manual tags from all sanityFetch calls - Fix generateStaticParams to use client.fetch directly - Keep revalidate exports as safety net (20 pages) - Keep generateStaticParams for build-time pre-rendering (13 routes) - Simplify webhook to revalidateTag("sanity") backup
1 parent 3f4cf11 commit a5a884e

File tree

21 files changed

+66
-124
lines changed

21 files changed

+66
-124
lines changed

app/(main)/(author)/author/[slug]/page.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
AuthorQueryWithRelatedResult,
1010
} from "@/sanity/types";
1111
import { sanityFetch } from "@/sanity/lib/live";
12+
import { client } from "@/sanity/lib/client";
1213
import { authorQuery, authorQueryWithRelated } from "@/sanity/lib/queries";
1314
import { resolveOpenGraphImage } from "@/sanity/lib/utils";
1415
import CoverMedia from "@/components/cover-media";
@@ -32,7 +33,6 @@ export async function generateMetadata(
3233
query: authorQuery,
3334
params: { slug },
3435
stega: false,
35-
tags: ["author", `author:${slug}`],
3636
})
3737
).data as AuthorQueryResult;
3838

@@ -49,12 +49,10 @@ export async function generateMetadata(
4949
}
5050

5151
export async function generateStaticParams() {
52-
const { data } = await sanityFetch({
53-
query: groq`*[_type == "author" && defined(slug.current)].slug.current`,
54-
tags: ["author-list"],
55-
stega: false,
56-
});
57-
return (data as string[]).map((slug) => ({ slug }));
52+
const slugs = await client.fetch<string[]>(
53+
groq`*[_type == "author" && defined(slug.current)].slug.current`,
54+
);
55+
return slugs.map((slug) => ({ slug }));
5856
}
5957

6058
export default async function AuthorPage({ params }: { params: Params }) {
@@ -64,7 +62,6 @@ export default async function AuthorPage({ params }: { params: Params }) {
6462
sanityFetch({
6563
query: authorQueryWithRelated,
6664
params: { slug },
67-
tags: ["author", `author:${slug}`],
6865
}),
6966
]);
7067

app/(main)/(author)/authors/page/[num]/page.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import MoreContent from "@/components/more-content";
22
import type { DocCountResult } from "@/sanity/types";
33
import { sanityFetch } from "@/sanity/lib/live";
4+
import { client } from "@/sanity/lib/client";
45

56
import PaginateList from "@/components/paginate-list";
67
import { docCount } from "@/sanity/lib/queries";
@@ -13,12 +14,9 @@ type Params = Promise<{ num: string }>;
1314
export const revalidate = 60;
1415

1516
export async function generateStaticParams() {
16-
const { data } = await sanityFetch({
17-
query: groq`count(*[_type == "author" && defined(slug.current)])`,
18-
tags: ["author-list"],
19-
stega: false,
20-
});
21-
const count = data as number;
17+
const count = await client.fetch<number>(
18+
groq`count(*[_type == "author" && defined(slug.current)])`,
19+
);
2220
const perPage = LIMIT;
2321
const pages = Math.ceil(count / perPage);
2422
return Array.from({ length: pages }, (_, i) => ({ num: String(i + 1) }));
@@ -33,7 +31,6 @@ export default async function Page({ params }: { params: Params }) {
3331
params: {
3432
type: "author",
3533
},
36-
tags: ["author-list", "author"],
3734
})
3835
).data as DocCountResult;
3936

app/(main)/(guest)/guest/[slug]/page.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import type {
99
GuestQueryWithRelatedResult,
1010
} from "@/sanity/types";
1111
import { sanityFetch } from "@/sanity/lib/live";
12+
import { client } from "@/sanity/lib/client";
1213
import { guestQuery, guestQueryWithRelated } from "@/sanity/lib/queries";
1314
import { resolveOpenGraphImage } from "@/sanity/lib/utils";
1415
import CoverMedia from "@/components/cover-media";
@@ -33,7 +34,6 @@ export async function generateMetadata(
3334
query: guestQuery,
3435
params: { slug },
3536
stega: false,
36-
tags: ["guest", `guest:${slug}`],
3737
})
3838
).data as GuestQueryResult;
3939
const previousImages = (await parent).openGraph?.images || [];
@@ -49,12 +49,10 @@ export async function generateMetadata(
4949
}
5050

5151
export async function generateStaticParams() {
52-
const { data } = await sanityFetch({
53-
query: groq`*[_type == "guest" && defined(slug.current)].slug.current`,
54-
tags: ["guest-list"],
55-
stega: false,
56-
});
57-
return (data as string[]).map((slug) => ({ slug }));
52+
const slugs = await client.fetch<string[]>(
53+
groq`*[_type == "guest" && defined(slug.current)].slug.current`,
54+
);
55+
return slugs.map((slug) => ({ slug }));
5856
}
5957

6058
export default async function GuestPage({ params }: { params: Params }) {
@@ -65,7 +63,6 @@ export default async function GuestPage({ params }: { params: Params }) {
6563
sanityFetch({
6664
query: guestQueryWithRelated,
6765
params: { slug },
68-
tags: ["guest", `guest:${slug}`],
6966
}),
7067
])
7168
).map((res) => res.data) as [GuestQueryWithRelatedResult];

app/(main)/(guest)/guests/page/[num]/page.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import MoreContent from "@/components/more-content";
22
import type { DocCountResult } from "@/sanity/types";
33
import { sanityFetch } from "@/sanity/lib/live";
4+
import { client } from "@/sanity/lib/client";
45

56
import PaginateList from "@/components/paginate-list";
67
import { docCount } from "@/sanity/lib/queries";
@@ -13,12 +14,9 @@ type Params = Promise<{ num: string }>;
1314
export const revalidate = 60;
1415

1516
export async function generateStaticParams() {
16-
const { data } = await sanityFetch({
17-
query: groq`count(*[_type == "guest" && defined(slug.current)])`,
18-
tags: ["guest-list"],
19-
stega: false,
20-
});
21-
const count = data as number;
17+
const count = await client.fetch<number>(
18+
groq`count(*[_type == "guest" && defined(slug.current)])`,
19+
);
2220
const perPage = LIMIT;
2321
const pages = Math.ceil(count / perPage);
2422
return Array.from({ length: pages }, (_, i) => ({ num: String(i + 1) }));
@@ -32,7 +30,6 @@ export default async function Page({ params }: { params: Params }) {
3230
params: {
3331
type: "guest",
3432
},
35-
tags: ["guest-list", "guest"],
3633
}),
3734
])
3835
).map((res) => res.data) as [DocCountResult];

app/(main)/(podcast)/podcast/[slug]/page.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { Metadata, ResolvingMetadata } from "next";
22
import { notFound } from "next/navigation";
33
import type { PodcastQueryResult } from "@/sanity/types";
44
import { sanityFetch } from "@/sanity/lib/live";
5+
import { client } from "@/sanity/lib/client";
56
import { podcastQuery } from "@/sanity/lib/queries";
67
import { resolveOpenGraphImage } from "@/sanity/lib/utils";
78
import Podcast from "../Podcast";
@@ -22,7 +23,6 @@ export async function generateMetadata(
2223
query: podcastQuery,
2324
params: { slug },
2425
stega: false,
25-
tags: ["podcast", `podcast:${slug}`],
2626
})
2727
).data as PodcastQueryResult;
2828
const previousImages = (await parent).openGraph?.images || [];
@@ -42,12 +42,10 @@ export async function generateMetadata(
4242
}
4343

4444
export async function generateStaticParams() {
45-
const { data } = await sanityFetch({
46-
query: groq`*[_type == "podcast" && defined(slug.current)].slug.current`,
47-
tags: ["podcast-list"],
48-
stega: false,
49-
});
50-
return (data as string[]).map((slug) => ({ slug }));
45+
const slugs = await client.fetch<string[]>(
46+
groq`*[_type == "podcast" && defined(slug.current)].slug.current`,
47+
);
48+
return slugs.map((slug) => ({ slug }));
5149
}
5250

5351
export default async function PodcastPage({ params }: { params: Params }) {
@@ -58,7 +56,6 @@ export default async function PodcastPage({ params }: { params: Params }) {
5856
sanityFetch({
5957
query: podcastQuery,
6058
params: { slug },
61-
tags: ["podcast", `podcast:${slug}`],
6259
}),
6360
])
6461
).map((res) => res.data) as [PodcastQueryResult];

app/(main)/(podcast)/podcasts/page.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ export default async function Page() {
8282
await Promise.all([
8383
sanityFetch({
8484
query: podcastsQuery,
85-
tags: ["podcast-list", "podcast"],
8685
}),
8786
])
8887
).map((res) => res.data) as [PodcastsQueryResult];

app/(main)/(podcast)/podcasts/page/[num]/page.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import MoreContent from "@/components/more-content";
22
import type { DocCountResult } from "@/sanity/types";
33
import { sanityFetch } from "@/sanity/lib/live";
4+
import { client } from "@/sanity/lib/client";
45

56
import PaginateList from "@/components/paginate-list";
67
import { docCount } from "@/sanity/lib/queries";
@@ -13,12 +14,9 @@ type Params = Promise<{ num: string }>;
1314
export const revalidate = 60;
1415

1516
export async function generateStaticParams() {
16-
const { data } = await sanityFetch({
17-
query: groq`count(*[_type == "podcast" && defined(slug.current)])`,
18-
tags: ["podcast-list"],
19-
stega: false,
20-
});
21-
const count = data as number;
17+
const count = await client.fetch<number>(
18+
groq`count(*[_type == "podcast" && defined(slug.current)])`,
19+
);
2220
const perPage = LIMIT;
2321
const pages = Math.ceil(count / perPage);
2422
return Array.from({ length: pages }, (_, i) => ({ num: String(i + 1) }));
@@ -32,7 +30,6 @@ export default async function Page({ params }: { params: Params }) {
3230
params: {
3331
type: "podcast",
3432
},
35-
tags: ["podcast-list", "podcast"],
3633
}),
3734
])
3835
).map((res) => res.data) as [DocCountResult];

app/(main)/(post)/blog/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function HeroPost({
6969

7070
export default async function Page() {
7171
const [heroPost] = (
72-
await Promise.all([sanityFetch({ query: blogQuery, tags: ["post-list", "post"] })])
72+
await Promise.all([sanityFetch({ query: blogQuery })])
7373
).map((res) => res.data) as [BlogQueryResult];
7474
return (
7575
<div className="container px-5 mx-auto">

app/(main)/(post)/blog/page/[num]/page.tsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import MoreContent from "@/components/more-content";
22
import type { DocCountResult } from "@/sanity/types";
33
import { sanityFetch } from "@/sanity/lib/live";
4+
import { client } from "@/sanity/lib/client";
45

56
import PaginateList from "@/components/paginate-list";
67
import { docCount } from "@/sanity/lib/queries";
@@ -13,12 +14,9 @@ type Params = Promise<{ num: string }>;
1314
export const revalidate = 60;
1415

1516
export async function generateStaticParams() {
16-
const { data } = await sanityFetch({
17-
query: groq`count(*[_type == "post" && defined(slug.current)])`,
18-
tags: ["post-list"],
19-
stega: false,
20-
});
21-
const count = data as number;
17+
const count = await client.fetch<number>(
18+
groq`count(*[_type == "post" && defined(slug.current)])`,
19+
);
2220
const perPage = LIMIT;
2321
const pages = Math.ceil(count / perPage);
2422
return Array.from({ length: pages }, (_, i) => ({ num: String(i + 1) }));
@@ -32,7 +30,6 @@ export default async function Page({ params }: { params: Params }) {
3230
params: {
3331
type: "post",
3432
},
35-
tags: ["post-list", "post"],
3633
}),
3734
])
3835
).map((res) => res.data) as [DocCountResult];

app/(main)/(post)/post/[slug]/page.tsx

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import PortableText from "@/components/portable-text";
1010

1111
import type { PostQueryResult } from "@/sanity/types";
1212
import { sanityFetch } from "@/sanity/lib/live";
13+
import { client } from "@/sanity/lib/client";
1314
import { postQuery } from "@/sanity/lib/queries";
1415
import { resolveOpenGraphImage } from "@/sanity/lib/utils";
1516
import CoverMedia from "@/components/cover-media";
@@ -32,7 +33,6 @@ export async function generateMetadata(
3233
query: postQuery,
3334
params: { slug },
3435
stega: false,
35-
tags: ["post", `post:${slug}`],
3636
})
3737
).data as PostQueryResult;
3838
const previousImages = (await parent).openGraph?.images || [];
@@ -52,12 +52,10 @@ export async function generateMetadata(
5252
}
5353

5454
export async function generateStaticParams() {
55-
const { data } = await sanityFetch({
56-
query: groq`*[_type == "post" && defined(slug.current)].slug.current`,
57-
tags: ["post-list"],
58-
stega: false,
59-
});
60-
return (data as string[]).map((slug) => ({ slug }));
55+
const slugs = await client.fetch<string[]>(
56+
groq`*[_type == "post" && defined(slug.current)].slug.current`,
57+
);
58+
return slugs.map((slug) => ({ slug }));
6159
}
6260

6361
export default async function PostPage({ params }: { params: Params }) {
@@ -68,7 +66,6 @@ export default async function PostPage({ params }: { params: Params }) {
6866
sanityFetch({
6967
query: postQuery,
7068
params: { slug },
71-
tags: ["post", `post:${slug}`],
7269
}),
7370
])
7471
).map((res) => res.data) as [PostQueryResult];

0 commit comments

Comments
 (0)