Skip to content

Commit 5dc9346

Browse files
oorestisimepieh
authored andcommitted
feat(gatsby-plugin-sharp): cache base64 if possible (#9059)
Closes: #6999 <!-- Q. Which branch should I use for my pull request? A. Use `master` branch (probably). Q. Which branch if my change is a bug fix for Gatsby v1? A. In this case, you should use the `v1` branch Q. Which branch if I'm still not sure? A. Use `master` branch. Ask in the PR if you're not sure and a Gatsby maintainer will be happy to help :) Note: We will only accept bug fixes for Gatsby v1. New features should be added to Gatsby v2. Learn more about contributing: https://www.gatsbyjs.org/docs/how-to-contribute/ -->
1 parent 0496ce9 commit 5dc9346

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

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

+33-16
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ function queueImageResizing({ file, args = {}, reporter }) {
441441
}
442442
}
443443

444-
async function notMemoizedbase64({ file, args = {}, reporter }) {
444+
async function generateBase64({ file, args, reporter }) {
445445
const options = healOptions(args, { width: 20 })
446446
let pipeline
447447
try {
@@ -484,29 +484,46 @@ async function notMemoizedbase64({ file, args = {}, reporter }) {
484484
pipeline
485485
)
486486
}
487-
488487
const [buffer, info] = await pipeline.toBufferAsync()
489-
const originalName = file.base
490-
491-
return {
488+
const base64output = {
492489
src: `data:image/${info.format};base64,${buffer.toString(`base64`)}`,
493490
width: info.width,
494491
height: info.height,
495492
aspectRatio: info.width / info.height,
496-
originalName: originalName,
493+
originalName: file.base,
497494
}
495+
return base64output
498496
}
499497

500-
const memoizedBase64 = _.memoize(
501-
notMemoizedbase64,
502-
({ file, args }) => `${file.id}${JSON.stringify(args)}`
503-
)
498+
const base64CacheKey = ({ file, args }) => `${file.id}${JSON.stringify(args)}`
499+
500+
const memoizedBase64 = _.memoize(generateBase64, base64CacheKey)
501+
502+
const cachifiedBase64 = async ({ cache, ...arg }) => {
503+
const cacheKey = base64CacheKey(arg)
504+
505+
const cachedBase64 = await cache.get(cacheKey)
506+
if (cachedBase64) {
507+
return cachedBase64
508+
}
509+
510+
const base64output = await generateBase64(arg)
511+
512+
await cache.set(cacheKey, base64output)
513+
514+
return base64output
515+
}
516+
517+
async function base64(arg) {
518+
if (arg.cache) {
519+
// Not all tranformer plugins are going to provide cache
520+
return await cachifiedBase64(arg)
521+
}
504522

505-
async function base64(args) {
506-
return await memoizedBase64(args)
523+
return await memoizedBase64(arg)
507524
}
508525

509-
async function fluid({ file, args = {}, reporter }) {
526+
async function fluid({ file, args = {}, reporter, cache }) {
510527
const options = healOptions(args, {})
511528

512529
// Account for images with a high pixel density. We assume that these types of
@@ -634,7 +651,7 @@ async function fluid({ file, args = {}, reporter }) {
634651
}
635652

636653
// Get base64 version
637-
const base64Image = await base64({ file, args: base64Args, reporter })
654+
const base64Image = await base64({ file, args: base64Args, reporter, cache })
638655

639656
// Construct src and srcSet strings.
640657
const originalImg = _.maxBy(images, image => image.width).src
@@ -682,7 +699,7 @@ async function fluid({ file, args = {}, reporter }) {
682699
}
683700
}
684701

685-
async function fixed({ file, args = {}, reporter }) {
702+
async function fixed({ file, args = {}, reporter, cache }) {
686703
const options = healOptions(args, {})
687704

688705
// if no width is passed, we need to resize the image based on the passed height
@@ -744,7 +761,7 @@ async function fixed({ file, args = {}, reporter }) {
744761
}
745762

746763
// Get base64 version
747-
const base64Image = await base64({ file, args: base64Args, reporter })
764+
const base64Image = await base64({ file, args: base64Args, reporter, cache })
748765

749766
const fallbackSrc = images[0].src
750767
const srcSet = images

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const slash = require(`slash`)
1515
// 4. Create the responsive images.
1616
// 5. Set the html w/ aspect ratio helper.
1717
module.exports = (
18-
{ files, markdownNode, markdownAST, pathPrefix, getNode, reporter },
18+
{ files, markdownNode, markdownAST, pathPrefix, getNode, reporter, cache },
1919
pluginOptions
2020
) => {
2121
const defaults = {
@@ -83,6 +83,7 @@ module.exports = (
8383
file: imageNode,
8484
args: options,
8585
reporter,
86+
cache,
8687
})
8788

8889
if (!fluidResult) {

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

+11
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const fixedNodeType = ({
5151
getNodeAndSavePathDependency,
5252
reporter,
5353
name,
54+
cache,
5455
}) => {
5556
return {
5657
type: new GraphQLObjectType({
@@ -80,6 +81,7 @@ const fixedNodeType = ({
8081
file,
8182
args,
8283
reporter,
84+
cache,
8385
})
8486
).then(({ src }) => src)
8587
},
@@ -96,6 +98,7 @@ const fixedNodeType = ({
9698
file,
9799
args,
98100
reporter,
101+
cache,
99102
})
100103
).then(({ srcSet }) => srcSet)
101104
},
@@ -151,6 +154,7 @@ const fixedNodeType = ({
151154
file,
152155
args,
153156
reporter,
157+
cache,
154158
})
155159
).then(o =>
156160
Object.assign({}, o, {
@@ -169,6 +173,7 @@ const fluidNodeType = ({
169173
getNodeAndSavePathDependency,
170174
reporter,
171175
name,
176+
cache,
172177
}) => {
173178
return {
174179
type: new GraphQLObjectType({
@@ -194,6 +199,7 @@ const fluidNodeType = ({
194199
file,
195200
args,
196201
reporter,
202+
cache,
197203
})
198204
).then(({ src }) => src)
199205
},
@@ -210,6 +216,7 @@ const fluidNodeType = ({
210216
file,
211217
args,
212218
reporter,
219+
cache,
213220
})
214221
).then(({ srcSet }) => srcSet)
215222
},
@@ -278,6 +285,7 @@ const fluidNodeType = ({
278285
file,
279286
args,
280287
reporter,
288+
cache,
281289
})
282290
).then(o =>
283291
Object.assign({}, o, {
@@ -295,6 +303,7 @@ module.exports = ({
295303
pathPrefix,
296304
getNodeAndSavePathDependency,
297305
reporter,
306+
cache,
298307
}) => {
299308
if (type.name !== `ImageSharp`) {
300309
return {}
@@ -305,6 +314,7 @@ module.exports = ({
305314
pathPrefix,
306315
getNodeAndSavePathDependency,
307316
reporter,
317+
cache,
308318
}
309319

310320
// TODO: Remove resolutionsNode and sizesNode for Gatsby v3
@@ -440,6 +450,7 @@ module.exports = ({
440450
resolve(
441451
base64({
442452
file,
453+
cache,
443454
})
444455
)
445456
} else {

0 commit comments

Comments
 (0)