Skip to content

[FSSDK-10777] SSR improvements + RSC support addition#318

Open
junaed-optimizely wants to merge 13 commits into3.x.xfrom
junaed/fssdk-10777-ssr-doc-update
Open

[FSSDK-10777] SSR improvements + RSC support addition#318
junaed-optimizely wants to merge 13 commits into3.x.xfrom
junaed/fssdk-10777-ssr-doc-update

Conversation

@junaed-optimizely
Copy link
Contributor

Summary

This pull request improves the documentation and implementation of server-side rendering (SSR) and ODP segment handling in the Optimizely React SDK V3.

It clarifies SSR usage in the README, adds a comprehensive Next.js integration guide, and updates the SDK's hooks to support more accurate decision-making during partial readiness states (such as when ODP segments are still being fetched). It also enhances test coverage for these scenarios.

Documentation improvements:

  • Expanded and clarified the SSR section in README.md to explain the need for a pre-fetched datafile, updated setup instructions, and added a summary of SSR limitations. References to Next.js and React Server Components were added, and the SSR example was simplified.
  • Added a new docs/nextjs-ssr.md guide detailing integration patterns for both Next.js App Router and Pages Router, including code examples, feature flag usage, server component usage, and SSR limitations.

SDK logic and behavior changes:

  • Updated the useExperiment, useFeature, and useDecision hooks to support synchronous decisions when getOptimizelyConfig and getUserContext are available, even if the client is not fully ready (e.g., ODP segments are still loading). This enables partial decisions during SSR or while waiting for async user/segment data. [1] [2] [3]

Testing improvements:

  • Enhanced test mocks in Experiment.spec.tsx, Feature.spec.tsx, and hooks.spec.tsx to include getOptimizelyConfig and getUserContext methods, simulating partial readiness and ODP scenarios. [1] [2] [3]
  • Added and updated tests to verify that hooks and components re-render with updated decisions once ODP segment fetching completes, ensuring correct feature flag behavior during partial readiness. [1] [2] [3]

Minor documentation and formatting fixes:

  • Cleaned up code samples and formatting in README.md for clarity and consistency. [1] [2] [3] [4] [5]

These changes make SSR and ODP segment handling more robust and developer-friendly, especially for teams using Next.js or requiring partial readiness support.

Test plan

Issues

  • FSSDK-10777

Copy link
Contributor

@raju-opti raju-opti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

few comments

@junaed-optimizely junaed-optimizely changed the title Junaed/fssdk 10777 ssr doc update [FSSDK-10777] SSR improvements Mar 2, 2026
@junaed-optimizely junaed-optimizely changed the title [FSSDK-10777] SSR improvements [FSSDK-10777] SSR improvements + RSC support addition Mar 2, 2026
Copy link
Contributor

@raju-opti raju-opti left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good. Few comments


### ODP audience segments

ODP (Optimizely Data Platform) audience segments require fetching segment data via an async network call, which is not available during server rendering. To include segment data during SSR, pass pre-fetched segments via the `qualifiedSegments` prop on `OptimizelyProvider`:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An example of how to pre-fetch the segments would be good.

@@ -1,5 +1,5 @@
/**
* Copyright 2019, Optimizely
* Copyright 2026 Optimizely
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you mean to add the year instead of replacing it?

* limitations under the License.
*/

import hoistNonReactStatics from 'hoist-non-react-statics';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any particular reason for splitting this from utils.ts?

const isClientReady = isServerSide || !!optimizely?.isReady();
const isReadyPromiseFulfilled = !!optimizely?.getIsReadyPromiseFulfilled();

const canMakeDecision = !!(optimizely?.getOptimizelyConfig() && optimizely.getUserContext());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should make sync only when qualified segements is provided. Otherwise should wait for segment fetch to keep backwards compatibility

// synchronous user context setting is required including for server side rendering (SSR)
this.setCurrentUserContext(userInfo);

if (this.userContext && !this.odpExplicitlyOff && this._client?.isOdpIntegrated() && qualifiedSegments) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we want do the same in the if block as well (after line 399)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants