Skip to content

Commit

Permalink
feat(contentful): replace to next/cache (#3088)
Browse files Browse the repository at this point in the history
  • Loading branch information
ykzts committed Dec 25, 2023
1 parent 52520bf commit 83211a1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 31 deletions.
3 changes: 1 addition & 2 deletions app/(content)/kuji/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { notFound } from 'next/navigation'
import { title as siteName, twitterAccount } from '@/lib/constants'
import { type FortuneEntry, getFortune, getFortuneIDs } from '@/lib/contentful'
import { generateFortuneName, getImageURL } from '@/lib/fortune'
import { fromAsync } from '@/lib/polyfills/array'
import ShareLinks from './_components/share-links'
import styles from './page.module.css'

Expand All @@ -16,7 +15,7 @@ export type Params = {
}

export async function generateStaticParams(): Promise<Params[]> {
const ids = await fromAsync(getFortuneIDs())
const ids = await getFortuneIDs()

return ids.map((id) => ({ id }))
}
Expand Down
4 changes: 3 additions & 1 deletion app/sitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
}
]

for await (const fortuneID of getFortuneIDs()) {
const fortuneIDs = await getFortuneIDs()

for (const fortuneID of fortuneIDs) {
items.push({
changeFrequency: 'monthly',
priority: 0.7,
Expand Down
35 changes: 18 additions & 17 deletions lib/contentful.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import {
createClient
} from 'contentful'
import dedent from 'dedent'
import { cache } from 'react'
import { fromAsync } from '@/lib/polyfills/array'
import { unstable_cache as cache } from 'next/cache'
import { redisClient } from '@/lib/redis'

let client: ContentfulClientApi<'WITHOUT_UNRESOLVABLE_LINKS'>
Expand Down Expand Up @@ -47,26 +46,27 @@ export type FortuneEntry = Entry<
'WITHOUT_UNRESOLVABLE_LINKS'
>

export const getFortune = cache(async function getFortune(
id?: string
): Promise<FortuneEntry> {
if (!id) throw new TypeError('The fortune ID is required.')
export const getFortune = cache(
async function getFortune(id?: string): Promise<FortuneEntry> {
if (!id) throw new TypeError('The fortune ID is required.')

return getClient().getEntry<FortuneEntrySkeleton>(id)
})
return getClient().getEntry<FortuneEntrySkeleton>(id)
},
['fortune']
)

export const getFortuneIDs = cache(
async function* getFortuneIDs(): AsyncGenerator<string, void> {
async function getFortuneIDs(): Promise<string[]> {
const results: string[] = []

const { items, limit, total } =
await getClient().getEntries<FortuneEntrySkeleton>({
content_type: 'fortune',
limit: 100,
select: ['sys.id']
})

for (const entry of items) {
yield entry.sys.id
}
results.push(...items.map((entry) => entry.sys.id))

if (items.length < total) {
const promises: Promise<EntryCollection<FortuneEntrySkeleton>>[] = []
Expand All @@ -83,12 +83,13 @@ export const getFortuneIDs = cache(

const collectionList = await Promise.all(promises)
for (const { items: collectionItems } of collectionList) {
for (const entry of collectionItems) {
yield entry.sys.id
}
results.push(...collectionItems.map((entry) => entry.sys.id))
}
}
}

return results
},
['fortunes']
)

export async function getAnyFortuneID(): Promise<string> {
Expand All @@ -99,7 +100,7 @@ export async function getAnyFortuneID(): Promise<string> {
}

const m = redisClient.multi()
const idSet = await fromAsync(getFortuneIDs())
const idSet = await getFortuneIDs()

m.eval<string[], string | null>(
dedent`
Expand Down
11 changes: 0 additions & 11 deletions lib/polyfills/array.ts

This file was deleted.

0 comments on commit 83211a1

Please sign in to comment.