Skip to content

Commit 3b40d80

Browse files
authored
feat(gatsby): enable lazy images by default (#28743)
* feat(gatsby): enable lazy images by default * revert changes to flags.ts * Keep disabled in CI
1 parent 968914f commit 3b40d80

File tree

2 files changed

+19
-48
lines changed

2 files changed

+19
-48
lines changed

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

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,45 +3,15 @@ const {
33
// queue: jobQueue,
44
// reportError,
55
_unstable_createJob,
6+
_lazyJobsEnabled,
67
} = require(`./index`)
78
const { pathExists } = require(`fs-extra`)
8-
const { slash, isCI } = require(`gatsby-core-utils`)
9-
const { trackFeatureIsUsed } = require(`gatsby-telemetry`)
9+
const { slash } = require(`gatsby-core-utils`)
1010
const { getProgressBar, createOrGetProgressBar } = require(`./utils`)
1111

1212
const { setPluginOptions } = require(`./plugin-options`)
1313
const path = require(`path`)
1414

15-
function prepareLazyImagesExperiment(reporter) {
16-
if (!process.env.GATSBY_EXPERIMENTAL_LAZY_IMAGES) {
17-
return
18-
}
19-
if (process.env.gatsby_executing_command !== `develop`) {
20-
// We don't want to ever have this flag enabled for anything other than develop
21-
// in case someone have this env var globally set
22-
delete process.env.GATSBY_EXPERIMENTAL_LAZY_IMAGES
23-
return
24-
}
25-
if (isCI()) {
26-
delete process.env.GATSBY_EXPERIMENTAL_LAZY_IMAGES
27-
reporter.warn(
28-
`Lazy Image Processing experiment is not available in CI environment. Continuing with regular mode.`
29-
)
30-
return
31-
}
32-
// We show a different notice for GATSBY_EXPERIMENTAL_FAST_DEV umbrella
33-
if (!process.env.GATSBY_EXPERIMENTAL_FAST_DEV) {
34-
reporter.info(
35-
`[gatsby-plugin-sharp] The lazy image processing experiment is enabled`
36-
)
37-
}
38-
trackFeatureIsUsed(`LazyImageProcessing`)
39-
}
40-
41-
exports.onPreInit = ({ reporter }) => {
42-
prepareLazyImagesExperiment(reporter)
43-
}
44-
4515
// create the progressbar once and it will be killed in another lifecycle
4616
const finishProgressBar = () => {
4717
const progressBar = getProgressBar()
@@ -53,7 +23,7 @@ const finishProgressBar = () => {
5323
exports.onPostBuild = () => finishProgressBar()
5424

5525
exports.onCreateDevServer = async ({ app, cache, reporter }) => {
56-
if (!process.env.GATSBY_EXPERIMENTAL_LAZY_IMAGES) {
26+
if (!_lazyJobsEnabled()) {
5727
finishProgressBar()
5828
return
5929
}

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const sharp = require(`./safe-sharp`)
22
const { generateImageData } = require(`./image-data`)
33
const imageSize = require(`probe-image-size`)
4+
const { isCI } = require(`gatsby-core-utils`)
45

56
const _ = require(`lodash`)
67
const fs = require(`fs-extra`)
@@ -151,6 +152,17 @@ function createJob(job, { reporter }) {
151152
return promise
152153
}
153154

155+
function lazyJobsEnabled() {
156+
return (
157+
process.env.gatsby_executing_command === `develop` &&
158+
!isCI() &&
159+
!(
160+
process.env.ENABLE_GATSBY_EXTERNAL_JOBS === `true` ||
161+
process.env.ENABLE_GATSBY_EXTERNAL_JOBS === `1`
162+
)
163+
)
164+
}
165+
154166
function queueImageResizing({ file, args = {}, reporter }) {
155167
const fullOptions = healOptions(getPluginOptions(), args, file.extension)
156168
const {
@@ -170,13 +182,7 @@ function queueImageResizing({ file, args = {}, reporter }) {
170182
inputPaths: [file.absolutePath],
171183
outputDir,
172184
args: {
173-
isLazy:
174-
!(
175-
process.env.ENABLE_GATSBY_EXTERNAL_JOBS === `true` ||
176-
process.env.ENABLE_GATSBY_EXTERNAL_JOBS === `1`
177-
) &&
178-
process.env.gatsby_executing_command === `develop` &&
179-
!!process.env.GATSBY_EXPERIMENTAL_LAZY_IMAGES,
185+
isLazy: lazyJobsEnabled(),
180186
operations: [
181187
{
182188
outputPath: relativePath,
@@ -244,13 +250,7 @@ function batchQueueImageResizing({ file, transforms = [], reporter }) {
244250
file.internal.contentDigest
245251
),
246252
args: {
247-
isLazy:
248-
!(
249-
process.env.ENABLE_GATSBY_EXTERNAL_JOBS === `true` ||
250-
process.env.ENABLE_GATSBY_EXTERNAL_JOBS === `1`
251-
) &&
252-
process.env.gatsby_executing_command === `develop` &&
253-
!!process.env.GATSBY_EXPERIMENTAL_LAZY_IMAGES,
253+
isLazy: lazyJobsEnabled(),
254254
operations,
255255
pluginOptions: getPluginOptions(),
256256
},
@@ -341,7 +341,7 @@ async function generateBase64({ file, args = {}, reporter }) {
341341
info = result.info
342342
} catch (err) {
343343
reportError(
344-
`Failed to process image ${file.absolutePath}.
344+
`Failed to process image ${file.absolutePath}.
345345
It is probably corrupt, so please try replacing it. If it still fails, please open an issue with the image attached.`,
346346
err,
347347
reporter
@@ -773,3 +773,4 @@ exports.getImageSize = getImageSize
773773
exports.getImageSizeAsync = getImageSizeAsync
774774
exports.stats = stats
775775
exports._unstable_createJob = createJob
776+
exports._lazyJobsEnabled = lazyJobsEnabled

0 commit comments

Comments
 (0)