You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: add ability to bootstrap flags on server and populate clients (#475)
This pull request introduces support for bootstrapping the Reflag
browser SDK with pre-fetched flag data, enabling faster initialization
for server-side rendered applications. It also refactors context
management for improved efficiency, adds SDK lifecycle methods, and
updates documentation and tooling. The most important changes are
grouped below.
**Bootstrapping and SSR support:**
* Added support for initializing the SDK with `bootstrappedFlags`,
allowing applications to avoid the initial network request for flags and
improve performance in server-side rendering scenarios. This includes
updates to `InitOptions`, `Config`, and initialization logic in
`ReflagClient`.
[[1]](diffhunk://#diff-f52188a93c3d2bacd6ba3cbc89a0c690673861c8848fae253abcb2ea55aa7acdL193-L215)
[[2]](diffhunk://#diff-f52188a93c3d2bacd6ba3cbc89a0c690673861c8848fae253abcb2ea55aa7acdR290-R294)
[[3]](diffhunk://#diff-f52188a93c3d2bacd6ba3cbc89a0c690673861c8848fae253abcb2ea55aa7acdR408-R409)
[[4]](diffhunk://#diff-f52188a93c3d2bacd6ba3cbc89a0c690673861c8848fae253abcb2ea55aa7acdR498-R499)
[[5]](diffhunk://#diff-b1fb2a339ad0237ec791b1b15cdeeb37155b030813262be5e9f32487f1c6f988R51-R73)
* Updated the documentation (`README.md`) with clear instructions and
examples for using bootstrapped flags in SSR setups.
**Context and state management improvements:**
* Refactored context update methods (`updateUser`, `updateCompany`,
`updateOtherContext`, `updateContext`) to use deep equality checks and
avoid unnecessary updates, improving efficiency and reducing redundant
network calls.
* Added new methods to get and update context (`getContext`,
`updateContext`) and to update flags directly (`updateFlags`).
[[1]](diffhunk://#diff-f52188a93c3d2bacd6ba3cbc89a0c690673861c8848fae253abcb2ea55aa7acdR571-R577)
[[2]](diffhunk://#diff-f52188a93c3d2bacd6ba3cbc89a0c690673861c8848fae253abcb2ea55aa7acdL555-R701)
**SDK lifecycle management:**
* Introduced SDK state management with internal state tracking (`idle`,
`initializing`, `initialized`, `stopped`) and corresponding methods
(`initialize`, `stop`, `getState`).
[[1]](diffhunk://#diff-f52188a93c3d2bacd6ba3cbc89a0c690673861c8848fae253abcb2ea55aa7acdR483-R488)
[[2]](diffhunk://#diff-f52188a93c3d2bacd6ba3cbc89a0c690673861c8848fae253abcb2ea55aa7acdR511-R539)
**Tooling and dependencies:**
* Added `fast-equals` dependency for deep equality checks and updated
scripts in `package.json` for improved test and build workflows.
[[1]](diffhunk://#diff-475fbf96532457ba4b9457941110adacea0f854ef45e19448218936a2086e9dbR42)
[[2]](diffhunk://#diff-475fbf96532457ba4b9457941110adacea0f854ef45e19448218936a2086e9dbL16-R25)
* Updated `.vscode/settings.json` for consistent formatting and improved
search excludes.
[[1]](diffhunk://#diff-a5de3e5871ffcc383a2294845bd3df25d3eeff6c29ad46e3a396577c413bf357R23-R25)
[[2]](diffhunk://#diff-a5de3e5871ffcc383a2294845bd3df25d3eeff6c29ad46e3a396577c413bf357R38)
**Examples and documentation:**
* Updated example code and HTML to demonstrate bootstrapped flag usage
and improved flag visibility logic.
[[1]](diffhunk://#diff-b1fb2a339ad0237ec791b1b15cdeeb37155b030813262be5e9f32487f1c6f988R18)
[[2]](diffhunk://#diff-b1fb2a339ad0237ec791b1b15cdeeb37155b030813262be5e9f32487f1c6f988R51-R73)
[[3]](diffhunk://#diff-b1fb2a339ad0237ec791b1b15cdeeb37155b030813262be5e9f32487f1c6f988L64-R83)
[[4]](diffhunk://#diff-33e97e534680a9ce8684a75ac72a7e4a3242bc3909d119a4ea7c64b81ee38901L1-R1)
These changes collectively enhance SDK performance, developer
experience, and maintainability.
Just as `isEnabled`, accessing `config` on the object returned by `getFlags` does not automatically
228
228
generate a `check` event, contrary to the `config` property on the object returned by `getFlag`.
229
229
230
-
## Updating user/company/other context
230
+
## Server-side rendering and bootstrapping
231
+
232
+
For server-side rendered applications, you can eliminate the initial network request by bootstrapping the client with pre-fetched flag data.
233
+
234
+
### Init options bootstrapped
235
+
236
+
```typescript
237
+
typeConfiguration= {
238
+
logger:console; // by default only logs warn/error, by passing `console` you'll log everything
239
+
apiBaseUrl?:"https://front.reflag.com";
240
+
sseBaseUrl?:"https://livemessaging.bucket.co";
241
+
feedback?:undefined; // See FEEDBACK.md
242
+
enableTracking?:true; // set to `false` to stop sending track events and user/company updates to Reflag servers. Useful when you're impersonating a user
243
+
offline?:boolean; // Use the SDK in offline mode. Offline mode is useful during testing and local development
244
+
bootstrappedFlags?:FetchedFlags; // Pre-fetched flags from server-side (see Server-side rendering section)
245
+
};
246
+
```
247
+
248
+
### Using bootstrappedFlags
249
+
250
+
Use the Node SDK's `getFlagsForBootstrap()` method to pre-fetch flags server-side, then pass them to the browser client:
This eliminates loading states and improves performance by avoiding the initial flags API call.
291
+
292
+
## Context management
293
+
294
+
### Updating user/company/other context
231
295
232
296
Attributes given for the user/company/other context in the ReflagClient constructor can be updated for use in flag targeting evaluation with the `updateUser()`, `updateCompany()` and `updateOtherContext()` methods.
233
297
They return a promise which resolves once the flags have been re-evaluated follow the update of the attributes.
> [!NOTE] > `user`/`company` attributes are also stored remotely on the Reflag servers and will automatically be used to evaluate flag targeting if the page is refreshed.
246
310
311
+
### setContext()
312
+
313
+
The `setContext()` method allows you to replace the entire context (user, company, and other attributes) at once. This method is useful when you need to completely change the context, such as when a user logs in or switches between different accounts.
- Update the user and company information on Reflag servers
340
+
- Return a promise that resolves once the flags have been re-evaluated
341
+
342
+
### getContext()
343
+
344
+
The `getContext()` method returns the current context being used for flag evaluation. This is useful for debugging or when you need to inspect the current user, company, and other attributes.
0 commit comments