Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SSR] Multiple factory error handling #180

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
1.11.0 (January 11, 2023)
- Updated the `SplitFactory` component provider to support server-side rendering (Related to issue #11 and #109). See our documentation for more details: https://help.split.io/hc/en-us/articles/360038825091-React-SDK#server-side-rendering
- Updated internal code to remove a circular dependency and avoid warning messages with tools like PNPM (Related to issue #176).

1.10.2 (December 12, 2023)
- Updated @splitsoftware/splitio package to version 10.24.1 that updates localStorage usage to clear cached feature flag definitions before initiating the synchronization process, if the cache was previously synchronized with a different SDK key (i.e., a different environment) or different Split Filter criteria, to avoid using invalid cached data when the SDK is ready from cache.

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@splitsoftware/splitio-react",
"version": "1.10.3-rc.0",
"version": "1.10.3-rc.1",
"description": "A React library to easily integrate and use Split JS SDK",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down
10 changes: 9 additions & 1 deletion src/SplitFactory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from 'react';
import { SplitComponent } from './SplitClient';
import { ISplitFactoryProps } from './types';
import { WARN_SF_CONFIG_AND_FACTORY, ERROR_SF_NO_CONFIG_AND_FACTORY } from './constants';
import { getSplitFactory, destroySplitFactory, IFactoryWithClients, getSplitClient } from './utils';
import { getSplitFactory, destroySplitFactory, IFactoryWithClients, getSplitClient, isServerEnvironment } from './utils';
import { DEFAULT_UPDATE_OPTIONS } from './useSplitClient';

/**
Expand All @@ -30,6 +30,14 @@ export class SplitFactory extends React.Component<ISplitFactoryProps, { factory:

let { factory, config } = props;

// At the moment, SSR is supported only with the SDK running in the client. So, it cannot create or use a factory in server-side, to avoid instantiating a factory per request/session.
if (isServerEnvironment) {
// @TODO Remove when SSR is supported with the SDK running in the server (initialState prop)
factory = undefined;
// @TODO Remove when SplitFactory component creates the factory in the commit phase (breaking change)
config = undefined;
}

// Instantiate factory
if (!factory && config) {
// We use an idempotent variant of the Split factory builder (i.e., given the same config, it returns the same already
Expand Down
11 changes: 9 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,19 @@ export interface ISplitStatus {
export interface ISplitContextValues extends ISplitStatus {

/**
* Split factory instance
* Split factory instance.
*
* NOTE: This property is not recommended for direct use, as better alternatives are available.
* It should not be used for server-side rendering, since it is null on the server and might cause a hydration mismatch.
*/
factory: SplitIO.IBrowserSDK | null;

/**
* Split client instance
* Split client instance.
*
* Caveat: This property is not recommended for direct use, as better alternatives are available.
* It should not be used for server-side rendering, since it is null on the server and might cause a hydration mismatch.
*
* @see {@link https://help.split.io/hc/en-us/articles/360020448791-JavaScript-SDK#2-instantiate-the-sdk-and-create-a-new-split-client}
*/
client: SplitIO.IBrowserClient | null;
Expand Down
11 changes: 11 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,14 @@ function evaluateFeatureFlags(client: SplitIO.IBrowserClient | null, _lastUpdate
getControlTreatmentsWithConfig(names) :
{} // empty object when evaluating with flag sets and client is not ready
}

// Utils based on https://github.com/alex-cory/use-ssr and https://github.com/facebook/react/blob/main/packages/shared/ExecutionEnvironment.js
const canUseDOM: boolean = !!(
typeof window !== 'undefined' &&
window.document &&
window.document.createElement
)

const canUseNative: boolean = typeof navigator != 'undefined' && navigator.product == 'ReactNative'

export const isServerEnvironment = !canUseDOM && !canUseNative