fix(fe2): nuxt state being missing in some cases (#5287)

This commit is contained in:
Kristaps Fabians Geikins
2025-08-21 13:11:54 +03:00
committed by GitHub
parent fa4784c2d4
commit 8c745ad853
7 changed files with 41 additions and 9 deletions
@@ -44,6 +44,38 @@ export const useStrictLogger = async (
return logger return logger
} }
/**
* Use when you need to be sure that the real structured pino logger is available
* (it isn't in some early startup contexts like apollo link setup)
*
* The async version is better in that it will build a real pino logger, but in sync contexts
* you can use this one that will at least fallback to console.log/warn/error
*/
export const useStrictLoggerSync = (
options?: Partial<{ dontNotifyFallback: boolean }>
) => {
const { dontNotifyFallback } = options || {}
let nuxtApp: Optional<NuxtApp> = undefined
try {
nuxtApp = useNuxtApp()
} catch {
// suppress 'nuxt is not available'
}
if (nuxtApp?.$logger) return nuxtApp?.$logger
// Nuxt app not found in this scope
const err = new Error(
'Nuxt app for logger not found! Initializing fallback structured logger...'
)
const logger = buildFakePinoLogger()
if (!dontNotifyFallback) logger.error(err)
return logger
}
/** /**
* Short-cut to useLogger().debug, useful when you quickly want to console.log something during development. * Short-cut to useLogger().debug, useful when you quickly want to console.log something during development.
* Calls to this are skipped outside of dev mode. * Calls to this are skipped outside of dev mode.
@@ -140,7 +140,7 @@ const useResetAuthState = (
const resolveDistinctId = useResolveUserDistinctId() const resolveDistinctId = useResolveUserDistinctId()
const { cbs } = useOnAuthStateChangeState() const { cbs } = useOnAuthStateChangeState()
const authToken = useAuthCookie() const authToken = useAuthCookie()
const logger = useLogger() const logger = useStrictLoggerSync()
return async ( return async (
resetOptions?: Partial<{ resetOptions?: Partial<{
@@ -229,7 +229,7 @@ export const useAuthManager = (
const getMixpanel = useDeferredMixpanel() const getMixpanel = useDeferredMixpanel()
const postAuthRedirect = usePostAuthRedirect() const postAuthRedirect = usePostAuthRedirect()
const { markLoggedOut } = useJustLoggedOutTracking() const { markLoggedOut } = useJustLoggedOutTracking()
const logger = useLogger() const logger = useStrictLoggerSync()
/** /**
* Invite token, if any * Invite token, if any
@@ -13,7 +13,7 @@ export type { AsyncWritableComputedOptions, AsyncWritableComputedRef }
* @param params * @param params
*/ */
export const writableAsyncComputed: typeof originalWritableAsyncComputed = (params) => { export const writableAsyncComputed: typeof originalWritableAsyncComputed = (params) => {
const logger = useLogger() const logger = useStrictLoggerSync()
return originalWritableAsyncComputed({ return originalWritableAsyncComputed({
...params, ...params,
debugging: params.debugging?.log debugging: params.debugging?.log
@@ -34,7 +34,7 @@ export const writableAsyncComputed: typeof originalWritableAsyncComputed = (para
*/ */
export const watchAsync = ((...args: Parameters<typeof watch>) => { export const watchAsync = ((...args: Parameters<typeof watch>) => {
const [source, cb, options] = args const [source, cb, options] = args
const logger = useLogger() const logger = useStrictLoggerSync()
const watches = shallowRef<Array<Promise<unknown>>>([]) const watches = shallowRef<Array<Promise<unknown>>>([])
@@ -11,7 +11,7 @@ export function wrapRefWithTracking<R extends Ref<unknown>>(
): R { ): R {
const { writesOnly, readsOnly } = options || {} const { writesOnly, readsOnly } = options || {}
const getTrace = () => (new Error('Trace:').stack || '').substring(7) const getTrace = () => (new Error('Trace:').stack || '').substring(7)
const logger = useLogger() const logger = useStrictLoggerSync()
return computed({ return computed({
get: () => { get: () => {
@@ -195,7 +195,7 @@ export function updateCacheByFilter<TData, TVariables = unknown>(
): boolean { ): boolean {
const { fragment, query } = filter const { fragment, query } = filter
const { ignoreCacheErrors = true, overwrite = true } = options const { ignoreCacheErrors = true, overwrite = true } = options
const logger = useLogger() const logger = useStrictLoggerSync()
if (!fragment && !query) { if (!fragment && !query) {
throw new Error( throw new Error(
@@ -382,7 +382,7 @@ export function modifyObjectFields<
) { ) {
const { fieldNameWhitelist, debug = false } = options || {} const { fieldNameWhitelist, debug = false } = options || {}
const logger = useLogger() const logger = useStrictLoggerSync()
const invocationId = nanoid() const invocationId = nanoid()
const log = (...args: Parameters<typeof logger.debug>) => { const log = (...args: Parameters<typeof logger.debug>) => {
if (!debug) return if (!debug) return
@@ -203,7 +203,7 @@ export const doesRouteFitTarget = (fullPathA: string, fullPathB: string) => {
urlA = new URL(fullPathA, fakeOrigin) urlA = new URL(fullPathA, fakeOrigin)
urlB = new URL(fullPathB, fakeOrigin) urlB = new URL(fullPathB, fakeOrigin)
} catch (e) { } catch (e) {
useLogger().warn('Failed to parse URLs', e) useStrictLoggerSync().warn('Failed to parse URLs', e)
return false return false
} }
@@ -37,7 +37,7 @@ export function isEmbedOptions(obj: unknown): obj is EmbedOptions {
} }
export function deserializeEmbedOptions(embedString: string | null): EmbedOptions { export function deserializeEmbedOptions(embedString: string | null): EmbedOptions {
const logger = useLogger() const logger = useStrictLoggerSync()
if (!embedString) { if (!embedString) {
return { isEnabled: false } return { isEnabled: false }
} }