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
Copy file name to clipboardExpand all lines: README.md
+47-73Lines changed: 47 additions & 73 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,6 @@ Refer to the [React SDK's developer documentation](https://docs.developers.optim
12
12
13
13
For React Native, review the [React Native developer documentation](https://docs.developers.optimizely.com/feature-experimentation/docs/javascript-react-native-sdk).
14
14
15
-
16
15
### Features
17
16
18
17
- Automatic datafile downloading
@@ -28,11 +27,7 @@ The React SDK is compatible with `React 16.8.0 +`
@@ -411,69 +408,46 @@ To rollout or experiment on a feature by user rather than by random percentage,
411
408
412
409
## Server Side Rendering
413
410
414
-
Right now server side rendering is possible with a few caveats.
411
+
The React SDK supports server-side rendering (SSR). To generate synchronous decisions during SSR, you must pre-fetch the datafile and pass it to `createInstance`. Using `sdkKey` alone is not supported for SSR because it requires an asynchronous network call.
415
412
416
-
**Caveats**
413
+
### Setup
417
414
418
-
1. You must download the datafile manually and pass in via the `datafile` option. Can not use `sdkKey` to automatically download.
415
+
Fetch the datafile on the server, create an Optimizely instance, and wrap your app with `<OptimizelyProvider>`:
419
416
420
-
2. Rendering of components must be completely synchronous (this is true for all server side rendering), thus the Optimizely SDK assumes that the optimizely client has been instantiated and fired it's `onReady` event already.
// Pre-fetched datafile (fetching mechanism depends on your framework)
421
+
constoptimizelyClient=createInstance({
422
+
datafile, // must be provided for SSR
423
+
});
423
424
424
-
Similar to browser side rendering you will need to wrap your app (or portion of the app using Optimizely) in the `<OptimizelyProvider>` component. A new prop
All other Optimizely components, such as `<OptimizelyFeature>` and `<OptimizelyExperiment>` can remain the same.
436
+
### React Server Components
434
437
435
-
### Full example
438
+
The SDK can also be used directly in React Server Components without `OptimizelyProvider`. See the [Next.js Integration Guide](docs/nextjs-ssr.md#react-server-components) for details.
436
439
437
-
```jsx
438
-
import*asReactfrom'react';
439
-
import*asReactDOMServerfrom'react-dom/server';
440
+
### Next.js Integration
440
441
441
-
import {
442
-
createInstance,
443
-
OptimizelyProvider,
444
-
useDecision,
445
-
} from'@optimizely/react-sdk';
442
+
For detailed Next.js examples covering both App Router and Pages Router patterns, see the [Next.js Integration Guide](docs/nextjs-ssr.md).
446
443
447
-
constfetch=require('node-fetch');
444
+
### Limitations
448
445
449
-
functionMyComponent() {
450
-
const [decision] =useDecision('flag1');
451
-
return (
452
-
<React.Fragment>
453
-
{ decision.enabled&&<p>The feature is enabled</p> }
454
-
{ !decision.enabled&&<p>The feature is not enabled</p> }
0 commit comments