DTK0027: Shared State Not Found
Package:
@vitejs/devtools
Message
Shared state of "
{key}" is not found, please provide an initial value for the first time
Cause
The RpcSharedStateHost.get() method was called with a key that does not exist in the shared state map, and neither initialValue nor sharedState was provided in the options. When accessing a shared state key for the first time, the host needs an initial value to create the state instance. Subsequent calls with the same key will return the existing state without requiring options.
Example
export default defineDevToolsPlugin({
name: 'my-plugin',
setup(context) {
// Throws DTK0027 because the key doesn't exist yet and no initial value is given
const state = await context.rpc.sharedState.get('my-plugin:config')
},
})Fix
Provide an initialValue when calling get() for a key that might not exist yet.
const state = await context.rpc.sharedState.get('my-plugin:config', {
initialValue: {
theme: 'dark',
showTimestamps: true,
},
})Alternatively, provide a pre-created sharedState instance:
import { createSharedState } from '@vitejs/devtools-kit/utils/shared-state'
const myState = createSharedState({
initialValue: { theme: 'dark' },
enablePatches: false,
})
const state = await context.rpc.sharedState.get('my-plugin:config', {
sharedState: myState,
})After the first call with an initial value, subsequent calls to the same key do not need options:
// This works because 'my-plugin:config' was already initialized above
const state = await context.rpc.sharedState.get('my-plugin:config')Source
packages/core/src/node/rpc-shared-state.ts