Skip to content

Commit 7e734cc

Browse files
KyleAMathewsascorbicgatsbybotpieh
authored
fix(gatsby): show multiple invites together & at end where people are more likely to see them (#28450)
* fix(gatsby): show multiple invites together & at end where people are more likely to see them * Add telemetry * update copy from suggestions by @DSchau * Actually stop listening * Update packages/gatsby/src/utils/show-experiment-notice.ts Co-authored-by: Matt Kane <[email protected]> * Add test for generating the message * rewrite messages & make shorter * make all the things happy * update flag * This breaking windows?? * Don't use explicit \n to see if that helps snapshot * Maybe jest is fine w/ explicit new-lines 🤷‍♂️ * Strip ansi for tests * Update packages/gatsby/src/services/initialize.ts Co-authored-by: Michal Piechowiak <[email protected]> * Update packages/gatsby/src/services/run-page-queries.ts Co-authored-by: Michal Piechowiak <[email protected]> * use trackCLI not trackFeatureIsUsed * only store that we showed the invite when we actually do show the invite * Call code directly from webpack's done callback * mock in jest * be more explicit about how the caching behavior changes * Show full code sample per @pelikhan's feedback * typescript fixes * Update packages/gatsby/src/services/initialize.ts Co-authored-by: Michal Piechowiak <[email protected]> * Update packages/gatsby/src/services/initialize.ts Co-authored-by: Matt Kane <[email protected]> Co-authored-by: gatsbybot <[email protected]> Co-authored-by: Michal Piechowiak <[email protected]>
1 parent 755950d commit 7e734cc

File tree

8 files changed

+113
-45
lines changed

8 files changed

+113
-45
lines changed

packages/gatsby/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
"st": "^2.0.0",
144144
"stack-trace": "^0.0.10",
145145
"string-similarity": "^1.2.2",
146+
"strip-ansi": "^5.2.0",
146147
"style-loader": "^0.23.1",
147148
"terminal-link": "^2.1.1",
148149
"terser-webpack-plugin": "^2.3.8",

packages/gatsby/src/services/initialize.ts

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,14 @@ if (
6565
sampleSiteForExperiment(`DEV_SSR`, 5)
6666
) {
6767
showExperimentNoticeAfterTimeout(
68-
`devSSR`,
69-
`
70-
Your dev experience is about to get better, faster, and stronger!
71-
72-
We'll soon be shipping support for SSR in development.
73-
74-
This will help the dev environment more closely mimic builds so you'll catch build errors earlier and fix them faster.
75-
76-
Try out develop SSR *today* by running your site with it enabled:
77-
78-
GATSBY_EXPERIMENTAL_DEV_SSR=true gatsby develop
79-
80-
Please let us know how it goes good, bad, or otherwise at gatsby.dev/dev-ssr-feedback
81-
`,
68+
`Server Side Rendering (SSR) in Development`,
69+
`gatsby.dev/dev-ssr-feedback`,
70+
`which helps surface issues with build errors more quickly. Here's how to try it:
71+
72+
module.exports = {
73+
flags : { DEV_SSR: true },
74+
plugins: [...]
75+
}`,
8276
1 // Show this immediately to the subset of sites selected.
8377
)
8478
}

packages/gatsby/src/services/run-page-queries.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,15 @@ export async function runPageQueries({
5050
!isCI()
5151
) {
5252
cancelNotice = showExperimentNoticeAfterTimeout(
53-
`queryOnDemand`,
54-
reporter.stripIndent(`
55-
Your local development experience is about to get better, faster, and stronger!
53+
`Query On Demand`,
54+
`https://gatsby.dev/query-on-demand-feedback`,
55+
`which avoids running page queries in development until you visit a page — so a lot less upfront work. Here's how to try it:
5656
57-
Your friendly Gatsby maintainers detected your site takes longer than ideal to run page queries. We're working right now to improve this.
58-
59-
If you're interested in trialing out one of these future improvements *today* which should make your local development experience faster, go ahead and run your site with QUERY_ON_DEMAND enabled.
60-
61-
You can enable it by adding "flags: { QUERY_ON_DEMAND: true }" to your gatsby-config.js
62-
63-
Please do let us know how it goes (good, bad, or otherwise) and learn more about it at https://gatsby.dev/query-on-demand-feedback
64-
`),
57+
modules.exports = {
58+
flags: { QUERY_ON_DEMAND: true },
59+
plugins: [...]
60+
}
61+
`,
6562
ONE_MINUTE
6663
)
6764
}

packages/gatsby/src/services/start-webpack-server.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from "../utils/webpack-error-utils"
1313

1414
import { printDeprecationWarnings } from "../utils/print-deprecation-warnings"
15+
import { showExperimentNotices } from "../utils/show-experiment-notice"
1516
import { printInstructions } from "../utils/print-instructions"
1617
import { prepareUrls } from "../utils/prepare-urls"
1718
import { startServer, IWebpackWatchingPauseResume } from "../utils/start-server"
@@ -97,6 +98,9 @@ export async function startWebpackServer({
9798
const isSuccessful = !messages.errors.length
9899

99100
if (isSuccessful && isFirstCompile) {
101+
// Show notices to users about potential experiments/feature flags they could
102+
// try.
103+
showExperimentNotices()
100104
printInstructions(
101105
program.sitePackageJson.name || `(Unnamed package)`,
102106
urls
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`show-experiment-notice generates a message 1`] = `
4+
"
5+
Hi from the Gatsby maintainers! Based on what we see in your site, these coming
6+
features may help you. All of these can be enabled within gatsby-config.js via
7+
flags (samples below)
8+
9+
The Flag (http://example.com), hi
10+
"
11+
`;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { createNoticeMessage } from "../show-experiment-notice"
2+
import stripAnsi from "strip-ansi"
3+
4+
jest.mock(`terminal-link`, () => (text, url) => `${text} (${url})`)
5+
6+
describe(`show-experiment-notice`, () => {
7+
it(`generates a message`, () => {
8+
expect(
9+
stripAnsi(
10+
createNoticeMessage([
11+
{
12+
noticeText: `hi`,
13+
umbrellaLink: `http://example.com`,
14+
experimentIdentifier: `The Flag`,
15+
},
16+
])
17+
)
18+
).toMatchSnapshot()
19+
})
20+
})
Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { getConfigStore } from "gatsby-core-utils"
22
import reporter from "gatsby-cli/lib/reporter"
3+
import chalk from "chalk"
4+
import telemetry from "gatsby-telemetry"
5+
import terminalLink from "terminal-link"
36

47
type CancelExperimentNoticeCallback = () => void
58

@@ -9,15 +12,26 @@ export type CancelExperimentNoticeCallbackOrUndefined =
912

1013
const ONE_DAY = 24 * 60 * 60 * 1000
1114

15+
interface INoticeObject {
16+
noticeText: string
17+
umbrellaLink: string
18+
experimentIdentifier: string
19+
}
20+
21+
const noticesToShow: Array<INoticeObject> = []
22+
const configStoreKey = (experimentIdentifier): string =>
23+
`lastExperimentNotice.${experimentIdentifier}`
24+
1225
export function showExperimentNoticeAfterTimeout(
1326
experimentIdentifier: string,
27+
umbrellaLink: string,
1428
noticeText: string,
1529
showNoticeAfterMs: number,
1630
minimumIntervalBetweenNoticesMs: number = ONE_DAY
1731
): CancelExperimentNoticeCallbackOrUndefined {
18-
const configStoreKey = `lastExperimentNotice.${experimentIdentifier}`
19-
20-
const lastTimeWeShowedNotice = getConfigStore().get(configStoreKey)
32+
const lastTimeWeShowedNotice = getConfigStore().get(
33+
configStoreKey(experimentIdentifier)
34+
)
2135

2236
if (lastTimeWeShowedNotice) {
2337
if (Date.now() - lastTimeWeShowedNotice < minimumIntervalBetweenNoticesMs) {
@@ -26,12 +40,43 @@ export function showExperimentNoticeAfterTimeout(
2640
}
2741

2842
const noticeTimeout = setTimeout(() => {
29-
reporter.info(`\n\n${noticeText}\n\n`)
30-
31-
getConfigStore().set(configStoreKey, Date.now())
43+
noticesToShow.push({ noticeText, umbrellaLink, experimentIdentifier })
3244
}, showNoticeAfterMs)
3345

3446
return function clearNoticeTimeout(): void {
3547
clearTimeout(noticeTimeout)
3648
}
3749
}
50+
51+
export const createNoticeMessage = (notices): string => {
52+
let message = `\nHi from the Gatsby maintainers! Based on what we see in your site, these coming
53+
features may help you. All of these can be enabled within gatsby-config.js via
54+
flags (samples below)`
55+
56+
notices.forEach(
57+
notice =>
58+
(message += `
59+
60+
${chalk.bgBlue.bold(
61+
terminalLink(notice.experimentIdentifier, notice.umbrellaLink)
62+
)}, ${notice.noticeText}\n`)
63+
)
64+
65+
return message
66+
}
67+
68+
export const showExperimentNotices = (): void => {
69+
if (noticesToShow.length > 0) {
70+
telemetry.trackCli(`InviteToTryExperiment`)
71+
// Store that we're showing the invite.
72+
noticesToShow.forEach(notice =>
73+
getConfigStore().set(
74+
configStoreKey(notice.experimentIdentifier),
75+
Date.now()
76+
)
77+
)
78+
79+
const message = createNoticeMessage(noticesToShow)
80+
reporter.info(message)
81+
}
82+
}

packages/gatsby/src/utils/start-server.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,20 +93,16 @@ export async function startServer(
9393
!isCI()
9494
) {
9595
cancelDevJSNotice = showExperimentNoticeAfterTimeout(
96-
`PRESERVE_WEBPACK_CACHE`,
97-
report.stripIndent(`
98-
Your friendly Gatsby maintainers detected your site has more JavaScript than most sites! We're working to make your site's JS compile as quickly as possible by avoiding clearing your webpack cache as often.
99-
100-
If you're interested in trialing this coming change *today* — which should make your local development experience faster — go ahead and enable the PRESERVE_WEBPACK_CACHE flag and run your develop server again.
101-
102-
To do so, add to your gatsby-config.js:
103-
104-
flags: {
105-
PRESERVE_WEBPACK_CACHE: true,
106-
}
107-
108-
Visit the umbrella issue to learn more: https://github.com/gatsbyjs/gatsby/discussions/28331
109-
`),
96+
`Preserve webpack's Cache`,
97+
`https://github.com/gatsbyjs/gatsby/discussions/28331`,
98+
`which changes Gatsby's cache clearing behavior to not clear webpack's
99+
cache unless you run "gatsby clean" or delete the .cache folder manually.
100+
Here's how to try it:
101+
102+
module.exports = {
103+
flags: { PRESERVE_WEBPACK_CACHE: true },
104+
plugins: [...]
105+
}`,
110106
THIRTY_SECONDS
111107
)
112108
}

0 commit comments

Comments
 (0)