Skip to content

Commit

Permalink
Update SanityLive.tsx
Browse files Browse the repository at this point in the history
  • Loading branch information
stipsan committed Nov 21, 2024
1 parent 139d541 commit 84808c4
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions next-enterprise/src/app/SanityLive.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {CorsOriginError} from '@sanity/client'
import {useRouter} from 'next/navigation'
import {useEffect} from 'react'
import {useEffectEvent} from 'use-effect-event'
import {expireTags} from './actions'

export function SanityLive() {
const router = useRouter()
Expand All @@ -16,19 +15,23 @@ export function SanityLive() {
if (event.type === 'welcome') {
console.info('Sanity is live with automatic refresh of published content')
} else if (event.type === 'message') {
expireTags(event.tags)
setTimeout(() => {
if (signal.aborted) return
router.refresh()
}, 1_000)
} else if (event.type === 'restart') {
router.refresh()
}
},
)
useEffect(() => {
const controller = new AbortController()
const {signal} = controller
let controller = new AbortController()
const subscription = client.live.events().subscribe({
next: (event) => {
if (event.type === 'message' || event.type === 'restart' || event.type === 'welcome') {
handleLiveEvent(event, signal)
controller.abort()
controller = new AbortController()
handleLiveEvent(event, controller.signal)
}
},
error: (error: unknown) => {
Expand All @@ -44,6 +47,7 @@ export function SanityLive() {
},
})
return () => {
controller.abort()
subscription.unsubscribe()
}
}, [handleLiveEvent])
Expand Down

0 comments on commit 84808c4

Please sign in to comment.