From 946ae7cd5dd0906a71ce849ba1a5c29c2cfb9e2e Mon Sep 17 00:00:00 2001 From: Pranav C Date: Tue, 11 Jun 2024 18:14:43 +0000 Subject: [PATCH] fix: add a nuxt plugin to handle chunk error --- packages/nc-gui/plugins/error-handler.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 packages/nc-gui/plugins/error-handler.ts diff --git a/packages/nc-gui/plugins/error-handler.ts b/packages/nc-gui/plugins/error-handler.ts new file mode 100644 index 0000000000..b53c2a0d7d --- /dev/null +++ b/packages/nc-gui/plugins/error-handler.ts @@ -0,0 +1,17 @@ +// ref - https://github.com/nuxt/nuxt/issues/26565 +export default defineNuxtPlugin((nuxtApp) => { + const MAX_RETRIES = 2 + const QUERY_PARAM_NAME = 'reload_attempt' + + // Handle "Failed to fetch dynamically imported module ..." or similar issues + nuxtApp.hook('app:chunkError', () => { + const searchParams = new URLSearchParams(window.location.search) + const currentRetry = Number(searchParams.get(QUERY_PARAM_NAME)) || 0 + if (currentRetry < MAX_RETRIES) { + console.log('[nuxt]: Reloading due to chunk error') + searchParams.set(QUERY_PARAM_NAME, (currentRetry + 1).toString()) + // Changing the search also causes a refresh + window.location.search = searchParams.toString() + } + }) +})