Skip to content

Commit

Permalink
fix: disable Prismic module gracefully on custom client resolution er…
Browse files Browse the repository at this point in the history
…ror (#224)

Co-authored-by: lihbr <lihbr@users.noreply.github.com>
  • Loading branch information
lihbr and lihbr authored Dec 3, 2024
1 parent e182bbb commit 359dcee
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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 || ''

Expand Down

0 comments on commit 359dcee

Please sign in to comment.