Skip to content

Commit 2a66958

Browse files
SekhmetDSchau
authored andcommitted
feat: use hashed folder names instead of filenames. closes #6232 (#8808)
**Related issue:** #6232 @LekoArts mentioned two ways of implementing it (one that concatenates `argsDigest` with `contentDigest` and other that uses `contentDigest` as one directory and `argsDigest` as another child so images are grouped together and it limits the number of directories in `static` directory). This PR implements the latter. ### Performance impact I measured the performance of original solution and one from this PR 5 times (and ignoring first build after changing gatsby version) for each (every time cleaning the cache) by running `gatsby build` on example [using-gatsby-image](https://github.com/gatsbyjs/gatsby/tree/master/examples/using-gatsby-image). Tested on Linux, Ryzen 2700X and 970 EVO. #### Original (`2.0.18`) - `14.70` - `14.08` - `13.65` - `14.22` - `13.80` **Average:** `14.09` #### This PR: - `13.77` - `13.51` - `13.61` - `13.78` - `13.66` **Average:** `13,666` So this results in 3% improvement in build times (it might be just luck, but at least it doesn't increase build time).
1 parent 3620cd0 commit 2a66958

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

packages/gatsby-plugin-sharp/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"async": "^2.1.2",
1212
"bluebird": "^3.5.0",
1313
"fs-exists-cached": "^1.0.0",
14+
"fs-extra": "^7.0.0",
1415
"imagemin": "^6.0.0",
1516
"imagemin-mozjpeg": "^7.0.0",
1617
"imagemin-pngquant": "^6.0.0",

packages/gatsby-plugin-sharp/src/__tests__/__snapshots__/index.js.snap

+11-11
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ Object {
1515
"aspectRatio": 2.0661764705882355,
1616
"base64": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAABYlAAAWJQFJUiTwAAABAklEQVQoz61R7WrCQBDM+7+Cv1qhlIogoUWMoH8KrfgOLVXRKDV4d7kkl6/pbsxpqBIo9mBuZ7N3k51bB7TKsrwZVsexyS2rKerwFhYFVI1DnkMQOHIuCaLB+ayuwXlMaApXgvMoxjTU8MIQE4rMx8SnNffUkU/qM/bbiPAeRfgw5n8tB+QgO1kmUvwROdm0kYHmUNoe+NoU2wZTdVjQH7Isg9YRpFIXor8vpGmKjb/FQQgslit8fi3wvQ/OHXLhxRvDHY7Qd5+x3e2udmNzTUN4fZvhoddH566L+8cn9AZuVcvpLR3e9kEAISWEkDAmbRXkGCcJ1r4PRRNPEkPu9Kn2Azs0CqJYU3JKAAAAAElFTkSuQmCC",
1717
"density": 144,
18-
"originalImg": "/static/test-1234-e2565.png",
18+
"originalImg": "/static/1234/e2565/test.png",
1919
"originalName": undefined,
2020
"presentationHeight": 68,
2121
"presentationWidth": 141,
2222
"sizes": "(max-width: 141px) 100vw, 141px",
23-
"src": "/static/test-1234-e2565.png",
24-
"srcSet": "/static/test-1234-0b382.png 200w,
25-
/static/test-1234-e2565.png 281w",
23+
"src": "/static/1234/e2565/test.png",
24+
"srcSet": "/static/1234/0b382/test.png 200w,
25+
/static/1234/e2565/test.png 281w",
2626
"srcSetType": "image/png",
2727
}
2828
`;
@@ -32,14 +32,14 @@ Object {
3232
"aspectRatio": 2.0661764705882355,
3333
"base64": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAABYlAAAWJQFJUiTwAAABAklEQVQoz61R7WrCQBDM+7+Cv1qhlIogoUWMoH8KrfgOLVXRKDV4d7kkl6/pbsxpqBIo9mBuZ7N3k51bB7TKsrwZVsexyS2rKerwFhYFVI1DnkMQOHIuCaLB+ayuwXlMaApXgvMoxjTU8MIQE4rMx8SnNffUkU/qM/bbiPAeRfgw5n8tB+QgO1kmUvwROdm0kYHmUNoe+NoU2wZTdVjQH7Isg9YRpFIXor8vpGmKjb/FQQgslit8fi3wvQ/OHXLhxRvDHY7Qd5+x3e2udmNzTUN4fZvhoddH566L+8cn9AZuVcvpLR3e9kEAISWEkDAmbRXkGCcJ1r4PRRNPEkPu9Kn2Azs0CqJYU3JKAAAAAElFTkSuQmCC",
3434
"density": 144,
35-
"originalImg": "/static/test-1234-76aa7.png",
35+
"originalImg": "/static/1234/76aa7/test.png",
3636
"originalName": undefined,
3737
"presentationHeight": 136,
3838
"presentationWidth": 281,
3939
"sizes": "(max-width: 281px) 100vw, 281px",
40-
"src": "/static/test-1234-76aa7.png",
41-
"srcSet": "/static/test-1234-304a5.png 200w,
42-
/static/test-1234-76aa7.png 281w",
40+
"src": "/static/1234/76aa7/test.png",
41+
"srcSet": "/static/1234/304a5/test.png 200w,
42+
/static/1234/76aa7/test.png 281w",
4343
"srcSetType": "image/png",
4444
}
4545
`;
@@ -49,13 +49,13 @@ Object {
4949
"aspectRatio": 1,
5050
"base64": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAIAAAAC64paAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAGElEQVQ4y2P4TwFgGNU8qnlU86jmgdUMAOBEq42KgAyMAAAAAElFTkSuQmCC",
5151
"density": 72,
52-
"originalImg": "/static/test-1234-7bb67.png",
52+
"originalImg": "/static/1234/7bb67/test.png",
5353
"originalName": undefined,
5454
"presentationHeight": 1,
5555
"presentationWidth": 1,
5656
"sizes": "(max-width: 1px) 100vw, 1px",
57-
"src": "/static/test-1234-7bb67.png",
58-
"srcSet": "/static/test-1234-7bb67.png 1w",
57+
"src": "/static/1234/7bb67/test.png",
58+
"srcSet": "/static/1234/7bb67/test.png 1w",
5959
"srcSetType": "image/png",
6060
}
6161
`;

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
const path = require(`path`)
2+
const fs = require(`fs-extra`)
23

34
jest.mock(`async/queue`, () => () => {
45
return {
56
push: jest.fn(),
67
}
78
})
89

10+
fs.ensureDirSync = jest.fn()
11+
912
const {
1013
base64,
1114
fluid,
@@ -69,8 +72,8 @@ describe(`gatsby-plugin-sharp`, () => {
6972
file,
7073
})
7174

72-
expect(result.src.indexOf(file.name)).toBe(8)
73-
expect(result.srcSet.indexOf(file.name)).toBe(8)
75+
expect(path.parse(result.src).name).toBe(file.name)
76+
expect(path.parse(result.srcSet).name).toBe(file.name)
7477
})
7578

7679
it(`accounts for pixel density`, async () => {

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

+13-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const crypto = require(`crypto`)
33
const imageSize = require(`probe-image-size`)
44
const _ = require(`lodash`)
55
const Promise = require(`bluebird`)
6-
const fs = require(`fs`)
6+
const fs = require(`fs-extra`)
77
const ProgressBar = require(`progress`)
88
const imagemin = require(`imagemin`)
99
const imageminMozjpeg = require(`imagemin-mozjpeg`)
@@ -369,10 +369,16 @@ function queueImageResizing({ file, args = {}, reporter }) {
369369

370370
const argsDigestShort = argsDigest.substr(argsDigest.length - 5)
371371

372-
const imgSrc = `/${file.name}-${
373-
file.internal.contentDigest
374-
}-${argsDigestShort}.${fileExtension}`
375-
const filePath = path.join(process.cwd(), `public`, `static`, imgSrc)
372+
const imgSrc = `/${file.name}.${fileExtension}`
373+
const dirPath = path.join(
374+
process.cwd(),
375+
`public`,
376+
`static`,
377+
file.internal.contentDigest,
378+
argsDigestShort
379+
)
380+
const filePath = path.join(dirPath, imgSrc)
381+
fs.ensureDirSync(dirPath)
376382

377383
// Create function to call when the image is finished.
378384
let outsideResolve, outsideReject
@@ -421,7 +427,8 @@ function queueImageResizing({ file, args = {}, reporter }) {
421427
queueJob(job, reporter)
422428

423429
// Prefix the image src.
424-
const prefixedSrc = options.pathPrefix + `/static` + imgSrc
430+
const digestDirPrefix = `${file.internal.contentDigest}/${argsDigestShort}`
431+
const prefixedSrc = options.pathPrefix + `/static/${digestDirPrefix}` + imgSrc
425432

426433
return {
427434
src: prefixedSrc,

0 commit comments

Comments
 (0)