Skip to content

Commit c40bc4b

Browse files
oorestisimewardpeet
authored andcommitted
fix(*): cache tracedSVG calculations when cache is present (#12044)
1 parent cbfebfe commit c40bc4b

File tree

3 files changed

+48
-25
lines changed

3 files changed

+48
-25
lines changed

packages/gatsby-plugin-sharp/src/index.js

+26-21
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const { scheduleJob } = require(`./scheduler`)
1010
const { createArgsDigest } = require(`./process-file`)
1111
const { reportError } = require(`./report-error`)
1212
const { getPluginOptions, healOptions } = require(`./plugin-options`)
13-
const { memoizedTraceSVG } = require(`./trace-svg`)
13+
const { memoizedTraceSVG, notMemoizedtraceSVG } = require(`./trace-svg`)
1414

1515
const imageSizeCache = new Map()
1616
const getImageSize = file => {
@@ -202,40 +202,49 @@ async function generateBase64({ file, args, reporter }) {
202202
return base64output
203203
}
204204

205-
const base64CacheKey = ({ file, args }) => `${file.id}${JSON.stringify(args)}`
205+
const generateCacheKey = ({ file, args }) => `${file.id}${JSON.stringify(args)}`
206206

207-
const memoizedBase64 = _.memoize(generateBase64, base64CacheKey)
207+
const memoizedBase64 = _.memoize(generateBase64, generateCacheKey)
208208

209-
const cachifiedBase64 = async ({ cache, ...arg }) => {
210-
const cacheKey = base64CacheKey(arg)
209+
const cachifiedProcess = async ({ cache, ...arg }, genKey, processFn) => {
210+
const cachedKey = genKey(arg)
211+
const cached = await cache.get(cachedKey)
211212

212-
const cachedBase64 = await cache.get(cacheKey)
213-
if (cachedBase64) {
214-
return cachedBase64
213+
if (cached) {
214+
return cached
215215
}
216216

217-
const base64output = await generateBase64(arg)
217+
const result = await processFn(arg)
218+
await cache.set(cachedKey, result)
218219

219-
await cache.set(cacheKey, base64output)
220-
221-
return base64output
220+
return result
222221
}
223222

224223
async function base64(arg) {
225224
if (arg.cache) {
226225
// Not all tranformer plugins are going to provide cache
227-
return await cachifiedBase64(arg)
226+
return await cachifiedProcess(arg, generateCacheKey, generateBase64)
228227
}
229228

230229
return await memoizedBase64(arg)
231230
}
232231

233-
async function getTracedSVG(options, file) {
232+
async function traceSVG(args) {
233+
if (args.cache) {
234+
// Not all tranformer plugins are going to provide cache
235+
return await cachifiedProcess(args, generateCacheKey, notMemoizedtraceSVG)
236+
}
237+
return await memoizedTraceSVG(args)
238+
}
239+
240+
async function getTracedSVG({ file, options, cache, reporter }) {
234241
if (options.generateTracedSVG && options.tracedSVG) {
235242
const tracedSVG = await traceSVG({
236-
file,
237243
args: options.tracedSVG,
238244
fileArgs: options,
245+
file,
246+
cache,
247+
reporter,
239248
})
240249
return tracedSVG
241250
}
@@ -380,7 +389,7 @@ async function fluid({ file, args = {}, reporter, cache }) {
380389
base64Image = await base64({ file, args: base64Args, reporter, cache })
381390
}
382391

383-
const tracedSVG = await getTracedSVG(options, file)
392+
const tracedSVG = await getTracedSVG({ options, file, cache, reporter })
384393

385394
// Construct src and srcSet strings.
386395
const originalImg = _.maxBy(images, image => image.width).src
@@ -503,7 +512,7 @@ async function fixed({ file, args = {}, reporter, cache }) {
503512
})
504513
}
505514

506-
const tracedSVG = await getTracedSVG(options, file)
515+
const tracedSVG = await getTracedSVG({ options, file, reporter, cache })
507516

508517
const fallbackSrc = images[0].src
509518
const srcSet = images
@@ -539,10 +548,6 @@ async function fixed({ file, args = {}, reporter, cache }) {
539548
}
540549
}
541550

542-
async function traceSVG(args) {
543-
return await memoizedTraceSVG(args)
544-
}
545-
546551
function toArray(buf) {
547552
var arr = new Array(buf.length)
548553

packages/gatsby-remark-images/src/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ module.exports = (
235235
file: imageNode,
236236
args,
237237
fileArgs: args,
238+
cache,
238239
reporter,
239240
})
240241

packages/gatsby-transformer-sharp/src/extend-node-type.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ function toArray(buf) {
4141
return arr
4242
}
4343

44-
const getTracedSVG = async ({ file, image, fieldArgs }) =>
44+
const getTracedSVG = async ({ file, image, fieldArgs, cache, reporter }) =>
4545
traceSVG({
4646
file,
4747
args: { ...fieldArgs.traceSVG },
4848
fileArgs: fieldArgs,
49+
cache,
50+
reporter,
4951
})
5052

5153
const fixedNodeType = ({
@@ -63,7 +65,12 @@ const fixedNodeType = ({
6365
base64: { type: GraphQLString },
6466
tracedSVG: {
6567
type: GraphQLString,
66-
resolve: parent => getTracedSVG(parent),
68+
resolve: parent =>
69+
getTracedSVG({
70+
...parent,
71+
cache,
72+
reporter,
73+
}),
6774
},
6875
aspectRatio: { type: GraphQLFloat },
6976
width: { type: GraphQLFloat },
@@ -195,7 +202,12 @@ const fluidNodeType = ({
195202
base64: { type: GraphQLString },
196203
tracedSVG: {
197204
type: GraphQLString,
198-
resolve: parent => getTracedSVG(parent),
205+
resolve: parent =>
206+
getTracedSVG({
207+
...parent,
208+
cache,
209+
reporter,
210+
}),
199211
},
200212
aspectRatio: { type: GraphQLFloat },
201213
src: { type: GraphQLString },
@@ -417,7 +429,12 @@ module.exports = ({
417429
src: { type: GraphQLString },
418430
tracedSVG: {
419431
type: GraphQLString,
420-
resolve: parent => getTracedSVG(parent),
432+
resolve: parent =>
433+
getTracedSVG({
434+
...parent,
435+
cache,
436+
reporter,
437+
}),
421438
},
422439
width: { type: GraphQLInt },
423440
height: { type: GraphQLInt },

0 commit comments

Comments
 (0)