Skip to content

Commit 3cb34fc

Browse files
authored
fix: retry notion getter (#885)
1 parent 6239c7b commit 3cb34fc

1 file changed

Lines changed: 20 additions & 2 deletions

File tree

src/components/Notion/utils.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,29 @@ import { NotionAPI } from 'notion-client'
33
import { getRevalidate } from 'utils/revalidate'
44

55
const notion = new NotionAPI()
6+
7+
const getNotionPageWithRetry = async (id: string, retries = 3) => {
8+
try {
9+
return await notion.getPage(id)
10+
} catch (e) {
11+
if (retries > 0) {
12+
const error = e as { response?: { status?: number; headers?: Headers } }
13+
const status = error.response?.status
14+
if (status === 429) {
15+
const retryAfterHeader = error.response?.headers?.get('retry-after')
16+
const delay = retryAfterHeader ? parseFloat(retryAfterHeader) * 1000 : 5000
17+
await new Promise((resolve) => setTimeout(resolve, delay))
18+
return getNotionPageWithRetry(id, retries - 1)
19+
}
20+
}
21+
throw e
22+
}
23+
}
24+
625
export const getNotionContentProps = unstable_cache(
726
async (id: string) => {
827
try {
9-
const result = await notion.getPage(id)
10-
return result
28+
return await getNotionPageWithRetry(id)
1129
} catch (e) {
1230
console.error('Unable to get content from Notion', e)
1331
return undefined

0 commit comments

Comments
 (0)