Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gatsby-plugin-sharp): fix progressbar & caching layer #19717

Merged
merged 5 commits into from
Nov 24, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 26 additions & 4 deletions packages/gatsby-plugin-sharp/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ jest.mock(`async/queue`, () => () => {
}
})

const { scheduleJob } = require(`../scheduler`)
fs.ensureDirSync = jest.fn()
fs.existsSync = jest.fn().mockReturnValue(false)
let isolatedQueueImageResizing
jest.isolateModules(() => {
isolatedQueueImageResizing = require(`../index`).queueImageResizing
})

const {
base64,
Expand All @@ -20,8 +25,6 @@ const {
getImageSize,
stats,
} = require(`../`)
const { scheduleJob } = require(`../scheduler`)
scheduleJob.mockResolvedValue(Promise.resolve())

jest.mock(`gatsby-cli/lib/reporter`, () => {
return {
Expand Down Expand Up @@ -56,6 +59,12 @@ describe(`gatsby-plugin-sharp`, () => {
}

describe(`queueImageResizing`, () => {
beforeEach(() => {
scheduleJob.mockClear()
fs.existsSync.mockReset()
fs.existsSync.mockReturnValue(false)
})

it(`should round height when auto-calculated`, () => {
// Resize 144-density.png (281x136) with a 3px width
const result = queueImageResizing({
Expand Down Expand Up @@ -93,7 +102,6 @@ describe(`gatsby-plugin-sharp`, () => {

// re-enable when image processing on demand is implemented
it.skip(`should process immediately when asked`, async () => {
scheduleJob.mockClear()
const result = queueImageResizing({
file: getFileObject(path.join(__dirname, `images/144-density.png`)),
args: { width: 3 },
Expand All @@ -105,7 +113,6 @@ describe(`gatsby-plugin-sharp`, () => {
})

it(`Shouldn't schedule a job when outputFile already exists`, async () => {
scheduleJob.mockClear()
fs.existsSync.mockReturnValue(true)

const result = queueImageResizing({
Expand All @@ -118,6 +125,21 @@ describe(`gatsby-plugin-sharp`, () => {
expect(fs.existsSync).toHaveBeenCalledWith(result.absolutePath)
expect(scheduleJob).not.toHaveBeenCalled()
})

it(`Shouldn't schedule a job when with same outputFile is already being queued`, async () => {
const result = isolatedQueueImageResizing({
file: getFileObject(path.join(__dirname, `images/144-density.png`)),
args: { width: 5 },
})
isolatedQueueImageResizing({
file: getFileObject(path.join(__dirname, `images/144-density.png`)),
args: { width: 5 },
})

await result.finishedPromise

expect(scheduleJob).toHaveBeenCalledTimes(1)
})
})

describe(`fluid`, () => {
Expand Down
7 changes: 4 additions & 3 deletions packages/gatsby-plugin-sharp/src/__tests__/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe(`scheduler`, () => {
},
contentDigest: `digest`,
}
await scheduleJob(job, boundActionCreators, {}, false, `value`)
await scheduleJob(job, boundActionCreators, `value`, {}, false)

expect(createProgress).not.toHaveBeenCalled()
expect(workerMock).toHaveBeenCalledWith([job.inputPath], job.outputDir, {
Expand Down Expand Up @@ -110,6 +110,7 @@ describe(`scheduler`, () => {
},
boundActionCreators,
{},
{},
false
)
} catch (err) {
Expand Down Expand Up @@ -147,7 +148,7 @@ describe(`scheduler`, () => {
},
contentDigest: `digest`,
}
const job1Promise = scheduleJob(job1, boundActionCreators, {}, false)
const job1Promise = scheduleJob(job1, boundActionCreators, {}, {}, false)

const job2 = {
inputPath: `/test-image.jpg`,
Expand All @@ -158,7 +159,7 @@ describe(`scheduler`, () => {
},
contentDigest: `digest`,
}
const job2Promise = scheduleJob(job2, boundActionCreators, {}, false)
const job2Promise = scheduleJob(job2, boundActionCreators, {}, {}, false)

await Promise.all([job1Promise, job2Promise])

Expand Down
13 changes: 10 additions & 3 deletions packages/gatsby-plugin-sharp/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ exports.setBoundActionCreators = actions => {
boundActionCreators = actions
}

const cachedOutputFiles = new Map()
function queueImageResizing({ file, args = {}, reporter }) {
const pluginOptions = getPluginOptions()
const options = healOptions(pluginOptions, args, file.extension)
Expand Down Expand Up @@ -103,19 +104,25 @@ function queueImageResizing({ file, args = {}, reporter }) {
outputPath: outputFilePath,
}

const outputFile = path.join(job.outputDir, job.outputPath)
let finishedPromise = Promise.resolve()

// Check if the output file already exists so we don't redo work.
if (cachedOutputFiles.has(outputFile)) {
finishedPromise = cachedOutputFiles.get(outputFile)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we return early here to avoid && !cachedOutputFiles.has(outputFile) check in L116

}

// Check if the output file already exists or already is being created.
// TODO: Remove this when jobs api is stable, it will have a better check
const outputFile = path.join(job.outputDir, job.outputPath)
if (!fs.existsSync(outputFile)) {
if (!fs.existsSync(outputFile) && !cachedOutputFiles.has(outputFile)) {
// schedule job immediately - this will be changed when image processing on demand is implemented
finishedPromise = scheduleJob(
job,
boundActionCreators,
pluginOptions,
reporter
)

cachedOutputFiles.set(outputFile, finishedPromise)
pieh marked this conversation as resolved.
Show resolved Hide resolved
}

return {
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-plugin-sharp/src/scheduler.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ const executeJobs = _.throttle(
const scheduleJob = async (
job,
boundActionCreators,
pluginOptions,
reporter,
reportStatus = true,
pluginOptions
reportStatus = true
) => {
const isQueued = toProcess.has(job.inputPath)
let scheduledPromise
Expand Down