Skip to content

Commit ac65546

Browse files
committed
feat(nimbus): Update nimbus experimentation code
1 parent a1fbf07 commit ac65546

File tree

10 files changed

+433
-106
lines changed

10 files changed

+433
-106
lines changed

packages/fxa-content-server/server/lib/beta-settings.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,10 @@ const settingsConfig = {
123123
'featureFlags.paymentsNextSubscriptionManagement'
124124
),
125125
},
126-
nimbusPreview: config.get('nimbusPreview'),
126+
nimbus: {
127+
enabled: config.get('nimbus.enabled'),
128+
preview: config.get('nimbus.preview'),
129+
},
127130
cms: {
128131
enabled: config.get('cms.enabled'),
129132
l10nEnabled: config.get('cms.l10nEnabled'),

packages/fxa-content-server/server/lib/configuration.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -399,12 +399,6 @@ const conf = (module.exports = convict({
399399
format: String,
400400
},
401401
},
402-
nimbusPreview: {
403-
default: false,
404-
doc: 'Enables preview mode for nimbus experiments for development and testing.',
405-
format: Boolean,
406-
env: 'NIMBUS_PREVIEW',
407-
},
408402
glean: {
409403
enabled: {
410404
default: false,
@@ -705,6 +699,18 @@ const conf = (module.exports = convict({
705699
format: ['src', 'dist'],
706700
},
707701
nimbus: {
702+
enabled: {
703+
default: false,
704+
doc: 'Enables nimbus experiments',
705+
env: 'NIMBUS_ENABLED',
706+
format: Boolean,
707+
},
708+
preview: {
709+
default: true,
710+
doc: 'Enables preview mode for nimbus experiments for development and testing.',
711+
env: 'NIMBUS_PREVIEW',
712+
format: Boolean,
713+
},
708714
host: {
709715
default: 'http://localhost:8001',
710716
doc: 'Base URI for cirrus (Nimbus experimentation endpoint).',

packages/fxa-content-server/server/lib/routes/react-app/route-definition-index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ function getIndexRouteDefinition(config) {
5858
const FEATURE_FLAGS_SHOW_LOCALE_TOGGLE = config.get(
5959
'featureFlags.showLocaleToggle'
6060
);
61-
const NIMBUS_PREVIEW = config.get('nimbusPreview');
6261
const GLEAN_ENABLED = config.get('glean.enabled');
6362
const GLEAN_APPLICATION_ID = config.get('glean.applicationId');
6463
const GLEAN_UPLOAD_ENABLED = config.get('glean.uploadEnabled');
@@ -68,6 +67,8 @@ function getIndexRouteDefinition(config) {
6867
const GLEAN_DEBUG_VIEW_TAG = config.get('glean.debugViewTag');
6968
const CMS_ENABLED = config.get('cms.enabled');
7069
const CMS_L10N_ENABLED = config.get('cms.l10nEnabled');
70+
const NIMBUS_ENABLED = config.get('nimbus.enabled');
71+
const NIMBUS_PREVIEW = config.get('nimbus.preview');
7172

7273
// Rather than relay all rollout rates, hand pick the ones that are applicable
7374
const ROLLOUT_RATES = config.get('rolloutRates');
@@ -129,7 +130,10 @@ function getIndexRouteDefinition(config) {
129130
enabled: CMS_ENABLED,
130131
l10nEnabled: CMS_L10N_ENABLED,
131132
},
132-
nimbusPreview: NIMBUS_PREVIEW,
133+
nimbus: {
134+
enabled: NIMBUS_ENABLED,
135+
preview: NIMBUS_PREVIEW,
136+
},
133137
glean: {
134138
// feature toggle
135139
enabled: GLEAN_ENABLED,

packages/fxa-settings/src/index.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { render } from 'react-dom';
77
import sentryMetrics from 'fxa-shared/sentry/browser';
88
import { AppErrorBoundary } from './components/ErrorBoundaries';
99
import App from './components/App';
10+
import { NimbusProvider } from './models/contexts/NimbusContext';
1011
import config, { readConfigMeta } from './lib/config';
1112
import { searchParams } from './lib/utilities';
1213
import { AppContext, initializeAppContext } from './models';
@@ -86,9 +87,11 @@ try {
8687
>
8788
<AppErrorBoundary>
8889
<AppContext.Provider value={appContext}>
89-
<ApolloProvider client={apolloClient}>
90-
<View />
91-
</ApolloProvider>
90+
<NimbusProvider>
91+
<ApolloProvider client={apolloClient}>
92+
<View />
93+
</ApolloProvider>
94+
</NimbusProvider>
9295
</AppContext.Provider>
9396
</AppErrorBoundary>
9497
</DynamicLocalizationProvider>

packages/fxa-settings/src/lib/config.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ export interface Config {
103103
showLocaleToggle?: boolean;
104104
paymentsNextSubscriptionManagement?: boolean;
105105
};
106-
nimbusPreview: boolean;
106+
nimbus: {
107+
enabled: boolean;
108+
preview: boolean;
109+
};
107110
cms: {
108111
enabled: boolean;
109112
l10nEnabled: boolean;
@@ -199,7 +202,10 @@ export function getDefault() {
199202
enabled: false,
200203
l10nEnabled: false,
201204
},
202-
nimbusPreview: false,
205+
nimbus: {
206+
enabled: true,
207+
preview: true,
208+
},
203209
} as Config;
204210
}
205211

packages/fxa-settings/src/lib/nimbus/index.ts

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

5-
// import * as Sentry from '@sentry/browser';
5+
import * as Sentry from '@sentry/browser';
66

77
/**
88
* A collection of attributes about the client that will be used for
@@ -30,43 +30,43 @@ export interface NimbusResult {
3030
* @returns the experiment and enrollment information for that `clientId`.
3131
*/
3232
export async function initializeNimbus(
33-
_clientId: string,
34-
_previewEnabled: boolean,
35-
_context: NimbusContextT
33+
clientId: string,
34+
previewEnabled: boolean,
35+
context: NimbusContextT
3636
): Promise<NimbusResult | null> {
3737
// Disabling experiment request for now. Leaving code here for the future
3838
// when we re-enable.
3939

40-
// const body = JSON.stringify({
41-
// client_id: clientId,
42-
// context,
43-
// });
40+
const body = JSON.stringify({
41+
client_id: clientId,
42+
context,
43+
});
4444

45-
// try {
46-
// const query =
47-
// previewEnabled === true ? `?nimbusPreview=${previewEnabled}` : '';
48-
// const resp = await fetch(`/nimbus-experiments${query}`, {
49-
// method: 'POST',
50-
// body,
51-
// headers: {
52-
// 'Content-Type': 'application/json',
53-
// },
54-
// });
45+
try {
46+
const query =
47+
previewEnabled === true ? `?nimbusPreview=${previewEnabled}` : '';
48+
const resp = await fetch(`/nimbus-experiments${query}`, {
49+
method: 'POST',
50+
body,
51+
headers: {
52+
'Content-Type': 'application/json',
53+
},
54+
});
5555

56-
// if (resp.status === 200) {
57-
// return (await resp.json()) as NimbusResult;
58-
// }
59-
// } catch (err) {
60-
// // Important, if this fails it will just show up in Sentry as a
61-
// // TypeError: NetworkError when attempting to fetch resource.
62-
// // Look at the previous fetch bread crumb to understand what
63-
// // request is actually failing.
64-
// Sentry.captureException(err, {
65-
// tags: {
66-
// source: 'nimbus-experiments',
67-
// },
68-
// });
69-
// }
56+
if (resp.status === 200) {
57+
return (await resp.json()) as NimbusResult;
58+
}
59+
} catch (err) {
60+
// Important, if this fails it will just show up in Sentry as a
61+
// TypeError: NetworkError when attempting to fetch resource.
62+
// Look at the previous fetch bread crumb to understand what
63+
// request is actually failing.
64+
Sentry.captureException(err, {
65+
tags: {
66+
source: 'nimbus-experiments',
67+
},
68+
});
69+
}
7070

7171
return null;
7272
}

packages/fxa-settings/src/models/contexts/AppContext.ts

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ import { KeyStretchExperiment } from '../experiments/key-stretch-experiment';
1414
import { UrlQueryData } from '../../lib/model-data';
1515
import { ReachRouterWindow } from '../../lib/window';
1616
import { SensitiveDataClient } from '../../lib/sensitive-data-client';
17-
import { initializeNimbus, NimbusContextT } from '../../lib/nimbus';
18-
import { parseAcceptLanguage } from '../../../../../libs/shared/l10n/src';
1917
import { getUniqueUserId } from '../../lib/cache';
20-
import { searchParams } from '../../lib/utilities';
2118

2219
// TODO, move some values from AppContext to SettingsContext after
2320
// using container components, FXA-8107
@@ -29,42 +26,13 @@ export interface AppContextValue {
2926
account?: Account;
3027
session?: Session;
3128
uniqueUserId?: string; // used for experiments
32-
experiments?: Promise<any>; // external response; not adding types
3329
}
3430

3531
export interface SettingsContextValue {
3632
alertBarInfo?: AlertBarInfo;
3733
navigatorLanguages?: readonly string[];
3834
}
3935

40-
/**
41-
* Fetches nimbus experiments from the Cirrus container via content-server.
42-
*
43-
* N.B: external response; not adding types
44-
*
45-
* @param uniqueUserId the ID that is used to retrieve the experiments for that client.
46-
* @returns a promise to the fetch JSON reponse.
47-
*/
48-
function fetchNimbusExperiments(uniqueUserId: string): Promise<any> {
49-
// We reuse parseAcceptLanguage with navigator.languages because
50-
// that is the same as getting the headers directly as stated on MDN.
51-
// See: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/languages
52-
const [locale] = parseAcceptLanguage(navigator.languages.join(', '));
53-
let [language, region] = locale.split('-');
54-
if (region) {
55-
region = region.toLowerCase();
56-
}
57-
58-
const nimbusPreview = config.nimbusPreview
59-
? config.nimbusPreview
60-
: searchParams(window.location.search).nimbusPreview === 'true';
61-
62-
return initializeNimbus(uniqueUserId, nimbusPreview, {
63-
language,
64-
region,
65-
} as NimbusContextT);
66-
}
67-
6836
export function initializeAppContext() {
6937
readConfigMeta((name: string) => {
7038
return document.head.querySelector(name);
@@ -81,7 +49,6 @@ export function initializeAppContext() {
8149
const session = new Session(authClient, apolloClient);
8250
const sensitiveDataClient = new SensitiveDataClient();
8351
const uniqueUserId = getUniqueUserId();
84-
const experiments = fetchNimbusExperiments(uniqueUserId);
8552

8653
const context: AppContextValue = {
8754
authClient,
@@ -91,7 +58,6 @@ export function initializeAppContext() {
9158
session,
9259
sensitiveDataClient,
9360
uniqueUserId,
94-
experiments,
9561
};
9662

9763
return context;

0 commit comments

Comments
 (0)