Skip to content

Commit 5072d60

Browse files
committed
Some more with caching #3890
1 parent 9ac5974 commit 5072d60

9 files changed

Lines changed: 79 additions & 32 deletions

File tree

web/app/[locale]/(pages)/[...slug]/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ export async function generateMetadata({
6767
...((isNewsPage || isMagazineRoom || isMagazinePage) && { type }),
6868
},
6969
stega: false,
70-
tags: [`page:${slug.join('/')}`],
70+
tags: [`page:/${slug.join('/')}`],
7171
requestTag: 'page-meta',
7272
})
7373

web/app/[locale]/(pages)/news/archive/[...slug]/page.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ export default async function ArchivedNewsPage({ params }: { params: Params }) {
237237
params: {
238238
lang: getNameFromIso(locale) ?? 'en_GB',
239239
},
240+
tags: [`sanity:siteMenu:${locale}`],
240241
})
241242

242243
const pageData = await getArchivedPageData(await params)

web/app/[locale]/(pages)/news/page.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export async function generateMetadata(): Promise<Metadata> {
2828
},
2929
stega: false,
3030
requestTag: 'meta-news',
31+
tags: [`sanity:newsroom:${locale}`],
3132
})
3233

3334
return constructSanityMetadata(pageSlug, locale, metaData)

web/app/[locale]/layout.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export default async function LocaleLayout({
4747
await routeSanityFetch({
4848
query: footerAndErrorImageQuery,
4949
params: queryParams,
50+
tags: [`sanity:footer:${locale}`],
5051
})
5152

5253
const { errorImage, ...footerData } = footerAndErrorImageData || {}

web/app/[locale]/page.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ export default async function Home(_: PageProps<'/[locale]'>) {
4242
params: {
4343
lang: getNameFromIso(locale) ?? 'en_GB',
4444
},
45+
tags: [`sanity:siteMenu:${locale}`],
4546
}),
4647
getPage({
4748
slug: '',

web/next.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export type ConfigRedirect = {
4343

4444
const nextConfig: NextConfig = withNextIntl({
4545
output: 'standalone',
46+
cacheComponents: true,
4647
transpilePackages: [
4748
'require-in-the-middle',
4849
'import-in-the-middle',

web/sanity/lib/fetch.ts

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { draftMode } from 'next/headers'
22
import type { DefinedFetchType } from 'next-sanity/live'
3-
import { client } from './client'
43
import { sanityFetch } from './live'
4+
import { optimizedFetch } from './simpleFetch'
55

66
/**
77
* Feature flag for the optimized fetch.
@@ -14,36 +14,6 @@ import { sanityFetch } from './live'
1414
export const IS_FETCH_OPTIMIZED =
1515
process.env.NEXT_PUBLIC_OPTIMIZED_SANITY_FETCH === 'true'
1616

17-
const optimizedFetch: DefinedFetchType = async ({
18-
query,
19-
params = {},
20-
tags = [],
21-
stega = false,
22-
perspective = 'published',
23-
requestTag = 'optimized-fetch',
24-
}) => {
25-
console.log('Fetching with optimized fetch:', {
26-
tags,
27-
stega,
28-
perspective,
29-
requestTag,
30-
})
31-
const { result, resultSourceMap } = await client.fetch(query, await params, {
32-
filterResponse: false,
33-
perspective,
34-
stega,
35-
useCdn: true,
36-
tag: requestTag,
37-
next: {
38-
// only revalidated on demand through the Sanity webhook
39-
revalidate: false,
40-
tags: tags,
41-
},
42-
})
43-
44-
return { data: result, sourceMap: resultSourceMap ?? null, tags: tags }
45-
}
46-
4717
export const routeSanityFetch: DefinedFetchType = async options => {
4818
if (!IS_FETCH_OPTIMIZED) {
4919
return sanityFetch(options)

web/sanity/lib/simpleClient.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { type ClientConfig, createClient } from '@sanity/client'
2+
import { apiVersion, dataset, projectId, studioUrl } from '../lib/api'
3+
4+
const sanityConfig: ClientConfig = {
5+
projectId,
6+
dataset,
7+
apiVersion,
8+
//perspective: dataset === 'global-development' ? 'drafts' : 'published',
9+
useCdn: true,
10+
ignoreBrowserTokenWarning: dataset === 'global-development',
11+
requestTagPrefix: 'website', // to track usage on web in sanity console
12+
stega: {
13+
studioUrl,
14+
// Set logger to 'console' for more verbose logging
15+
// logger: console,
16+
filter: props => {
17+
if (props.sourcePath.at(-1) === 'title') {
18+
return true
19+
}
20+
21+
return props.filterDefault(props)
22+
},
23+
},
24+
}
25+
26+
export const client = createClient({
27+
...sanityConfig,
28+
token: process.env.SANITY_API_TOKEN,
29+
})
30+
31+
//only for next config
32+
export const notSecuredTokenClient = createClient({
33+
...sanityConfig,
34+
token: process.env.SANITY_API_TOKEN,
35+
})
36+
37+
export const noCdnClient = createClient({
38+
...sanityConfig,
39+
useCdn: false,
40+
token: process.env.SANITY_API_TOKEN,
41+
})

web/sanity/lib/simpleFetch.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use cache'
2+
import { cacheTag } from 'next/cache'
3+
import type { DefinedFetchType } from 'next-sanity/live'
4+
import { client as simpleClient } from './simpleClient'
5+
6+
export const optimizedFetch: DefinedFetchType = async ({
7+
query,
8+
params = {},
9+
tags = [],
10+
perspective = 'published',
11+
requestTag = 'optimized-fetch',
12+
}) => {
13+
console.log('Fetching with optimized fetch:', {
14+
tags,
15+
perspective,
16+
requestTag,
17+
})
18+
cacheTag(...tags)
19+
const { result, resultSourceMap } = await simpleClient.fetch(
20+
query,
21+
await params,
22+
{
23+
filterResponse: false,
24+
perspective,
25+
useCdn: true,
26+
tag: requestTag,
27+
},
28+
)
29+
30+
return { data: result, sourceMap: resultSourceMap ?? null, tags: tags }
31+
}

0 commit comments

Comments
 (0)