Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: disable Prismic module gracefully on custom client resolution error #224

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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