fix(nuxt): handle errors parsing/stringifying logs

This commit is contained in:
Daniel Roe 2024-03-27 17:12:57 +00:00
parent fa9d43753d
commit 4a87c35df6
No known key found for this signature in database
GPG Key ID: 3714AB03996F442B

View File

@ -37,9 +37,13 @@ export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.hook('dev:ssr-logs', (logs) => { nuxtApp.hook('dev:ssr-logs', (logs) => {
for (const log of logs) { for (const log of logs) {
// deduplicate so we don't print out things that are logged on client // deduplicate so we don't print out things that are logged on client
try {
if (!hydrationLogs.size || !hydrationLogs.has(JSON.stringify(log.args))) { if (!hydrationLogs.size || !hydrationLogs.has(JSON.stringify(log.args))) {
logger.log(normalizeServerLog({ ...log })) logger.log(normalizeServerLog({ ...log }))
} }
} catch {
logger.log(normalizeServerLog({ ...log }))
}
} }
}) })
@ -50,7 +54,8 @@ export default defineNuxtPlugin((nuxtApp) => {
// pass SSR logs after hydration // pass SSR logs after hydration
nuxtApp.hooks.hook('app:suspense:resolve', async () => { nuxtApp.hooks.hook('app:suspense:resolve', async () => {
if (typeof window !== 'undefined') { if (typeof window !== 'undefined') {
const logs = parse(document.getElementById('__NUXT_LOGS__')?.textContent || '[]', nuxtApp._payloadRevivers) as LogObject[] const content = document.getElementById('__NUXT_LOGS__')?.textContent
const logs = content ? parse(content, nuxtApp._payloadRevivers) as LogObject[] : []
await nuxtApp.hooks.callHook('dev:ssr-logs', logs) await nuxtApp.hooks.callHook('dev:ssr-logs', logs)
} }
}) })