Skip to content

Commit

Permalink
fix: add missing preview mode check
Browse files Browse the repository at this point in the history
  • Loading branch information
rdunk committed Mar 12, 2024
1 parent 34a8850 commit 510c8b1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ export default defineNuxtModule<SanityModuleOptions>({

nuxt.options.runtimeConfig.sanity = defu(nuxt.options.runtimeConfig.sanity, {
visualEditing: options.visualEditing && {
...visualEditing,
previewModeId: visualEditing!.previewMode ? crypto.randomBytes(16).toString('hex') : '',
token: options.visualEditing.token || '',
},
Expand Down
8 changes: 7 additions & 1 deletion src/runtime/plugins/visual-editing.client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
// @ts-expect-error need correct typing of #imports
import { defineNuxtPlugin, useRuntimeConfig, useSanityVisualEditing, useSanityLiveMode } from '#imports'
import { defineNuxtPlugin, useRuntimeConfig, useSanityVisualEditing, useSanityLiveMode, useState } from '#imports'

export default defineNuxtPlugin(() => {
const $config = useRuntimeConfig();
const { visualEditing } = $config.public.sanity;

// The state value will be true if preview mode is enabled.
// If preview mode is not enabled, return early.
if (!useState('_sanity_visualEditing').value) {
return
}

if (
visualEditing?.mode === 'live-visual-editing' ||
visualEditing?.mode === 'visual-editing'
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/plugins/visual-editing.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ export default defineNuxtPlugin(() => {
const $config = useRuntimeConfig()
const { visualEditing } = $config.sanity

// If preview mode is _configured_ (i.e. `visualEditing.previewMode` is set)
// check the cookie value against `previewModeId` to determine if visual
// editing is enabled.
if (visualEditing?.previewMode) {
const previewModeCookie = useCookie('__sanity_preview')
enabled.value = previewModeCookie.value === visualEditing.previewModeId
// If preview mode is _not_ configured, just enable visual editing.
} else if (typeof visualEditing === 'object' && !visualEditing.previewMode) {
enabled.value = true
}
Expand Down

0 comments on commit 510c8b1

Please sign in to comment.