Skip to content

Commit b225b92

Browse files
axe312gerwardpeet
authored andcommitted
chore(gatsby-transformer-sqip): improve sqip performance and debug logging (#13349)
1 parent 177d35c commit b225b92

File tree

4 files changed

+74
-11
lines changed

4 files changed

+74
-11
lines changed

packages/gatsby-transformer-sqip/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
},
99
"dependencies": {
1010
"@babel/runtime": "^7.0.0",
11+
"bluebird": "^3.5.4",
1112
"fs-extra": "^4.0.2",
1213
"gatsby-plugin-sharp": "^2.1.5",
14+
"md5-file": "^4.0.0",
1315
"mini-svg-data-uri": "^1.0.0",
1416
"p-queue": "^2.3.0",
1517
"sqip": "^0.3.3"

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { resolve } = require(`path`)
2+
const md5File = require(`bluebird`).promisify(require(`md5-file`))
23

34
const {
45
DuotoneGradientType,
@@ -43,7 +44,9 @@ module.exports = async args => {
4344

4445
async function sqipSharp({ type, cache, getNodeAndSavePathDependency, store }) {
4546
const program = store.getState().program
46-
const cacheDir = resolve(`${program.directory}/.cache/sqip/`)
47+
const cacheDir = resolve(
48+
`${program.directory}/node_modules/.cache/gatsby-transformer-sqip/`
49+
)
4750

4851
await ensureDir(cacheDir)
4952

@@ -107,6 +110,7 @@ async function sqipSharp({ type, cache, getNodeAndSavePathDependency, store }) {
107110
}
108111

109112
const file = getNodeAndSavePathDependency(image.parent, context.path)
113+
const { contentDigest } = image.internal
110114

111115
const job = await queueImageResizing({ file, args: sharpArgs })
112116

@@ -120,6 +124,7 @@ async function sqipSharp({ type, cache, getNodeAndSavePathDependency, store }) {
120124
return generateSqip({
121125
cache,
122126
cacheDir,
127+
contentDigest,
123128
absolutePath,
124129
numberOfPrimitives,
125130
blur,
@@ -219,9 +224,12 @@ async function sqipContentful({ type, cache, store }) {
219224

220225
const absolutePath = await cacheImage(store, asset, options)
221226

227+
const contentDigest = await md5File(absolutePath)
228+
222229
return generateSqip({
223230
cache,
224231
cacheDir,
232+
contentDigest,
225233
absolutePath,
226234
numberOfPrimitives,
227235
blur,

packages/gatsby-transformer-sqip/src/generate-sqip.js

+53-10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = async function generateSqip(options) {
1818
blur,
1919
mode,
2020
cacheDir,
21+
contentDigest,
2122
} = options
2223

2324
debug({ options })
@@ -35,17 +36,59 @@ module.exports = async function generateSqip(options) {
3536
.update(JSON.stringify(sqipOptions))
3637
.digest(`hex`)
3738

38-
const cacheKey = `sqip-${name}-${optionsHash}`
39-
const cachePath = resolve(cacheDir, `${name}-${optionsHash}.svg`)
40-
let primitiveData = await cache.get(cacheKey)
39+
const cacheKey = `${contentDigest}-${optionsHash}`
40+
const cachePath = resolve(cacheDir, `${contentDigest}-${optionsHash}.svg`)
4141

42-
debug({ primitiveData })
42+
debug(
43+
`Request preview generation for ${name} (${contentDigest}-${optionsHash})`
44+
)
4345

44-
if (!primitiveData) {
46+
return queue.add(async () => {
47+
let primitiveData = await cache.get(cacheKey)
4548
let svg
46-
if (await exists(cachePath)) {
47-
const svgBuffer = await readFile(cachePath)
48-
svg = svgBuffer.toString()
49+
50+
debug(
51+
`Executing preview generation request for ${name} (${contentDigest}-${optionsHash})`
52+
)
53+
54+
if (!primitiveData) {
55+
if (await exists(cachePath)) {
56+
debug(
57+
`Primitive result file already exists for ${name} (${contentDigest}-${optionsHash})`
58+
)
59+
const svgBuffer = await readFile(cachePath)
60+
svg = svgBuffer.toString()
61+
} else {
62+
debug(
63+
`Generate primitive result file of ${name} (${contentDigest}-${optionsHash})`
64+
)
65+
66+
const result = await new Promise((resolve, reject) => {
67+
try {
68+
const result = sqip({
69+
filename: absolutePath,
70+
...sqipOptions,
71+
})
72+
resolve(result)
73+
} catch (error) {
74+
reject(error)
75+
}
76+
})
77+
78+
svg = result.final_svg
79+
80+
await writeFile(cachePath, svg)
81+
debug(
82+
`Wrote primitive result file to disk for ${name} (${contentDigest}-${optionsHash})`
83+
)
84+
}
85+
86+
primitiveData = {
87+
svg,
88+
dataURI: svgToMiniDataURI(svg),
89+
}
90+
91+
await cache.set(cacheKey, primitiveData)
4992
} else {
5093
debug(`generate sqip for ${name}`)
5194
const result = await queue.add(
@@ -74,7 +117,7 @@ module.exports = async function generateSqip(options) {
74117
}
75118

76119
await cache.set(cacheKey, primitiveData)
77-
}
78120

79-
return primitiveData
121+
return primitiveData
122+
})
80123
}

yarn.lock

+10
Original file line numberDiff line numberDiff line change
@@ -5101,6 +5101,11 @@ bluebird@^3.5.3:
51015101
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.3.tgz#7d01c6f9616c9a51ab0f8c549a79dfe6ec33efa7"
51025102
integrity sha512-/qKPUQlaW1OyR51WeCPBvRnAlnZFUJkCSG5HzGnuIqhgyJtF+T94lFnn33eiazjRm2LAHVy2guNnaq48X9SJuw==
51035103

5104+
bluebird@^3.5.4:
5105+
version "3.5.5"
5106+
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.5.tgz#a8d0afd73251effbbd5fe384a77d73003c17a71f"
5107+
integrity sha512-5am6HnnfN+urzt4yfg7IgTbotDjIT/u8AJpEt0sIU9FtXfVeezXAPKswrG+xKUCOYAINpSdgZVDU6QFh+cuH3w==
5108+
51045109
51055110
version "0.0.3"
51065111
resolved "https://registry.yarnpkg.com/bmp-js/-/bmp-js-0.0.3.tgz#64113e9c7cf1202b376ed607bf30626ebe57b18a"
@@ -14654,6 +14659,11 @@ md5-file@^3.1.1:
1465414659
dependencies:
1465514660
buffer-alloc "^1.1.0"
1465614661

14662+
md5-file@^4.0.0:
14663+
version "4.0.0"
14664+
resolved "https://registry.yarnpkg.com/md5-file/-/md5-file-4.0.0.tgz#f3f7ba1e2dd1144d5bf1de698d0e5f44a4409584"
14665+
integrity sha512-UC0qFwyAjn4YdPpKaDNw6gNxRf7Mcx7jC1UGCY4boCzgvU2Aoc1mOGzTtrjjLKhM5ivsnhoKpQVxKPp+1j1qwg==
14666+
1465714667
md5.js@^1.3.4:
1465814668
version "1.3.4"
1465914669
resolved "https://registry.yarnpkg.com/md5.js/-/md5.js-1.3.4.tgz#e9bdbde94a20a5ac18b04340fc5764d5b09d901d"

0 commit comments

Comments
 (0)