Skip to content

Commit 47ee367

Browse files
committed
Add a cache
This adds the Gatsby cache to the plugin. When pages are fetched, we first check to see if the page content has been cached. If so, the content is returned directly from the cache.
1 parent 3811cc3 commit 47ee367

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

gatsby-node.js

+2-2
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

+16-3
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)