The Unleash Svelte SDK lets you evaluate feature flags in Svelte applications. It connects to Unleash or Unleash Edge to fetch evaluated flags for a given Unleash context.
You can use this SDK with Unleash Enterprise or Unleash Open Source.
For an overview of how Unleash SDKs work, including offline behavior, feature compatibility across SDKs, and default refresh and metrics intervals, refer to the SDK overview.
To use this SDK, you need:
Import the provider like this in your entrypoint file (typically index.svelte):
Set url to the /api/frontend endpoint of your Unleash instance or your Unleash Edge instance. Set clientKey to a frontend API token.
To check if a feature is enabled:
To check variants:
useFlagsStatus retrieves the ready state and error events. Follow the following steps in order to delay rendering until the flags have been fetched.
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:
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.