Files
WeChatDataAnalysis/frontend/composables/useApiBase.js
T
Alfred Tang a79ff81be0 fix: resolve chat messages not loading due to Nuxt auto-import bug and WCDB timeout
- Move frontend/utils/ to frontend/lib/ to avoid Nuxt unimport scanner
  incorrectly extracting function parameter names (value, fallback) as
  module-level exports, which injected phantom imports that broke all
  client-side JavaScript execution
- Update all import paths across 13 files from ~/utils/ to ~/lib/
- Add timeout (5s) and negative cache (60s TTL) to WCDB realtime
  ensure_connected() to prevent open_account() from hanging indefinitely
  when the WeChat database is locked
- Reorder selectContact() to fire loadMessages() before navigateTo() so
  the message fetch starts before route navigation triggers Suspense
- Add watch: false to SSR useAsyncData calls to prevent unnecessary
  re-fetching on client-side route changes
2026-03-14 17:17:10 +08:00

41 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { normalizeApiBase, readApiBaseOverride } from '~/lib/api-settings'
// Client-side cache so that useApiBase() can be called safely outside
// the Nuxt composable context (e.g. inside async callbacks / onMounted chains).
let _clientCache = ''
export const useApiBase = () => {
if (process.client && _clientCache) return _clientCache
// useRuntimeConfig() requires the Nuxt app context, which is only
// guaranteed during synchronous setup. On the client we cache the
// result so later (context-less) calls still work.
let config
try {
config = useRuntimeConfig()
} catch {
// Context unavailable fall back to cached value or default.
return _clientCache || '/api'
}
// Default to same-origin `/api` so Nuxt devProxy / backend-mounted UI both work.
// Override priority:
// 1) Local UI setting (web + desktop)
// 2) NUXT_PUBLIC_API_BASE env/runtime config
// 3) `/api`
const override = process.client ? readApiBaseOverride() : ''
const runtime = String(config?.public?.apiBase || '').trim()
const result = normalizeApiBase(override || runtime || '/api')
if (process.client) _clientCache = result
return result
}
/**
* Call this when the user changes the API base override in settings
* so the cached value is refreshed.
*/
export const invalidateApiBaseCache = () => {
_clientCache = ''
}