API Logger (in-device/in-web)
an API logger within the native app’s embedded web view (web container) or in-web via DevTools panel so users can easily monitor all API calls made by the web page.
References:
Implementation Overview
- Use WeakMap for storing logs with capping:
- Server-side: WeakMap keyed by
req(NodeIncomingMessage) to isolate logs per SSR/ISR/CSP request - Client-side: WeakMap keyed by a provider context object created by
ApiLoggerProvider - Max entries:
MAX_LOG_ENTRIES(100). When exceeded, append a singleCAPPED_LOG_NOTICEwith taglog-limit
- Server-side: WeakMap keyed by
- Hydration and retrieval:
- Next context exposes getters for
serverApiLogsandcspApiLogsso they hydrate into__NEXT_DATA__ getSharedCacheableServerProps()attachesserverApiLogsviaconditionallyAddServerApiLogsGetter(serverCtx, cacheableCtx)cacheableServerProps()attachescspApiLogsviaconditionallyAddCspApiLogsGetter(cacheableCtx)- DevTools
getServerSideApiLogs()reads bothserverApiLogsandcspApiLogsfrom__NEXT_DATA__.props.pagePropsand concatenates
- Next context exposes getters for
- CSR logging funnels through
callAPIClientSide, appending entries directly without React dispatches - Env gating ensures no production exposure:
- Gate:
process.env.NODE_CONFIG_ENV !== 'production'(akaALLOW_API_LOGGING_ENV_GATE) - Toggle: cookie
tv-api-loggermust be set to"1"
- Gate:
- Server-side flow
getConditionalApiLoggerObj(ctx)returns{ appendApiLog }only when cookie is enabled and env gate allows- Cookie is detected from
ctx.req.cookies['tv-api-logger'](SSR) orctx.viewCacheKey['tv-api-logger'](CSP; extracted viaextractViewCacheKey) callAPIServerSide()callsappendApiLog()to add toWeakMap<req, ApiLogEntry[]>- Getters:
conditionallyAddServerApiLogsGetter(serverCtx, cacheableCtx)andconditionallyAddCspApiLogsGetter(cacheableCtx)enable hydration for SSR and CSP pages
- Client-side flow
ApiLoggerProvidercreates a context object and registers it viasetClientApiLoggerContext(contextObj)callAPIClientSide()callslogClientApiCall()to add entries toWeakMap<contextObj, ApiLogEntry[]>- DevTools reads
getClientApiLogs()and merges with SSR+CSP logs for unified display
- DevTools Integration
- Toggle
Enable API Loggerwrites/removes cookie and refreshes page Show API Loggeropens the modal/panel displaying combined entries- Provider upsert: DevTools conditionally injects
ApiLoggerProviderviaupsertProvider(<ApiLoggerProvider />)when the cookie is enabled, ensuring a single instance and clean removal when disabled
- Toggle