From 2572f994ba9ca28cd18c2108d2483fbf573cbcf2 Mon Sep 17 00:00:00 2001 From: Mike Allanson Date: Wed, 11 Jul 2018 15:43:30 +0100 Subject: [PATCH 1/2] Update now that getState().nodes returns a Map rather than an Array --- .../src/gatsby-node.js | 39 ++++++++++--------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/packages/gatsby-transformer-screenshot/src/gatsby-node.js b/packages/gatsby-transformer-screenshot/src/gatsby-node.js index 8d7ccc4e113ad..91657015433a3 100644 --- a/packages/gatsby-transformer-screenshot/src/gatsby-node.js +++ b/packages/gatsby-transformer-screenshot/src/gatsby-node.js @@ -16,29 +16,30 @@ exports.onPreBootstrap = ( pluginOptions ) => { const { createNode, touchNode } = actions + const screenshotNodes = Array.from(store.getState().nodes.values()).filter( + n => n.internal.type === `Screenshot` + ) // Check for updated screenshots // and prevent Gatsby from garbage collecting remote file nodes return Promise.all( - _.values(store.getState().nodes) - .filter(n => n.internal.type === `Screenshot`) - .map(async n => { - if (n.expires && new Date() >= new Date(n.expires)) { - // Screenshot expired, re-run Lambda - await createScreenshotNode({ - url: n.url, - parent: n.parent, - store, - cache, - createNode, - createNodeId, - }) - } else { - // Screenshot hasn't yet expired, touch the image node - // to prevent garbage collection - touchNode({ nodeId: n.screenshotFile___NODE }) - } - }) + screenshotNodes.map(async n => { + if (n.expires && new Date() >= new Date(n.expires)) { + // Screenshot expired, re-run Lambda + await createScreenshotNode({ + url: n.url, + parent: n.parent, + store, + cache, + createNode, + createNodeId, + }) + } else { + // Screenshot hasn't yet expired, touch the image node + // to prevent garbage collection + touchNode({ nodeId: n.screenshotFile___NODE }) + } + }) ) } From 032acedbf3cc16ca4479040b83fa6c5880495b36 Mon Sep 17 00:00:00 2001 From: Mike Allanson Date: Wed, 11 Jul 2018 16:30:10 +0100 Subject: [PATCH 2/2] Use the `getNodes()` helper, Luke --- packages/gatsby-transformer-screenshot/src/gatsby-node.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/gatsby-transformer-screenshot/src/gatsby-node.js b/packages/gatsby-transformer-screenshot/src/gatsby-node.js index 91657015433a3..c445ce207bf75 100644 --- a/packages/gatsby-transformer-screenshot/src/gatsby-node.js +++ b/packages/gatsby-transformer-screenshot/src/gatsby-node.js @@ -1,6 +1,5 @@ const crypto = require(`crypto`) const axios = require(`axios`) -const _ = require(`lodash`) const { createRemoteFileNode } = require(`gatsby-source-filesystem`) const SCREENSHOT_ENDPOINT = `https://h7iqvn4842.execute-api.us-east-2.amazonaws.com/prod/screenshot` @@ -12,11 +11,11 @@ const createContentDigest = obj => .digest(`hex`) exports.onPreBootstrap = ( - { store, cache, actions, createNodeId }, + { store, cache, actions, createNodeId, getNodes }, pluginOptions ) => { const { createNode, touchNode } = actions - const screenshotNodes = Array.from(store.getState().nodes.values()).filter( + const screenshotNodes = getNodes().filter( n => n.internal.type === `Screenshot` )