diff --git a/CHANGES.txt b/CHANGES.txt index 3ece218..941d295 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,7 +1,16 @@ -1.11.0 (January 16, 2023) - - Added the new `SplitFactoryProvider` component as a replacement for the now deprecated `SplitFactory` component. - The new component is a revised version of `SplitFactory`, addressing improper handling of the SDK initialization side-effects in the `componentDidMount` and `componentDidUpdate` methods (commit phase), causing some issues like memory leaks and the SDK not reinitializing when component props change (Related to issue #11 and #148). - The `SplitFactoryProvider` component can be used as a drop-in replacement for `SplitFactory`. It utilizes the React Hooks API, that requires React 16.8.0 or later, and supports server-side rendering. See our documentation for more details (Related to issue #11 and #109). +1.11.1 (March 26, 2024) + - Bugfixing - Added tslib as an explicit dependency to avoid issues with some package managers that don't resolve it automatically as a transitive dependency from @splitsoftware/splitio-commons (Related to issue https://github.com/splitio/javascript-client/issues/795). + +1.11.0 (January 16, 2024) + - Added new `SplitFactoryProvider` component as a replacement for the now deprecated `SplitFactory` component. + - Bugfixing: The new component is a revised version of `SplitFactory` that properly handles SDK side effects (i.e., factory creation and destruction) within the React component lifecycle, + - resolving memory leak issues in React development mode, strict mode and server-side rendering (Related to issues #11 and #109), + - and also ensuring that the SDK is updated if `config` or `factory` props change (Related to issues #11 and #148). + - Notable changes when migrating from `SplitFactory` to `SplitFactoryProvider`: + - `SplitFactoryProvider` utilizes the React Hooks API, requiring React 16.8.0 or later, while `SplitFactory` is compatible with React 16.3.0 or later. + - When using the `config` prop with `SplitFactoryProvider`, the `factory` and `client` properties in `SplitContext` and the `manager` property in `useSplitManager` results are `null` in the first render, until the context is updated when some event is emitted on the SDK main client (ready, ready from cache, timeout, or update, depending on the configuration of the `updateOn` props of the component). This differs from the previous behavior where `factory`, `client`, and `manager` were immediately available. + - Updating the `config` prop in `SplitFactoryProvider` reinitializes the SDK with the new configuration, while `SplitFactory` does not reinitialize the SDK. You should pass a reference to the configuration object (e.g., via a global variable, `useState`, or `useMemo`) rather than a new instance on each render, to avoid unnecessary reinitializations. + - Updating the `factory` prop in `SplitFactoryProvider` replaces the current SDK instance, unlike `SplitFactory` where it is ignored. - Updated internal code to remove a circular dependency and avoid warning messages with tools like PNPM (Related to issue #176). - Updated @splitsoftware/splitio package to version 10.25.1 for vulnerability fixes. diff --git a/MIGRATION-GUIDE.md b/MIGRATION-GUIDE.md new file mode 100644 index 0000000..7cb805f --- /dev/null +++ b/MIGRATION-GUIDE.md @@ -0,0 +1,142 @@ + +# Migrating to get React SDK v1.11.0 improvements: Replacing the deprecated `SplitFactory` and `withSplitFactory` components + +Starting from React SDK v1.11.0, the `SplitFactoryProvider` component is available and can replace the older `SplitFactory` and `withSplitFactory` components. The deprecated components will continue working, until they are removed in a future major release. + +We recommend migrating to the new `SplitFactoryProvider` component instead. This component is a revised version of `SplitFactory` that properly handles SDK side effects (i.e., factory creation and destruction) within the React component lifecycle. By migrating, you can benefit from a number of improvements: + + - Resolution of memory leak issues in React development mode, strict mode, and server-side rendering. + + - Updating the SDK when `config` or `factory` props change. + +Notable changes to consider when migrating: + - `SplitFactoryProvider` utilizes the React Hooks API, requiring React 16.8.0 or later, while `SplitFactory` is compatible with React 16.3.0 or later. + + - When using the `config` prop with `SplitFactoryProvider`, the `factory` and `client` properties in `SplitContext` and the `manager` property in `useSplitManager` results are `null` in the first render, until the context is updated when some event is emitted on the SDK main client (ready, ready from cache, timeout, or update, depending on the configuration of the `updateOn` props of the component). This differs from the previous behavior where `factory`, `client`, and `manager` were immediately available. Nonetheless, it is not recommended to use the `client` and `factory` properties directly as better alternatives are available. For example, use the `useTrack` and `useSplitTreatments` hooks rather than the client's `track` and `getTreatments` methods. + + - Updating the `config` prop in `SplitFactoryProvider` reinitializes the SDK with the new configuration, while `SplitFactory` does not reinitialize the SDK. You should pass a reference to the configuration object (e.g., via a global variable, `useState`, or `useMemo`) rather than a new instance on each render, to avoid unnecessary reinitializations. + + - Updating the `factory` prop in `SplitFactoryProvider` replaces the current SDK instance, unlike `SplitFactory` where it is ignored. + +To migrate your existing code, replace: + +```javascript +const MyApp = () => { + return ( + + + + ); +}; +``` + +or + +```javascript +const MyApp = withSplitFactory(mySplitConfig)(MyComponent); +``` + +with: + +```javascript +const MyApp = () => { + return ( + + + + ); +}; +``` + +and consider that `factory`, `client` and `manager` properties might be `null` until the SDK has emitted some event: + +```javascript +const MyComponent = () => { + // factoryFromContext === factory, clientFromContext === client, and they are null until some SDK event is emitted + const { factory: factoryFromContext, client: clientFromContext } = useContext(SplitContext); + const { factory, client } = useSplitClient(); + + // Example to evaluate all your flags when the SDK is ready and re-evaluate on SDK_UPDATE events + const { manager } = useSplitManager(); + const FEATURE_FLAG_NAMES = manager ? manager.names() : []; + const { treatments, isReady } = useSplitTreatments({ names: FEATURE_FLAG_NAMES, updateOnSdkUpdate: true }); // updateOnSdkReady is true by default + + return isReady ? + treatments['feature-flag-1'].treatment === 'on' ? + : + : + +} +``` + +# Migrating to get React SDK v1.10.0 improvements: Replacing the deprecated `useClient`, `useTreatments`, and `useManager` hooks + +Starting from React SDK v1.10.0, the `useSplitClient`, `useSplitTreatments`, and `useSplitManager` hooks are available and can replace the older `useClient`, `useTreatments`, and `useManager` hooks respectively. The deprecated hooks will continue working, until they are removed in a future major release. + +We recommend migrating to the new versions `useSplitClient`, `useSplitTreatments` and `useSplitManager` respectively, which provide a more flexible API: + +- They accept an options object as parameter, instead of a list of parameters as their deprecated counterparts. The options object can contain the same parameters as the old hooks, plus some extra optional parameters: `updateOnSdkReady`, `updateOnSdkReadyFromCache`, `updateOnSdkTimedout`, and `updateOnSdkUpdate`, which control when the hook updates the component. For example, you can set `updateOnSdkUpdate` to `true`, which is `false` by default, to update the component when an `SDK_UPDATE` event is emitted. This is useful when you want to avoid unnecessary re-renders of your components. + +- They return an object containing the SDK status properties. These properties are described in the ['Subscribe to events and changes' section](https://help.split.io/hc/en-us/articles/360038825091-React-SDK#subscribe-to-events-and-changes) and enable conditional rendering of components based on the SDK status, eliminating the need to access the Split context or use the client's `ready` promise or event listeners. For example, you can show a loading spinner until the SDK is ready, and use the `treatments` result to render the variants of your app once the SDK is ready. + +The usage of the new hooks is shown below: + +```js +const { client, isReady, isReadyFromCache, hasTimedout, lastUpdate } = useSplitClient({ splitKey: userId, updateOnSdkUpdate: true }); +const { treatments, isReady, isReadyFromCache, hasTimedout, lastUpdate } = useSplitTreatments({ names: ['feature-flag-1'], updateOnSdkTimedout: false }); +const { manager, isReady, isReadyFromCache, hasTimedout, lastUpdate } = useSplitManager(); +``` + + + +To migrate your existing code, replace: + +```javascript +const client = useClient(optionalSplitKey, optionalTrafficType, optionalAttributes); +const treatments = useTreatments(featureFlagNames, optionalAttributes, optionalSplitKey); +const manager = useManager(); +``` + +with: + +```javascript +const { client } = useSplitClient({ splitKey: optionalSplitKey, trafficType: optionalTrafficType, attributes: optionalAttributes }); +const { treatments } = useSplitTreatments({ names: featureFlagNames, attributes: optionalAttributes, splitKey: optionalSplitKey }); +const { manager } = useSplitManager(); +``` + +and use the status properties to conditionally render your components. For example, use the following code: + +```javascript +const MyComponent = ({ userId }) => { + + const { treatments, isReady } = useSplitTreatments({ names: [FEATURE_X], splitKey: userId }) + + return isReady ? + treatments[FEATURE_X].treatment === 'on' ? + : + : + +} +``` + +instead of: + +```javascript +const MyComponent = ({ userId }) => { + + const [sdkIsReady, setSdkIsReady] = useState(false); + const client = useClient(userId); + const treatments = useTreatments([FEATURE_X], undefined, userId); + + useEffect(() => { + if (client) client.ready().then(() => setSdkIsReady(true)); + }, [client]); + + return isReady ? + treatments[FEATURE_X].treatment === 'on' ? + : + : + +} +``` diff --git a/README.md b/README.md index 53c4b7f..3937848 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,7 @@ Split has built and maintains SDKs for: * .NET [Github](https://github.com/splitio/dotnet-client) [Docs](https://help.split.io/hc/en-us/articles/360020240172--NET-SDK) * Android [Github](https://github.com/splitio/android-client) [Docs](https://help.split.io/hc/en-us/articles/360020343291-Android-SDK) * Angular [Github](https://github.com/splitio/angular-sdk-plugin) [Docs](https://help.split.io/hc/en-us/articles/6495326064397-Angular-utilities) +* Flutter [Github](https://github.com/splitio/flutter-sdk-plugin) [Docs](https://help.split.io/hc/en-us/articles/8096158017165-Flutter-plugin) * GO [Github](https://github.com/splitio/go-client) [Docs](https://help.split.io/hc/en-us/articles/360020093652-Go-SDK) * iOS [Github](https://github.com/splitio/ios-client) [Docs](https://help.split.io/hc/en-us/articles/360020401491-iOS-SDK) * Java [Github](https://github.com/splitio/java-client) [Docs](https://help.split.io/hc/en-us/articles/360020405151-Java-SDK) @@ -86,6 +87,7 @@ Split has built and maintains SDKs for: * JavaScript for Browser [Github](https://github.com/splitio/javascript-browser-client) [Docs](https://help.split.io/hc/en-us/articles/360058730852-Browser-SDK) * Node [Github](https://github.com/splitio/javascript-client) [Docs](https://help.split.io/hc/en-us/articles/360020564931-Node-js-SDK) * PHP [Github](https://github.com/splitio/php-client) [Docs](https://help.split.io/hc/en-us/articles/360020350372-PHP-SDK) +* PHP thin-client [Github](https://github.com/splitio/php-thin-client) [Docs](https://help.split.io/hc/en-us/articles/18305128673933-PHP-Thin-Client-SDK) * Python [Github](https://github.com/splitio/python-client) [Docs](https://help.split.io/hc/en-us/articles/360020359652-Python-SDK) * React [Github](https://github.com/splitio/react-client) [Docs](https://help.split.io/hc/en-us/articles/360038825091-React-SDK) * React Native [Github](https://github.com/splitio/react-native-client) [Docs](https://help.split.io/hc/en-us/articles/4406066357901-React-Native-SDK) diff --git a/package-lock.json b/package-lock.json index 377cab9..c39002d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,17 +1,18 @@ { "name": "@splitsoftware/splitio-react", - "version": "1.11.0", + "version": "1.11.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@splitsoftware/splitio-react", - "version": "1.11.0", + "version": "1.11.1", "license": "Apache-2.0", "dependencies": { - "@splitsoftware/splitio": "10.25.1", + "@splitsoftware/splitio": "10.25.2", "memoize-one": "^5.1.1", - "shallowequal": "^1.1.0" + "shallowequal": "^1.1.0", + "tslib": "^2.3.1" }, "devDependencies": { "@testing-library/jest-dom": "^5.16.5", @@ -1547,9 +1548,9 @@ } }, "node_modules/@splitsoftware/splitio": { - "version": "10.25.1", - "resolved": "https://registry.npmjs.org/@splitsoftware/splitio/-/splitio-10.25.1.tgz", - "integrity": "sha512-QmCOI2VNhjIMieibtMcc595oyQB5sgrYKSqw7wxKtwMX0VtuPbZ3Lw8fwd0nH2WSKq3Pcjyu3nVSYQRp1bGEvA==", + "version": "10.25.2", + "resolved": "https://registry.npmjs.org/@splitsoftware/splitio/-/splitio-10.25.2.tgz", + "integrity": "sha512-lX06PSoA+hcw66c889RxK9t9cd8evguBFk+x1pw2L4J/58+7XHxp/z1Nnbtb3AOw8s/sX4h87qlTiZqMGPmcmA==", "dependencies": { "@splitsoftware/splitio-commons": "1.13.1", "@types/google.analytics": "0.0.40", @@ -1558,6 +1559,7 @@ "ioredis": "^4.28.0", "js-yaml": "^3.13.1", "node-fetch": "^2.7.0", + "tslib": "^2.3.1", "unfetch": "^4.2.0" }, "engines": { @@ -12089,9 +12091,9 @@ } }, "@splitsoftware/splitio": { - "version": "10.25.1", - "resolved": "https://registry.npmjs.org/@splitsoftware/splitio/-/splitio-10.25.1.tgz", - "integrity": "sha512-QmCOI2VNhjIMieibtMcc595oyQB5sgrYKSqw7wxKtwMX0VtuPbZ3Lw8fwd0nH2WSKq3Pcjyu3nVSYQRp1bGEvA==", + "version": "10.25.2", + "resolved": "https://registry.npmjs.org/@splitsoftware/splitio/-/splitio-10.25.2.tgz", + "integrity": "sha512-lX06PSoA+hcw66c889RxK9t9cd8evguBFk+x1pw2L4J/58+7XHxp/z1Nnbtb3AOw8s/sX4h87qlTiZqMGPmcmA==", "requires": { "@splitsoftware/splitio-commons": "1.13.1", "@types/google.analytics": "0.0.40", @@ -12101,6 +12103,7 @@ "ioredis": "^4.28.0", "js-yaml": "^3.13.1", "node-fetch": "^2.7.0", + "tslib": "^2.3.1", "unfetch": "^4.2.0" } }, diff --git a/package.json b/package.json index 9085bdd..a8dcb9c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@splitsoftware/splitio-react", - "version": "1.11.0", + "version": "1.11.1", "description": "A React library to easily integrate and use Split JS SDK", "main": "lib/index.js", "module": "es/index.js", @@ -8,6 +8,7 @@ "files": [ "README.md", "CONTRIBUTORS-GUIDE.md", + "MIGRATION-GUIDE.md", "LICENSE", "CHANGES.txt", "src", @@ -62,9 +63,10 @@ }, "homepage": "https://github.com/splitio/react-client#readme", "dependencies": { - "@splitsoftware/splitio": "10.25.1", + "@splitsoftware/splitio": "10.25.2", "memoize-one": "^5.1.1", - "shallowequal": "^1.1.0" + "shallowequal": "^1.1.0", + "tslib": "^2.3.1" }, "devDependencies": { "@testing-library/jest-dom": "^5.16.5", diff --git a/src/SplitFactory.tsx b/src/SplitFactory.tsx index 74d9236..1636100 100644 --- a/src/SplitFactory.tsx +++ b/src/SplitFactory.tsx @@ -14,10 +14,19 @@ import { DEFAULT_UPDATE_OPTIONS } from './useSplitClient'; * The underlying SDK factory and client is set on the constructor, and cannot be changed during the component lifecycle, * even if the component is updated with a different config or factory prop. * - * @deprecated Replace with the new `SplitFactoryProvider` component. - * `SplitFactoryProvider` is a drop-in replacement that properly handles side effects (factory creation and destruction) within the React component lifecycle, avoiding issues with factory recreation and memory leaks. - * Note: There is a subtle breaking change in `SplitFactoryProvider`. When using the `config` prop, `factory` and `client` properties in the context are `null` in the first render, until the context is updated when some event is emitted on - * the SDK main client (ready, ready from cache, timeout or update depending on the configuration of the `updateOnXXX` props of the component). This differs from the previous behavior where `factory` and `client` were immediately available. + * @deprecated `SplitFactory` will be removed in a future major release. We recommend replacing it with the new `SplitFactoryProvider` component. + * + * `SplitFactoryProvider` is a revised version of `SplitFactory` that properly handles SDK side effects (i.e., factory creation and destruction) within the React component lifecycle, + * resolving memory leak issues in React development mode, strict mode and server-side rendering, and also ensuring that the SDK is updated if `config` or `factory` props change. + * + * Notable changes to consider when migrating: + * - `SplitFactoryProvider` utilizes the React Hooks API, requiring React 16.8.0 or later, while `SplitFactory` is compatible with React 16.3.0 or later. + * - When using the `config` prop with `SplitFactoryProvider`, the `factory` and `client` properties in `SplitContext` and the `manager` property in `useSplitManager` results + * are `null` in the first render, until the context is updated when some event is emitted on the SDK main client (ready, ready from cache, timeout, or update, depending on + * the configuration of the `updateOn` props of the component). This differs from the previous behavior where `factory`, `client`, and `manager` were immediately available. + * - Updating the `config` prop in `SplitFactoryProvider` reinitializes the SDK with the new configuration, while `SplitFactory` does not reinitialize the SDK. You should pass a + * reference to the configuration object (e.g., via a global variable, `useState`, or `useMemo`) rather than a new instance on each render, to avoid unnecessary reinitializations. + * - Updating the `factory` prop in `SplitFactoryProvider` replaces the current SDK instance, unlike `SplitFactory` where it is ignored. * * @see {@link https://help.split.io/hc/en-us/articles/360038825091-React-SDK#2-instantiate-the-sdk-and-create-a-new-split-client} */ diff --git a/src/SplitFactoryProvider.tsx b/src/SplitFactoryProvider.tsx index 5f91df7..5b55b8b 100644 --- a/src/SplitFactoryProvider.tsx +++ b/src/SplitFactoryProvider.tsx @@ -3,7 +3,7 @@ import React from 'react'; import { SplitComponent } from './SplitClient'; import { ISplitFactoryProps } from './types'; import { WARN_SF_CONFIG_AND_FACTORY } from './constants'; -import { getSplitFactory, destroySplitFactory, IFactoryWithClients, getSplitClient, getStatus, __factories } from './utils'; +import { getSplitFactory, destroySplitFactory, IFactoryWithClients, getSplitClient, getStatus } from './utils'; import { DEFAULT_UPDATE_OPTIONS } from './useSplitClient'; /** @@ -11,8 +11,8 @@ import { DEFAULT_UPDATE_OPTIONS } from './useSplitClient'; * and automatically destroy the SDK (shutdown and release resources) when it is unmounted or `config` prop updated. SplitFactoryProvider must wrap other library components and * functions since they access the Split Context and its properties (factory, client, isReady, etc). * - * NOTE: Either pass a factory instance or a config object. If both are passed, the config object will be ignored. - * Pass the same reference to the config or factory object rather than a new instance on each render, to avoid unnecessary props changes and SDK reinitializations. + * NOTE: Either pass a `factory` instance or a `config` object as props. If both props are passed, the `config` prop will be ignored. + * Pass the same reference to the `config` or `factory` object rather than a new instance on each render, to avoid unnecessary props changes and SDK reinitializations. * * @see {@link https://help.split.io/hc/en-us/articles/360038825091-React-SDK#2-instantiate-the-sdk-and-create-a-new-split-client} */ @@ -44,7 +44,7 @@ export function SplitFactoryProvider(props: ISplitFactoryProps) { // Effect to subscribe/unsubscribe to events React.useEffect(() => { - const factory = config && __factories.get(config); + const factory = config && getSplitFactory(config); if (factory) { const client = getSplitClient(factory); const status = getStatus(client); diff --git a/src/__tests__/testUtils/utils.tsx b/src/__tests__/testUtils/utils.tsx index 0d02364..443f46e 100644 --- a/src/__tests__/testUtils/utils.tsx +++ b/src/__tests__/testUtils/utils.tsx @@ -110,7 +110,7 @@ export function testAttributesBinding(Component: React.FunctionComponent); - wrapper.rerender(); - wrapper.rerender(); - wrapper.rerender(); + wrapper.rerender(); + wrapper.rerender(); + wrapper.rerender(); } diff --git a/src/withSplitFactory.tsx b/src/withSplitFactory.tsx index 45eb55f..fd83a0e 100644 --- a/src/withSplitFactory.tsx +++ b/src/withSplitFactory.tsx @@ -10,10 +10,19 @@ import { SplitFactory } from './SplitFactory'; * @param config Config object used to instantiate a Split factory * @param factory Split factory instance to use instead of creating a new one with the config object. * - * @deprecated Use `SplitFactoryProvider` instead. - * `SplitFactoryProvider` is a drop-in replacement of `SplitFactory` that properly handles side effects (factory creation and destruction) within the React component lifecycle, avoiding issues with factory recreation and memory leaks. - * Note: There is a subtle breaking change in `SplitFactoryProvider`. When using the `config` prop, `factory` and `client` properties in the context are `null` in the first render, until the context is updated when some event is emitted on - * the SDK main client (ready, ready from cache, timeout or update depending on the configuration of the `updateOnXXX` props of the component). This differs from the previous behavior where `factory` and `client` were immediately available. + * @deprecated `withSplitFactory` will be removed in a future major release. We recommend replacing it with the new `SplitFactoryProvider` component. + * + * `SplitFactoryProvider` is a revised version of `SplitFactory` that properly handles SDK side effects (i.e., factory creation and destruction) within the React component lifecycle, + * resolving memory leak issues in React development mode, strict mode and server-side rendering, and also ensuring that the SDK is updated if `config` or `factory` props change. + * + * Notable changes to consider when migrating: + * - `SplitFactoryProvider` utilizes the React Hooks API, requiring React 16.8.0 or later, while `SplitFactory` is compatible with React 16.3.0 or later. + * - When using the `config` prop with `SplitFactoryProvider`, the `factory` and `client` properties in `SplitContext` and the `manager` property in `useSplitManager` results + * are `null` in the first render, until the context is updated when some event is emitted on the SDK main client (ready, ready from cache, timeout, or update, depending on + * the configuration of the `updateOn` props of the component). This differs from the previous behavior where `factory`, `client`, and `manager` were immediately available. + * - Updating the `config` prop in `SplitFactoryProvider` reinitializes the SDK with the new configuration, while `SplitFactory` does not reinitialize the SDK. You should pass a + * reference to the configuration object (e.g., via a global variable, `useState`, or `useMemo`) rather than a new instance on each render, to avoid unnecessary reinitializations. + * - Updating the `factory` prop in `SplitFactoryProvider` replaces the current SDK instance, unlike `SplitFactory` where it is ignored. */ export function withSplitFactory(config?: SplitIO.IBrowserSettings, factory?: SplitIO.IBrowserSDK, attributes?: SplitIO.Attributes) {