From 53d371b019deae7a1f8a70fc478ffdf6e23553f5 Mon Sep 17 00:00:00 2001 From: lihbr Date: Mon, 2 Dec 2024 15:16:11 +0100 Subject: [PATCH] fix: disable Prismic module gracefully on custom client resolution error --- src/runtime/plugin.ts | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/runtime/plugin.ts b/src/runtime/plugin.ts index f6a1a23..8f72b6d 100644 --- a/src/runtime/plugin.ts +++ b/src/runtime/plugin.ts @@ -1,7 +1,8 @@ -import type { Client } from '@prismicio/client' +import { createClient, type Client } from '@prismicio/client' import { createPrismic } from '@prismicio/vue' import type { PrismicModuleOptions } from '../types' +import { logger } from '../lib/logger' import { defineNuxtPlugin } from '#app' import NuxtLink from '#app/components/nuxt-link' import { useCookie, useRequestEvent, onNuxtReady, refreshNuxtData, useHead, useRuntimeConfig, useRouter } from '#imports' @@ -15,7 +16,24 @@ import richTextSerializer from '#build/prismic/proxy/richTextSerializer' export default defineNuxtPlugin(async (nuxtApp) => { const options: PrismicModuleOptions = useRuntimeConfig().public.prismic - const client: Client | undefined = typeof _client === 'function' ? await nuxtApp.runWithContext(() => _client()) : _client + let client: Client | undefined + if (typeof _client === 'function') { + try { + client = await nuxtApp.runWithContext(() => _client()) + } + catch (error) { + logger.error('An error happened while resolving your Prismic custom client, disabling Prismic module gracefully...', error) + + // The Vue plugin still requires a client to work, we're providing an obviously broken one. + client = createClient( + 'error-resolving-custom-client', + { ...options, documentAPIEndpoint: undefined }, + ) + } + } + else { + client = _client + } const endpoint = options.environment || options.endpoint || client?.documentAPIEndpoint || ''