Skip to content

Commit 2af637d

Browse files
[FSSDK-10777] doc improvement
1 parent 68923cf commit 2af637d

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,32 @@ function MyComponent() {
433433
</OptimizelyProvider>;
434434
```
435435

436+
### Configuring the instance for server use
437+
438+
Server-side instances are short-lived (created per request) and may not be garbage collected immediately. To avoid unnecessary background work and ensure events are dispatched before the instance is discarded, configure `createInstance` with server-appropriate options:
439+
440+
```jsx
441+
import { createInstance, OptimizelyDecideOption } from '@optimizely/react-sdk';
442+
443+
const isServer = typeof window === 'undefined';
444+
445+
const optimizelyClient = createInstance({
446+
datafile,
447+
datafileOptions: { autoUpdate: !isServer },
448+
eventBatchSize: isServer ? 1 : 10,
449+
eventMaxQueueSize: isServer ? 1 : 100,
450+
// Optional: disable decision events on server if they will be sent from the client
451+
defaultDecideOptions: isServer ? [OptimizelyDecideOption.DISABLE_DECISION_EVENT] : [],
452+
});
453+
```
454+
455+
| Option | Server value | Why |
456+
|---|---|---|
457+
| `datafileOptions.autoUpdate` | `false` | No need to poll for datafile updates on a per-request instance |
458+
| `eventBatchSize` | `1` | Flush events immediately — the instance won't live long enough for a batch to fill |
459+
| `eventMaxQueueSize` | `1` | Prevent event accumulation in a short-lived instance |
460+
| `defaultDecideOptions` | `[DISABLE_DECISION_EVENT]` | Optional — avoids duplicate decision events if the client will also fire them after hydration |
461+
436462
### React Server Components
437463

438464
The SDK can also be used directly in React Server Components without `OptimizelyProvider`. Create an instance, set the user, wait for readiness, and make decisions — all within an `async` server component:

docs/nextjs-ssr.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,21 @@ Since `OptimizelyProvider` uses React Context (a client-side feature), it must b
4747
// src/providers/OptimizelyProvider.tsx
4848
'use client';
4949

50-
import { OptimizelyProvider, createInstance } from '@optimizely/react-sdk';
50+
import { OptimizelyProvider, createInstance, OptimizelyDecideOption } from '@optimizely/react-sdk';
5151
import { ReactNode, useState } from 'react';
5252

5353
export function OptimizelyClientProvider({ children, datafile }: { children: ReactNode; datafile: object }) {
54-
const [optimizely] = useState(() => createInstance({ datafile }));
54+
const [optimizely] = useState(() => {
55+
const isServer = typeof window === 'undefined';
56+
return createInstance({
57+
datafile,
58+
datafileOptions: { autoUpdate: !isServer },
59+
eventBatchSize: isServer ? 1 : 10,
60+
eventMaxQueueSize: isServer ? 1 : 100,
61+
// Optional: disable decision events on server if they will be sent from the client
62+
defaultDecideOptions: isServer ? [OptimizelyDecideOption.DISABLE_DECISION_EVENT] : [],
63+
});
64+
});
5565
const isServerSide = typeof window === 'undefined';
5666

5767
return (
@@ -62,6 +72,8 @@ export function OptimizelyClientProvider({ children, datafile }: { children: Rea
6272
}
6373
```
6474

75+
> See [Configuring the instance for server use](../README.md#configuring-the-instance-for-server-use) in the README for an explanation of each option.
76+
6577
### 3. Wire it up in your root layout
6678

6779
```tsx

0 commit comments

Comments
 (0)