Skip to content

Commit 10a7880

Browse files
authored
Merge pull request #16 from LandonSchropp/main
Add Caching
2 parents 3811cc3 + 47ee367 commit 10a7880

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

gatsby-node.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ const YAML = require("yaml")
77
const NOTION_NODE_TYPE = "Notion"
88

99
exports.sourceNodes = async (
10-
{ actions, createContentDigest, createNodeId, reporter },
10+
{ actions, createContentDigest, createNodeId, reporter, cache },
1111
{ token, databaseId, propsToFrontmatter = true, lowerTitleLevel = true },
1212
) => {
13-
const pages = await getPages({ token, databaseId }, reporter)
13+
const pages = await getPages({ token, databaseId }, reporter, cache)
1414

1515
pages.forEach((page) => {
1616
const title = getNotionPageTitle(page)

src/notion-api/get-pages.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,21 @@ const fetch = require("node-fetch")
22
const { errorMessage } = require("../error-message")
33
const { getBlocks } = require("./get-blocks")
44

5-
exports.getPages = async ({ token, databaseId, notionVersion = "2021-05-13" }, reporter) => {
5+
async function fetchPageChildren({ page, token, notionVersion }, reporter, cache) {
6+
let cacheKey = `notionApiPageChildren:${page.id}:${page.last_edited_time}`
7+
8+
let children = await cache.get(cacheKey)
9+
10+
if (children) {
11+
return children
12+
}
13+
14+
children = await getBlocks({ id: page.id, token, notionVersion }, reporter)
15+
await cache.set(cacheKey, children)
16+
return children
17+
}
18+
19+
exports.getPages = async ({ token, databaseId, notionVersion = "2021-05-13" }, reporter, cache) => {
620
let hasMore = true
721
let startCursor = ""
822
const url = `https://api.notion.com/v1/databases/${databaseId}/query`
@@ -32,8 +46,7 @@ exports.getPages = async ({ token, databaseId, notionVersion = "2021-05-13" }, r
3246
hasMore = result.has_more
3347

3448
for (let page of result.results) {
35-
page.children = await getBlocks({ id: page.id, token, notionVersion }, reporter)
36-
49+
page.children = await fetchPageChildren({ page, token, notionVersion }, reporter, cache)
3750
pages.push(page)
3851
}
3952
} catch (e) {

0 commit comments

Comments
 (0)