@@ -41,6 +41,10 @@ export function useCommentsNavigator () {
4141 // prevent multiple updates in the same frame
4242 frameRef . current = true
4343 window . requestAnimationFrame ( ( ) => {
44+ // filter out disconnected refs before counting
45+ commentRefs . current = commentRefs . current . filter ( item =>
46+ item . ref ?. current ?. isConnected
47+ )
4448 const next = commentRefs . current . length
4549 // transition to the new comment count
4650 startTransition ?. ( ( ) => setCommentCount ( next ) )
@@ -93,7 +97,11 @@ export function useCommentsNavigator () {
9397 const { includeDescendants = false , clearOutline = false } = options
9498
9599 const refNode = commentRef . current
96- if ( ! refNode ) return
100+ if ( ! refNode ) {
101+ // update the comment count, the ref may be disconnected
102+ throttleCountUpdate ( )
103+ return
104+ }
97105
98106 const toRemove = commentRefs . current . filter ( item => {
99107 const node = item ?. ref ?. current
@@ -117,8 +125,8 @@ export function useCommentsNavigator () {
117125
118126 if ( toRemove . length ) {
119127 commentRefs . current = commentRefs . current . filter ( item => ! toRemove . includes ( item ) )
120- throttleCountUpdate ( )
121128 }
129+ throttleCountUpdate ( )
122130 } , [ throttleCountUpdate ] )
123131
124132 // scroll to the next new comment
@@ -128,7 +136,11 @@ export function useCommentsNavigator () {
128136
129137 const ref = list [ 0 ] ?. ref
130138 const node = ref ?. current
131- if ( ! node ) return
139+ if ( ! node ) {
140+ // update the comment count, the ref may be disconnected
141+ throttleCountUpdate ( )
142+ return
143+ }
132144
133145 // smoothly scroll to the start of the comment
134146 node . scrollIntoView ( { behavior : 'smooth' , block : 'start' } )
@@ -148,7 +160,7 @@ export function useCommentsNavigator () {
148160
149161 // if we reached the end, reset the navigator
150162 if ( list . length === 1 ) clearCommentRefs ( )
151- } , [ clearCommentRefs , untrackNewComment ] )
163+ } , [ clearCommentRefs , untrackNewComment , throttleCountUpdate ] )
152164
153165 // create the navigator object once
154166 if ( ! navigatorRef . current ) {
0 commit comments