From c40bc4b0d1d4ca2b60ac6029087a41d7ab2d7a34 Mon Sep 17 00:00:00 2001 From: Orestis Ioannou Date: Tue, 28 May 2019 09:15:14 +0200 Subject: [PATCH] fix(*): cache tracedSVG calculations when cache is present (#12044) --- packages/gatsby-plugin-sharp/src/index.js | 47 ++++++++++--------- packages/gatsby-remark-images/src/index.js | 1 + .../src/extend-node-type.js | 25 ++++++++-- 3 files changed, 48 insertions(+), 25 deletions(-) diff --git a/packages/gatsby-plugin-sharp/src/index.js b/packages/gatsby-plugin-sharp/src/index.js index c7d789a5d2750..8895e2179a4aa 100644 --- a/packages/gatsby-plugin-sharp/src/index.js +++ b/packages/gatsby-plugin-sharp/src/index.js @@ -10,7 +10,7 @@ const { scheduleJob } = require(`./scheduler`) const { createArgsDigest } = require(`./process-file`) const { reportError } = require(`./report-error`) const { getPluginOptions, healOptions } = require(`./plugin-options`) -const { memoizedTraceSVG } = require(`./trace-svg`) +const { memoizedTraceSVG, notMemoizedtraceSVG } = require(`./trace-svg`) const imageSizeCache = new Map() const getImageSize = file => { @@ -202,40 +202,49 @@ async function generateBase64({ file, args, reporter }) { return base64output } -const base64CacheKey = ({ file, args }) => `${file.id}${JSON.stringify(args)}` +const generateCacheKey = ({ file, args }) => `${file.id}${JSON.stringify(args)}` -const memoizedBase64 = _.memoize(generateBase64, base64CacheKey) +const memoizedBase64 = _.memoize(generateBase64, generateCacheKey) -const cachifiedBase64 = async ({ cache, ...arg }) => { - const cacheKey = base64CacheKey(arg) +const cachifiedProcess = async ({ cache, ...arg }, genKey, processFn) => { + const cachedKey = genKey(arg) + const cached = await cache.get(cachedKey) - const cachedBase64 = await cache.get(cacheKey) - if (cachedBase64) { - return cachedBase64 + if (cached) { + return cached } - const base64output = await generateBase64(arg) + const result = await processFn(arg) + await cache.set(cachedKey, result) - await cache.set(cacheKey, base64output) - - return base64output + return result } async function base64(arg) { if (arg.cache) { // Not all tranformer plugins are going to provide cache - return await cachifiedBase64(arg) + return await cachifiedProcess(arg, generateCacheKey, generateBase64) } return await memoizedBase64(arg) } -async function getTracedSVG(options, file) { +async function traceSVG(args) { + if (args.cache) { + // Not all tranformer plugins are going to provide cache + return await cachifiedProcess(args, generateCacheKey, notMemoizedtraceSVG) + } + return await memoizedTraceSVG(args) +} + +async function getTracedSVG({ file, options, cache, reporter }) { if (options.generateTracedSVG && options.tracedSVG) { const tracedSVG = await traceSVG({ - file, args: options.tracedSVG, fileArgs: options, + file, + cache, + reporter, }) return tracedSVG } @@ -380,7 +389,7 @@ async function fluid({ file, args = {}, reporter, cache }) { base64Image = await base64({ file, args: base64Args, reporter, cache }) } - const tracedSVG = await getTracedSVG(options, file) + const tracedSVG = await getTracedSVG({ options, file, cache, reporter }) // Construct src and srcSet strings. const originalImg = _.maxBy(images, image => image.width).src @@ -503,7 +512,7 @@ async function fixed({ file, args = {}, reporter, cache }) { }) } - const tracedSVG = await getTracedSVG(options, file) + const tracedSVG = await getTracedSVG({ options, file, reporter, cache }) const fallbackSrc = images[0].src const srcSet = images @@ -539,10 +548,6 @@ async function fixed({ file, args = {}, reporter, cache }) { } } -async function traceSVG(args) { - return await memoizedTraceSVG(args) -} - function toArray(buf) { var arr = new Array(buf.length) diff --git a/packages/gatsby-remark-images/src/index.js b/packages/gatsby-remark-images/src/index.js index 3ae2d38ad3211..80463f9dac52b 100644 --- a/packages/gatsby-remark-images/src/index.js +++ b/packages/gatsby-remark-images/src/index.js @@ -235,6 +235,7 @@ module.exports = ( file: imageNode, args, fileArgs: args, + cache, reporter, }) diff --git a/packages/gatsby-transformer-sharp/src/extend-node-type.js b/packages/gatsby-transformer-sharp/src/extend-node-type.js index 712f897b4da8d..bf67249770767 100644 --- a/packages/gatsby-transformer-sharp/src/extend-node-type.js +++ b/packages/gatsby-transformer-sharp/src/extend-node-type.js @@ -41,11 +41,13 @@ function toArray(buf) { return arr } -const getTracedSVG = async ({ file, image, fieldArgs }) => +const getTracedSVG = async ({ file, image, fieldArgs, cache, reporter }) => traceSVG({ file, args: { ...fieldArgs.traceSVG }, fileArgs: fieldArgs, + cache, + reporter, }) const fixedNodeType = ({ @@ -63,7 +65,12 @@ const fixedNodeType = ({ base64: { type: GraphQLString }, tracedSVG: { type: GraphQLString, - resolve: parent => getTracedSVG(parent), + resolve: parent => + getTracedSVG({ + ...parent, + cache, + reporter, + }), }, aspectRatio: { type: GraphQLFloat }, width: { type: GraphQLFloat }, @@ -195,7 +202,12 @@ const fluidNodeType = ({ base64: { type: GraphQLString }, tracedSVG: { type: GraphQLString, - resolve: parent => getTracedSVG(parent), + resolve: parent => + getTracedSVG({ + ...parent, + cache, + reporter, + }), }, aspectRatio: { type: GraphQLFloat }, src: { type: GraphQLString }, @@ -417,7 +429,12 @@ module.exports = ({ src: { type: GraphQLString }, tracedSVG: { type: GraphQLString, - resolve: parent => getTracedSVG(parent), + resolve: parent => + getTracedSVG({ + ...parent, + cache, + reporter, + }), }, width: { type: GraphQLInt }, height: { type: GraphQLInt },