Skip to content

Commit

Permalink
fix(*): cache tracedSVG calculations when cache is present (#12044)
Browse files Browse the repository at this point in the history
  • Loading branch information
oorestisime authored and wardpeet committed May 28, 2019
1 parent cbfebfe commit c40bc4b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 25 deletions.
47 changes: 26 additions & 21 deletions packages/gatsby-plugin-sharp/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-remark-images/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ module.exports = (
file: imageNode,
args,
fileArgs: args,
cache,
reporter,
})

Expand Down
25 changes: 21 additions & 4 deletions packages/gatsby-transformer-sharp/src/extend-node-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ({
Expand All @@ -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 },
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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 },
Expand Down

0 comments on commit c40bc4b

Please sign in to comment.