fix: global data not warming up correctly after hydration

This commit is contained in:
Zoe
2026-03-02 00:11:06 -06:00
parent 5a35976ce0
commit c84f982277
4 changed files with 26 additions and 17 deletions
+7 -4
View File
@@ -22,8 +22,8 @@ export const useAgents = () => {
initPromise: Promise<void> | null; initPromise: Promise<void> | null;
}; };
const init = () => { const init = (): Promise<void> => {
if (state.initPromise) return; if (state.initPromise) return state.initPromise;
state.initPromise = (async () => { state.initPromise = (async () => {
const query = triplit.query('agents') const query = triplit.query('agents')
@@ -32,10 +32,13 @@ export const useAgents = () => {
const { results } = await useQuery('agents', triplit, query) const { results } = await useQuery('agents', triplit, query)
watch(results, (newAgents) => { watch(results, (newAgents) => {
console.log("newAgents", newAgents); if (newAgents && newAgents.length > 0) {
state.list.value = newAgents as unknown as Agent[] || []; state.list.value = newAgents as unknown as Agent[];
}
}, { immediate: true, flush: 'sync' }); }, { immediate: true, flush: 'sync' });
})(); })();
return state.initPromise;
}; };
const getAgent = (id: MaybeRef<string>) => { const getAgent = (id: MaybeRef<string>) => {
+7 -3
View File
@@ -29,17 +29,21 @@ export const useModels = () => {
initPromise: Promise<void> | null; initPromise: Promise<void> | null;
}; };
const init = () => { const init = (): Promise<void> => {
if (state.initPromise) return; if (state.initPromise) return state.initPromise;
state.initPromise = (async () => { state.initPromise = (async () => {
const query = triplit.query('providers').Include('models'); const query = triplit.query('providers').Include('models');
const { results } = await useQuery('providers', triplit, query) const { results } = await useQuery('providers', triplit, query)
watch(results, (newProviders) => { watch(results, (newProviders) => {
state.providers.value = newProviders as unknown as ProviderWithModels[] || []; if (newProviders && newProviders.length > 0) {
state.providers.value = newProviders as unknown as ProviderWithModels[];
}
}, { immediate: true, flush: 'sync' }); }, { immediate: true, flush: 'sync' });
})(); })();
return state.initPromise;
}; };
const allModels = computed<ModelWithProvider[]>(() => { const allModels = computed<ModelWithProvider[]>(() => {
+1 -1
View File
@@ -33,7 +33,7 @@ export const useUserSettings = () => {
const state = nuxtApp._userSettingsState as UserSettingsState; const state = nuxtApp._userSettingsState as UserSettingsState;
const init = () => { const init = (): Promise<void> => {
if (state.initPromise) return state.initPromise; if (state.initPromise) return state.initPromise;
state.initPromise = (async () => { state.initPromise = (async () => {
+5 -3
View File
@@ -1,6 +1,7 @@
export default defineNuxtPlugin(async () => { export default defineNuxtPlugin({
name: 'global-data',
// Warm up the caches enforce: 'pre',
async setup() {
await Promise.all([ await Promise.all([
useAgents().init(), useAgents().init(),
useModels().init(), useModels().init(),
@@ -8,4 +9,5 @@ export default defineNuxtPlugin(async () => {
]); ]);
console.log('Global data ready'); console.log('Global data ready');
}
}); });