Skip to content

Commit f19c807

Browse files
piehvladar
andauthored
chore(gatsby): enable query on demand (and lazy images) by default for local development (#28787)
* feat(flags): add LOCKED_IN mode for flags for cases where opt-in features become defaults * feat(gatsby): enable query on demand (and lazy images) by default for local development * fix "lint" * chore(telemetry): don't track usage of query on demand / lazy images anymore - those are defaults * use plain false instead of weird Symbol do opt out of telemetry tracking * Update packages/gatsby/src/utils/handle-flags.ts Co-authored-by: Vladimir Razuvaev <[email protected]> * update snapshots Co-authored-by: Vladimir Razuvaev <[email protected]>
1 parent bd6b899 commit f19c807

File tree

5 files changed

+203
-87
lines changed

5 files changed

+203
-87
lines changed

packages/gatsby/src/services/initialize.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { IPluginInfoOptions } from "../bootstrap/load-plugins/types"
2828
import { internalActions } from "../redux/actions"
2929
import { IGatsbyState } from "../redux/types"
3030
import { IBuildContext } from "./types"
31+
import availableFlags from "../utils/flags"
3132

3233
interface IPluginResolution {
3334
resolve: string
@@ -41,18 +42,17 @@ if (
4142
process.env.GATSBY_EXPERIMENTAL_FAST_DEV &&
4243
!isCI()
4344
) {
44-
process.env.GATSBY_EXPERIMENTAL_LAZY_IMAGES = `true`
45-
process.env.GATSBY_EXPERIMENTAL_QUERY_ON_DEMAND = `true`
4645
process.env.GATSBY_EXPERIMENTAL_DEV_SSR = `true`
46+
process.env.PRESERVE_FILE_DOWNLOAD_CACHE = `true`
47+
process.env.PRESERVE_WEBPACK_CACHE = `true`
4748

4849
reporter.info(`
49-
Three fast dev experiments are enabled: Query on Demand, Development SSR, and Lazy Images (only with gatsby-plugin-sharp@^2.10.0).
50+
Three fast dev experiments are enabled: Development SSR, preserving file download cache and preserving webpack cache.
5051
5152
Please give feedback on their respective umbrella issues!
5253
53-
- https://gatsby.dev/query-on-demand-feedback
5454
- https://gatsby.dev/dev-ssr-feedback
55-
- https://gatsby.dev/lazy-images-feedback
55+
- https://gatsby.dev/cache-clearing-feedback
5656
`)
5757

5858
telemetry.trackFeatureIsUsed(`FastDev`)
@@ -194,7 +194,6 @@ export async function initialize({
194194
)
195195
}
196196

197-
const availableFlags = require(`../utils/flags`).default
198197
// Get flags
199198
const { enabledConfigFlags, unknownFlagMessage, message } = handleFlags(
200199
availableFlags,
@@ -217,7 +216,9 @@ export async function initialize({
217216

218217
// track usage of feature
219218
enabledConfigFlags.forEach(flag => {
220-
telemetry.trackFeatureIsUsed(flag.telemetryId)
219+
if (flag.telemetryId) {
220+
telemetry.trackFeatureIsUsed(flag.telemetryId)
221+
}
221222
})
222223

223224
// Track the usage of config.flags
@@ -282,15 +283,9 @@ export async function initialize({
282283
delete process.env.GATSBY_EXPERIMENTAL_QUERY_ON_DEMAND
283284
} else if (isCI()) {
284285
delete process.env.GATSBY_EXPERIMENTAL_QUERY_ON_DEMAND
285-
reporter.warn(
286-
`Experimental Query on Demand feature is not available in CI environment. Continuing with regular mode.`
286+
reporter.verbose(
287+
`Experimental Query on Demand feature is not available in CI environment. Continuing with eager query running.`
287288
)
288-
} else {
289-
// We already show a notice for this flag.
290-
if (!process.env.GATSBY_EXPERIMENTAL_FAST_DEV) {
291-
reporter.info(`Using experimental Query on Demand feature`)
292-
}
293-
telemetry.trackFeatureIsUsed(`QueryOnDemand`)
294289
}
295290
}
296291

packages/gatsby/src/utils/__tests__/__snapshots__/handle-flags.ts.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ Object {
8585
"umbrellaIssue": "test",
8686
},
8787
],
88-
"message": "
89-
90-
We're shipping new features! For final testing, we're rolling them out first to a small % of Gatsby users
88+
"message": "We're shipping new features! For final testing, we're rolling them out first to a small % of Gatsby users
9189
and your site was automatically chosen as one of them. With your help, we'll then release them to everyone in the next minor release.
9290
9391
We greatly appreciate your help testing the change. Please report any feedback good or bad in the umbrella issue. If you do encounter problems, please disable the flag by setting it to false in your gatsby-config.js like:

packages/gatsby/src/utils/__tests__/handle-flags.ts

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,129 @@ describe(`handle flags`, () => {
276276
"
277277
`)
278278
})
279+
280+
describe(`LOCKED_IN`, () => {
281+
it(`Enables locked in flag by default and doesn't mention it in terminal (no spam)`, () => {
282+
const response = handleFlags(
283+
[
284+
{
285+
name: `ALWAYS_LOCKED_IN`,
286+
env: `GATSBY_ALWAYS_LOCKED_IN`,
287+
command: `all`,
288+
description: `test`,
289+
umbrellaIssue: `test`,
290+
telemetryId: `test`,
291+
experimental: false,
292+
// this will always LOCKED IN
293+
testFitness: (): fitnessEnum => `LOCKED_IN`,
294+
},
295+
],
296+
{},
297+
`develop`
298+
)
299+
300+
expect(response.enabledConfigFlags).toContainEqual(
301+
expect.objectContaining({ name: `ALWAYS_LOCKED_IN` })
302+
)
303+
expect(response.message).toEqual(``)
304+
})
305+
306+
it(`Display message saying config flag for LOCKED_IN feature is no-op`, () => {
307+
const response = handleFlags(
308+
[
309+
{
310+
name: `ALWAYS_LOCKED_IN_SET_IN_CONFIG`,
311+
env: `GATSBY_ALWAYS_LOCKED_IN_SET_IN_CONFIG`,
312+
command: `all`,
313+
description: `test`,
314+
umbrellaIssue: `test`,
315+
telemetryId: `test`,
316+
experimental: false,
317+
// this will always LOCKED IN
318+
testFitness: (): fitnessEnum => `LOCKED_IN`,
319+
},
320+
],
321+
{
322+
// this has no effect, but we want to show to user that
323+
ALWAYS_LOCKED_IN_SET_IN_CONFIG: true,
324+
},
325+
`develop`
326+
)
327+
328+
expect(response.enabledConfigFlags).toContainEqual(
329+
expect.objectContaining({ name: `ALWAYS_LOCKED_IN_SET_IN_CONFIG` })
330+
)
331+
expect(response.message).toMatchInlineSnapshot(`
332+
"Some features you configured with flags are used natively now.
333+
Those flags no longer have any effect and you can remove them from config:
334+
- ALWAYS_LOCKED_IN_SET_IN_CONFIG · (Umbrella Issue (test)) · test
335+
"
336+
`)
337+
})
338+
339+
it(`Kitchen sink`, () => {
340+
const response = handleFlags(
341+
activeFlags.concat([
342+
{
343+
name: `ALWAYS_LOCKED_IN`,
344+
env: `GATSBY_ALWAYS_LOCKED_IN`,
345+
command: `all`,
346+
description: `test`,
347+
umbrellaIssue: `test`,
348+
telemetryId: `test`,
349+
experimental: false,
350+
// this will always LOCKED IN
351+
testFitness: (): fitnessEnum => `LOCKED_IN`,
352+
},
353+
{
354+
name: `ALWAYS_LOCKED_IN_SET_IN_CONFIG`,
355+
env: `GATSBY_ALWAYS_LOCKED_IN_SET_IN_CONFIG`,
356+
command: `all`,
357+
description: `test`,
358+
umbrellaIssue: `test`,
359+
telemetryId: `test`,
360+
experimental: false,
361+
// this will always LOCKED IN
362+
testFitness: (): fitnessEnum => `LOCKED_IN`,
363+
},
364+
]),
365+
{
366+
ALWAYS_OPT_IN: true,
367+
DEV_SSR: true,
368+
PARTIAL_RELEASE: false,
369+
PARTIAL_RELEASE_ONLY_NEW_LODASH: false,
370+
// this has no effect, but we want to show to user that
371+
ALWAYS_LOCKED_IN_SET_IN_CONFIG: true,
372+
},
373+
`develop`
374+
)
375+
376+
// this is enabled, but because it's not configurable anymore and user doesn't set it explicitly in config - there is no point in printing information about it
377+
expect(response.enabledConfigFlags).toContainEqual(
378+
expect.objectContaining({ name: `ALWAYS_LOCKED_IN` })
379+
)
380+
// this is enabled, but because it's not configurable anymore and user sets it in config - we want to mention that this config flag has no effect anymore
381+
expect(response.enabledConfigFlags).toContainEqual(
382+
expect.objectContaining({ name: `ALWAYS_LOCKED_IN_SET_IN_CONFIG` })
383+
)
384+
expect(response.message).toMatchInlineSnapshot(`
385+
"The following flags are active:
386+
- DEV_SSR · (Umbrella Issue (https://github.com/gatsbyjs/gatsby/discussions/28138)) · SSR pages on full reloads during develop. Helps you detect SSR bugs and fix them without needing to do full builds.
387+
388+
Some features you configured with flags are used natively now.
389+
Those flags no longer have any effect and you can remove them from config:
390+
- ALWAYS_LOCKED_IN_SET_IN_CONFIG · (Umbrella Issue (test)) · test
391+
392+
There are 5 other flags available that you might be interested in:
393+
- FAST_DEV · Enable all experiments aimed at improving develop server start time
394+
- QUERY_ON_DEMAND · (Umbrella Issue (https://github.com/gatsbyjs/gatsby/discussions/27620)) · Only run queries when needed instead of running all queries upfront. Speeds starting the develop server.
395+
- ONLY_BUILDS · (Umbrella Issue (test)) · test
396+
- ALL_COMMANDS · (Umbrella Issue (test)) · test
397+
- YET_ANOTHER · (Umbrella Issue (test)) · test
398+
- PARTIAL_RELEASE · (Umbrella Issue (test)) · test
399+
- PARTIAL_RELEASE_ONLY_NEW_LODASH · (Umbrella Issue (test)) · test
400+
"
401+
`)
402+
})
403+
})
279404
})

packages/gatsby/src/utils/flags.ts

Lines changed: 31 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sampleSiteForExperiment from "./sample-site-for-experiment"
21
import _ from "lodash"
32
import semver from "semver"
43

@@ -29,14 +28,17 @@ export const satisfiesSemvers = (
2928
return result
3029
}
3130

32-
export type fitnessEnum = true | false | "OPT_IN"
31+
export type fitnessEnum = true | false | "OPT_IN" | "LOCKED_IN"
3332

3433
export interface IFlag {
3534
name: string
3635
env: string
3736
description: string
3837
command: executingCommand
39-
telemetryId: string
38+
/**
39+
* Use string identifier to track enabled flag or false to disable any tracking (useful when flag becomes new defaults)
40+
*/
41+
telemetryId: string | false
4042
// Heuristics for deciding if a flag is experimental:
4143
// - there are known bugs most people will encounter and that block being
4244
// able to use Gatsby normally
@@ -47,13 +49,20 @@ export interface IFlag {
4749
// resolved and ~50+ people have tested it, experimental should be set to
4850
// false.
4951
experimental: boolean
50-
// Generally just return true.
51-
//
52-
// False means it'll be disabled despite the user setting it true e.g.
53-
// it just won't work e.g. it doesn't have new enough version for something.
54-
//
55-
// OPT_IN means the gatsby will enable the flag (unless the user explicitly
56-
// disablees it.
52+
/**
53+
* True means conditions for the feature are met and can be opted in by user.
54+
*
55+
* False means it'll be disabled despite the user setting it true e.g.
56+
* it just won't work e.g. it doesn't have new enough version for something.
57+
*
58+
* OPT_IN means the gatsby will enable the flag (unless the user explicitly
59+
* disables it.
60+
*
61+
* LOCKED_IN means that feature is enabled always (unless `noCI` condition is met).
62+
* This is mostly to provide more meaningful terminal messages instead of removing
63+
* flag from the flag list when users has the flag set in configuration
64+
* (avoids showing unknown flag message and shows "no longer needed" message).
65+
*/
5766
testFitness: (flag: IFlag) => fitnessEnum
5867
includedFlags?: Array<string>
5968
umbrellaIssue?: string
@@ -70,8 +79,6 @@ const activeFlags: Array<IFlag> = [
7079
description: `Enable all experiments aimed at improving develop server start time`,
7180
includedFlags: [
7281
`DEV_SSR`,
73-
`QUERY_ON_DEMAND`,
74-
`LAZY_IMAGES`,
7582
`PRESERVE_FILE_DOWNLOAD_CACHE`,
7683
`PRESERVE_WEBPACK_CACHE`,
7784
],
@@ -91,70 +98,33 @@ const activeFlags: Array<IFlag> = [
9198
name: `QUERY_ON_DEMAND`,
9299
env: `GATSBY_EXPERIMENTAL_QUERY_ON_DEMAND`,
93100
command: `develop`,
94-
telemetryId: `QueryOnDemand`,
101+
telemetryId: false,
95102
experimental: false,
96103
description: `Only run queries when needed instead of running all queries upfront. Speeds starting the develop server.`,
97104
umbrellaIssue: `https://gatsby.dev/query-on-demand-feedback`,
98105
noCI: true,
99-
testFitness: (): fitnessEnum => {
100-
// Take a 10% of slice of users.
101-
if (sampleSiteForExperiment(`QUERY_ON_DEMAND`, 10)) {
102-
let isPluginSharpNewEnoughOrNotInstalled = false
103-
try {
104-
// Try requiring plugin-sharp so we know if it's installed or not.
105-
// If it does, we also check if it's new enough.
106-
// eslint-disable-next-line
107-
require.resolve(`gatsby-plugin-sharp`)
108-
const semverConstraints = {
109-
// Because of this, this flag will never show up
110-
"gatsby-plugin-sharp": `>=2.10.0`,
111-
}
112-
if (satisfiesSemvers(semverConstraints)) {
113-
isPluginSharpNewEnoughOrNotInstalled = true
114-
}
115-
} catch (e) {
116-
if (e.code === `MODULE_NOT_FOUND`) {
117-
isPluginSharpNewEnoughOrNotInstalled = true
118-
}
119-
}
120-
121-
if (isPluginSharpNewEnoughOrNotInstalled) {
122-
return `OPT_IN`
123-
} else {
124-
// Don't opt them in but they can still manually enable.
125-
return true
126-
}
127-
} else {
128-
// Don't opt them in but they can still manually enable.
129-
return true
130-
}
131-
},
106+
testFitness: (): fitnessEnum => `LOCKED_IN`,
132107
},
133108
{
134109
name: `LAZY_IMAGES`,
135110
env: `GATSBY_EXPERIMENTAL_LAZY_IMAGES`,
136111
command: `develop`,
137-
telemetryId: `LazyImageProcessing`,
112+
telemetryId: false,
138113
experimental: false,
139114
description: `Don't process images during development until they're requested from the browser. Speeds starting the develop server. Requires [email protected] or above.`,
140115
umbrellaIssue: `https://gatsby.dev/lazy-images-feedback`,
141116
noCI: true,
142117
testFitness: (): fitnessEnum => {
143-
// Take a 10% of slice of users.
144-
if (sampleSiteForExperiment(`QUERY_ON_DEMAND`, 10)) {
145-
const semverConstraints = {
146-
// Because of this, this flag will never show up
147-
"gatsby-plugin-sharp": `>=2.10.0`,
148-
}
149-
if (satisfiesSemvers(semverConstraints)) {
150-
return `OPT_IN`
151-
} else {
152-
// gatsby-plugi-sharp is either not installed or not new enough so
153-
// just disable — it won't work anyways.
154-
return false
155-
}
118+
const semverConstraints = {
119+
// Because of this, this flag will never show up
120+
"gatsby-plugin-sharp": `>=2.10.0`,
121+
}
122+
if (satisfiesSemvers(semverConstraints)) {
123+
return `LOCKED_IN`
156124
} else {
157-
return true
125+
// gatsby-plugin-sharp is either not installed or not new enough so
126+
// just disable — it won't work anyways.
127+
return false
158128
}
159129
},
160130
},

0 commit comments

Comments
 (0)