Welcome to the "Advanced Usage" section of the configcat-vue
documentation. Here, we'll explore advanced ways of using the configcat-vue
plugin
Polling modes are used to control how often ConfigCat's SDK client downloads the values of your feature flags from ConfigCat's servers. The default polling mode is AutoPoll
. Auto Polling fetches your latest feature flag values every 60 seconds by default. If you need to change this, you can do so by specifying a polling mode and setting the polling interval (in seconds) in the pollingIntervalInSeconds
property.
- Import
PollingMode
fromconfigcat-vue
:
import { PollingMode } from "configcat-vue";
- Add
pollingMode
to yourapp.use
:
app.use(ConfigCatPlugin, {
sdkKey: "YOUR-CONFIGCAT-SDKKEY", // sdkKey is required
pollingMode: PollingMode.AutoPoll, // Optional. Default is AutoPoll
// ...
});
- If you want to change default interval of AutoPoll, add the number of seconds to
pollingIntervalInSeconds
inclientOptions
as follows:
app.use(ConfigCatPlugin, {
sdkKey: "YOUR-CONFIGCAT-SDKKEY", // sdkKey is required
pollingMode: PollingMode.AutoPoll, // Optional. Default is AutoPoll
clientOptions: {
pollIntervalSeconds: 5 // Optional. Specify the polling interval in seconds. Default is 60 seconds.
}
});
pollingMode can be one of the following:
-
PollingMode.AutoPoll
-
PollingMode.ManualPoll
-
PollingMode.LazyLoad
See documentation here: https://configcat.com/docs/advanced/caching/
You may want to log the actions of the underlying ConfigCat SDK client. The configcat-vue
plugin allows you to do this by specifying a logger in clientOptions
:
See documentation here: https://configcat.com/docs/sdk-reference/js/#logging
- First, add
createConsoleLogger
, andLoggerLevel
to your import:
import { createConsoleLogger, LogLevel } from "configcat-vue";
- Create the logger with a specified log level:
Documentation: https://configcat.com/docs/sdk-reference/js/#setting-log-levels
- Use the logger in
clientOptions
:
app.use(ConfigCatPlugin, {
sdkKey: "YOUR-CONFIGCAT-SDK-KEY", // // sdkKey is required
clientOptions: { // clientOptions is optional
// ...
logger: createConsoleLogger(LogLevel.Info),
}
});
The following methods are available on LogLevel:
- LogLevel.Debug - All events are logged.
- LogLevel.Info - Info, Warn and Error are logged. Debug events are discarded.
- LogLevel.Warn - Warn and Error events are logged. Info and Debug events are discarded.
- LogLevel.Error - Error events are logged. All other events are discarded.
- LogLevel.Off - No events are logged.
The User Object represent a user in your application. Specifying a User Object allows you to use ConfigCat's Targeting feature.
See documentation here: https://configcat.com/docs/advanced/user-object/
The User Object can be passed as a prop to the FeatureWrapper component.
- Define the User Object:
<script setup lang="ts">
import { reactive } from 'vue';
import { FeatureWrapper } from "configcat-vue";
const state = reactive({
userObject: {
identifier: 'john@example.com'
}
})
</script>
- Pass it to the FeatureWrapper component:
<template>
<div class="my-app">
<FeatureWrapper featureKey="YOUR-FEATURE-FLAG-KEY" :userObject="userObject">
<TheNewFeature />
</FeatureWrapper>
</div>
</template>
When you toggle your feature flag ON/OFF in the ConfigCat dashboard the FeatureWrapper component emits the updated feature flag value. You can listen and react to the changes using @flag-value-changed
like this:
<template>
<div class="my-app">
<FeatureWrapper featureKey="YOUR-FEATURE-FLAG-KEY" @flag-value-changed="handleFlagValueChange">
<TheNewFeature />
</FeatureWrapper>
</div>
</template>
<script setup lang="ts">
{/* React to the flag value changes */}
const handleFlagValueChange = (flagValue: boolean) => {
console.log('Flag value changed to: ', flagValue);
}
</script>
The underlying ConfigCat SDK client that powers the configcat-vue
plugin provides several hooks (events) that you can subscribe to if you need to get notified of its actions.
See documentation here: https://configcat.com/docs/sdk-reference/js/#hooks
The configcat-vue
plugin, exposes (provides) the underlying ConfigCat SDK client incase you need to subscribe to any of its hooks.
You can access it in your Vue.js app by injecting it into your component like this:
<script setup lang="ts">
import { inject, onBeforeMount } from 'vue';
import { FeatureWrapper } from "configcat-vue";
// ...
// Import the ConfigCat SDK client interface
import type { IConfigCatClient } from 'configcat-vue';
// Inject the underlying ConfigCat SDK client that powers the `configcat-vue` plugin
const configCatClient = inject<IConfigCatClient>('configCatClient');
onBeforeMount(() => {
// Subscribe to the hook using the .on method of the ConfigCat SDK client
configCatClient?.on('flagEvaluated', () => {
console.log('Flag evaluated');
});
});
</script>
See documentation here: https://configcat.com/docs/sdk-reference/js/#hooks