Skip to content

Commit

Permalink
feat!: set setup plugin as parallel (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
huang-julien authored Jan 3, 2025
1 parent 2e159fc commit 871b19f
Showing 1 changed file with 96 additions and 92 deletions.
188 changes: 96 additions & 92 deletions src/runtime/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,104 +13,108 @@ import linkResolver from '#build/prismic/proxy/linkResolver'
// @ts-expect-error vfs cannot be resolved here
import richTextSerializer from '#build/prismic/proxy/richTextSerializer'

export default defineNuxtPlugin(async (nuxtApp) => {
const options: PrismicModuleOptions = useRuntimeConfig().public.prismic
let client: Client | undefined
if (typeof _client === 'function') {
try {
client = await nuxtApp.runWithContext(() => _client())
export default defineNuxtPlugin({
name: 'prismic:setup',
parallel: true,
async setup(nuxtApp) {

Check failure on line 19 in src/runtime/plugin.ts

View workflow job for this annotation

GitHub Actions / lint

Multiple spaces found before '{'
const options: PrismicModuleOptions = useRuntimeConfig().public.prismic
let client: Client | undefined
if (typeof _client === 'function') {
try {
client = await nuxtApp.runWithContext(() => _client())
}
catch (error) {
console.error('[@nuxtjs/prismic] An error happened while resolving your Prismic custom client, disabling Prismic module gracefully...', error)

Check failure on line 28 in src/runtime/plugin.ts

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
// 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 },
)
}
}
catch (error) {
console.error('[@nuxtjs/prismic] 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
}
}
else {
client = _client
}

const endpoint = options.environment || options.endpoint || client?.documentAPIEndpoint || ''

const prismicPlugin = createPrismic({
...options,
endpoint,
// TypeScript expects either an endpoint of a client, not both
client: client as undefined,
linkResolver,
richTextSerializer,
injectComponents: false, // Handled at module level
components: {
linkInternalComponent: NuxtLink,
linkExternalComponent: NuxtLink,
...options.components,
},
})

nuxtApp.vueApp.use(prismicPlugin)

if (options.preview) {
const previewCookie = useCookie('io.prismic.preview').value

// Update client with req when running server side
if (import.meta.server) {
const req = useRequestEvent()?.node.req
if (req) {
prismicPlugin.client.enableAutoPreviewsFromReq(req)

Check failure on line 39 in src/runtime/plugin.ts

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
const endpoint = options.environment || options.endpoint || client?.documentAPIEndpoint || ''

Check failure on line 41 in src/runtime/plugin.ts

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
const prismicPlugin = createPrismic({
...options,
endpoint,
// TypeScript expects either an endpoint of a client, not both
client: client as undefined,
linkResolver,
richTextSerializer,
injectComponents: false, // Handled at module level
components: {
linkInternalComponent: NuxtLink,
linkExternalComponent: NuxtLink,
...options.components,
},
})

Check failure on line 56 in src/runtime/plugin.ts

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
nuxtApp.vueApp.use(prismicPlugin)

Check failure on line 58 in src/runtime/plugin.ts

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
if (options.preview) {
const previewCookie = useCookie('io.prismic.preview').value

Check failure on line 61 in src/runtime/plugin.ts

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
// Update client with req when running server side
if (import.meta.server) {
const req = useRequestEvent()?.node.req
if (req) {
prismicPlugin.client.enableAutoPreviewsFromReq(req)
}
}
}

if (previewCookie) {
try {
const session = typeof previewCookie === 'string' ? JSON.parse(decodeURIComponent(previewCookie)) : previewCookie

if (Object.keys(session).some(key =>
key in session
&& typeof session[key] === 'object'
&& session[key] !== null
&& 'preview' in session[key]
&& session[key].preview)
) {
let afterEachCalled = false
onNuxtReady(() => {
if (!afterEachCalled) {

Check failure on line 69 in src/runtime/plugin.ts

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
if (previewCookie) {
try {
const session = typeof previewCookie === 'string' ? JSON.parse(decodeURIComponent(previewCookie)) : previewCookie

Check failure on line 73 in src/runtime/plugin.ts

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
if (Object.keys(session).some(key =>
key in session
&& typeof session[key] === 'object'
&& session[key] !== null
&& 'preview' in session[key]
&& session[key].preview)
) {
let afterEachCalled = false
onNuxtReady(() => {
if (!afterEachCalled) {
refreshNuxtData()
}
})
useRouter().afterEach(() => {
afterEachCalled = true
refreshNuxtData()
}
})
useRouter().afterEach(() => {
afterEachCalled = true
refreshNuxtData()
})
})
}
}
catch (error) {
console.warn('Failed to parse Prismic preview cookie', error)
}
}
catch (error) {
console.warn('Failed to parse Prismic preview cookie', error)
}
}
}

if (options.toolbar && prismicPlugin.client?.repositoryName) {
// Add toolbar
useHead({
script: [{
key: 'prismic-preview',
src: `https://static.cdn.prismic.io/prismic.min.js?repo=${prismicPlugin.client.repositoryName}&new=true`,
async: true,
defer: true,
crossorigin: 'anonymous',
}],
})
}
else {
// TODO: We might want to let user disable this behavior because it might have unexpected side effects
useCookie('io.prismic.preview').value = null
}

return {
provide: { prismic: prismicPlugin },

Check failure on line 98 in src/runtime/plugin.ts

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
if (options.toolbar && prismicPlugin.client?.repositoryName) {
// Add toolbar
useHead({
script: [{
key: 'prismic-preview',
src: `https://static.cdn.prismic.io/prismic.min.js?repo=${prismicPlugin.client.repositoryName}&new=true`,
async: true,
defer: true,
crossorigin: 'anonymous',
}],
})
}
else {
// TODO: We might want to let user disable this behavior because it might have unexpected side effects
useCookie('io.prismic.preview').value = null
}

return {
provide: { prismic: prismicPlugin },
}
}
})

0 comments on commit 871b19f

Please sign in to comment.