Beautiful Open-Source Form Builder
Fix route chunk error recovery and observability (#1288)
Julien Nahumb80a17b2b8a6
diff --git a/client/lib/chunk-error-recovery.js b/client/lib/chunk-error-recovery.jsnew file mode 100644index 000000000..d78a157de--- /dev/null+++ b/client/lib/chunk-error-recovery.js@@ -0,0 +1,35 @@+export const CHUNK_ERROR_RECOVERY_TAG = 'chunk-error-recovery'++export function createChunkErrorRecovery ({ captureException, flush, reload }) {+ let recoveryStarted = false++ return ({ error }) => {+ if (recoveryStarted) {+ return false+ }++ recoveryStarted = true++ try {+ captureException(error, {+ tags: {+ handled_by: CHUNK_ERROR_RECOVERY_TAG,+ },+ })+ } catch {+ // Recovery must not depend on telemetry being available.+ }++ Promise.resolve()+ .then(() => flush(500))+ .catch(() => {})+ .finally(() => {+ reload({+ persistState: true,+ ttl: 60_000,+ })+ })++ return true+ }+}diff --git a/client/nuxt.config.ts b/client/nuxt.config.tsindex 64f7c4e9b..a444107fb 100644--- a/client/nuxt.config.ts+++ b/client/nuxt.config.ts@@ -86,7 +86,8 @@ export default defineNuxtConfig({},experimental: {- inlineRouteRules: true+ inlineRouteRules: true,+ emitRouteChunkError: 'manual'},sentry: {diff --git a/client/plugins/0.error-handler.client.js b/client/plugins/0.error-handler.client.jsindex 29025e510..a6e473c0e 100644--- a/client/plugins/0.error-handler.client.js+++ b/client/plugins/0.error-handler.client.js@@ -1,21 +1,12 @@-export default defineNuxtPlugin((nuxtApp) => {-const router = useRouter()+import * as Sentry from '@sentry/nuxt'+import { createChunkErrorRecovery } from '~/lib/chunk-error-recovery.js'- router.onError((error) => {- if (- error.message.includes('Failed to fetch dynamically imported module') ||- error.message.includes('Failed to load resource')- ) {- window.location.reload()- }- })+export default defineNuxtPlugin((nuxtApp) => {+ const recover = createChunkErrorRecovery({+ captureException: Sentry.captureException,+ flush: Sentry.flush,+ reload: reloadNuxtApp,+ })- nuxtApp.hook('app:error', (error) => {- if (- error.message.includes('Loading chunk') ||- error.message.includes('Failed to load resource')- ) {- window.location.reload()- }- })-})\ No newline at end of file+ nuxtApp.hook('app:chunkError', recover)+})diff --git a/client/sentry.client.config.ts b/client/sentry.client.config.tsindex c74802410..e64970f19 100644--- a/client/sentry.client.config.ts+++ b/client/sentry.client.config.ts@@ -1,4 +1,5 @@import * as Sentry from "@sentry/nuxt";+import { CHUNK_ERROR_RECOVERY_TAG } from './lib/chunk-error-recovery.js'Sentry.init({// If set up, you can use your runtime config here@@ -27,12 +28,15 @@ Sentry.init({return null}- // Filter out chunk loading errors- if (+ // The recovery plugin reports exact Nuxt chunk errors before reloading.+ // Keep filtering noisy browser errors that merely resemble chunk failures.+ const isRecoveredChunkError = event.tags?.handled_by === CHUNK_ERROR_RECOVERY_TAG+ const resemblesChunkError =errorValue.includes('Failed to fetch dynamically imported module') ||errorValue.includes('Loading chunk') ||errorValue.includes('Failed to load resource')- ) {++ if (!isRecoveredChunkError && resemblesChunkError) {return null}}diff --git a/client/test/unit/chunk-error-recovery.test.ts b/client/test/unit/chunk-error-recovery.test.tsnew file mode 100644index 000000000..680cc05b9--- /dev/null+++ b/client/test/unit/chunk-error-recovery.test.ts@@ -0,0 +1,80 @@+import { describe, expect, it, vi } from 'vitest'+import {+ CHUNK_ERROR_RECOVERY_TAG,+ createChunkErrorRecovery,+} from '../../lib/chunk-error-recovery.js'++const flushPromises = () => new Promise(resolve => setTimeout(resolve, 0))++describe('createChunkErrorRecovery', () => {+ it('reports the exact chunk error before reloading with persistent loop protection', async () => {+ const captureException = vi.fn()+ const flush = vi.fn(() => Promise.resolve(true))+ const reload = vi.fn()+ const recover = createChunkErrorRecovery({ captureException, flush, reload })+ const error = new Error('Failed to fetch dynamically imported module')++ expect(recover({ error })).toBe(true)+ await flushPromises()++ expect(captureException).toHaveBeenCalledWith(error, {+ tags: {+ handled_by: CHUNK_ERROR_RECOVERY_TAG,+ },+ })+ expect(flush).toHaveBeenCalledWith(500)+ expect(reload).toHaveBeenCalledWith({+ persistState: true,+ ttl: 60_000,+ })+ })++ it('deduplicates chunk errors while recovery is already in progress', async () => {+ let finishFlush+ const flush = vi.fn(() => new Promise(resolve => {+ finishFlush = resolve+ }))+ const captureException = vi.fn()+ const reload = vi.fn()+ const recover = createChunkErrorRecovery({ captureException, flush, reload })++ expect(recover({ error: new Error('first') })).toBe(true)+ expect(recover({ error: new Error('second') })).toBe(false)+ await Promise.resolve()+ finishFlush(true)+ await flushPromises()++ expect(captureException).toHaveBeenCalledTimes(1)+ expect(reload).toHaveBeenCalledTimes(1)+ })++ it('still reloads when telemetry cannot be flushed', async () => {+ const reload = vi.fn()+ const recover = createChunkErrorRecovery({+ captureException: vi.fn(),+ flush: vi.fn(() => Promise.reject(new Error('Sentry unavailable'))),+ reload,+ })++ expect(recover({ error: new Error('chunk failure') })).toBe(true)+ await flushPromises()++ expect(reload).toHaveBeenCalledTimes(1)+ })++ it('still reloads when telemetry capture throws synchronously', async () => {+ const reload = vi.fn()+ const recover = createChunkErrorRecovery({+ captureException: vi.fn(() => {+ throw new Error('Sentry unavailable')+ }),+ flush: vi.fn(() => Promise.resolve(true)),+ reload,+ })++ expect(recover({ error: new Error('chunk failure') })).toBe(true)+ await flushPromises()++ expect(reload).toHaveBeenCalledTimes(1)+ })+})