Svelte SDK
This Svelte SDK is designed to help you integrate with Unleash and evaluate feature flags inside your application.
You can use this client with Unleash Enterprise or Unleash Open Source.
Installation
How to Use
Initialize the Client
Depending on your needs and specific use-case, prepare one of:
And a respective frontend token (or, if you’re using the Unleash Proxy, one of your proxy’s designated client keys, previously known as proxy secrets).
Import the provider like this in your entrypoint file (typically index.svelte):
Connection Options
To connect this SDK to your Unleash instance’s front-end API, use the URL to your Unleash instance’s front-end API (<unleash-url>/api/frontend) as the url parameter. For the clientKey parameter, use a FRONTEND token generated from your Unleash instance. Refer to the API tokens documentation for more information.
To connect this SDK to the Unleash Edge, use the URL to your Unleash Edge instance as the url parameter. For the clientKey parameter, use a FRONTEND token generated from your Unleash Edge instance. Refer to the API tokens documentation for more information. Ensure that your Unleash Edge instance is correctly configured to have access to the feature toggles your FRONTEND token is requesting.
Check Feature Toggle Status
To check if a feature is enabled:
Check Variants
To check variants:
Defer Rendering Until Flags Fetched
useFlagsStatus retrieves the ready state and error events. Follow the following steps in order to delay rendering until the flags have been fetched.
Updating Context
Initial context can be specified on a FlagProvider config.context property.
<FlagProvider config={{ ...config, context: { userId: 123 }}>
This code sample shows you how to update the unleash context dynamically:
Advanced Use Cases
Deferring Client Start
By default, the Unleash client will start polling for toggles immediately when the FlagProvider component renders. You can prevent it by setting startClient prop to false. This is useful when you’d like to for example bootstrap the client and work offline.
Deferring the client start gives you more fine-grained control over when to start fetching the feature toggle configuration. This could be handy in cases where you need to get some other context data from the server before fetching toggles, for instance.
To start the client, use the client’s start method. The below snippet of pseudocode will defer polling until the end of the asyncProcess function.