Skip to content

Commit

Permalink
Cache data when sourcing nodes (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
TommasoAmici committed May 29, 2021
1 parent b4eb80f commit fb32932
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@ const { fetchCollection } = require("./dist/index");

const GAME_NODE_TYPE = "BggGame";

exports.sourceNodes = async ({ actions, createNodeId, createContentDigest }, pluginOptions) => {
exports.sourceNodes = async (
{ actions, cache, createNodeId, createContentDigest },
pluginOptions
) => {
const { createNode } = actions;

const games = await fetchCollection(pluginOptions);
games.forEach(game => {
const cacheKey = "gatsby-source-bgg";
let sourceData = await cache.get(cacheKey);
if (!sourceData) {
sourceData = await fetchCollection(pluginOptions);
await cache.set(cacheKey, sourceData);
}

sourceData.forEach(game => {
const nodeMeta = {
id: createNodeId(`${GAME_NODE_TYPE}-${game.objectID}`),
parent: null,
Expand Down

0 comments on commit fb32932

Please sign in to comment.