@grainql/analytics-web) reads
remote configuration. Grain Tag has no remote configuration.
Record<string, string>). A flag is the string 'true' or 'false',
and a number is a string that you parse. When this page and the TypeScript types of the
published package disagree, the types win.
The cache-first flow
Analytics Web never waits for the network to answer a read. One read goes through these steps.1
Answer from cache or defaults
getConfig returns the value from the cache in localStorage. In Node.js the cache is in
memory. If the cache holds no value, getConfig returns the value from
defaultConfigurations. If neither holds the key, it returns undefined.2
Fetch in the background
The read starts a background request to the API. The API applies the rules of the tenant and
returns the values for this client.
3
Update the cache and notify listeners
When the response arrives, Analytics Web writes the values to the cache and calls every
change listener with the full set of values. The next
getConfig returns the new value.defaultConfigurations, the first getConfig on a new browser returns undefined
until the first response arrives. Set a default for every key that the UI reads on first paint.
Read a value
The synchronous methods answer from the cache and defaults. Use them in a component body and in event handlers.forceRefresh: true they skip the cache and wait for the API.
fetchConfig sends one request to the API and returns the full response, or null when the
request fails. It also updates the cache.
Options
getConfigAsync, getAllConfigsAsync, and fetchConfig accept a RemoteConfigOptions object.
The response of
fetchConfig holds these fields.
Read Personalization for the
properties option in a full example.
Preload at startup
preloadConfig waits for the API one time, then every getConfig answers from the cache. Call
it after setUserId so that the API evaluates the rules for the right user. With no
immediateKeys, it loads every key.
Listen for changes
A change listener receives the full set of values each time the cache updates. The cache updates after a background fetch, after a manual refresh, and afterfetchConfig.
destroy().
In React, useConfig and useAllConfigs register the listener for you and update the
component when the value changes. Read useConfig.
Refresh and cache options
Three options ofGrainConfig control the refresh timer and the cache.
Read Configuration options for the other options.
Create a configuration in the dashboard
- Open grainql.com/dashboard.
- Open Dashboard > Remote Config.
- Create a configuration key and set its default value.
- If a segment of users must receive a different value, add a rule.
- Publish.
Rules for the client
- Set a default for every key that the UI reads before the first response.
- Compare values as strings.
'true'is a string, not a boolean. - Call
preloadConfigaftersetUserIdwhen a user-specific value must be correct on first paint. - Do not put a secret in a configuration value. With
authStrategy: 'NONE', anyone who holds the tenant alias can read every value.