Skip to content
Open
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
32 changes: 32 additions & 0 deletions packages/ui/src/views/Edit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ export function DefaultEditView({

const hasCheckedForStaleDataRef = useRef(false)
const originalUpdatedAtRef = useRef(data?.updatedAt)
const lastServerRefreshRef = useRef<number>(Date.now())

const lockExpiryTime = lastUpdateTime + lockDurationInMilliseconds
const isLockExpired = Date.now() > lockExpiryTime
Expand Down Expand Up @@ -315,6 +316,7 @@ export function DefaultEditView({
// This allows detecting if another user modifies the document after this save
originalUpdatedAtRef.current = updatedAt
hasCheckedForStaleDataRef.current = false
lastServerRefreshRef.current = Date.now()

if (context?.incrementVersionCount !== false) {
incrementVersionCount()
Expand Down Expand Up @@ -580,6 +582,36 @@ export function DefaultEditView({
}
}, [isInitializing])

// Fix #14217: Refresh document when user returns to tab after being away
// This prevents stale data from being displayed after publish
useEffect(() => {
if (typeof window === 'undefined' || !id) return

const handleVisibilityChange = () => {
if (document.visibilityState === 'visible') {
const timeSinceLastRefresh = Date.now() - lastServerRefreshRef.current
if (timeSinceLastRefresh >= 5000) {
router.refresh()
}
}
}

const handleFocus = () => {
const timeSinceLastRefresh = Date.now() - lastServerRefreshRef.current
if (timeSinceLastRefresh >= 5000) {
router.refresh()
}
}

document.addEventListener('visibilitychange', handleVisibilityChange)
window.addEventListener('focus', handleFocus)

return () => {
document.removeEventListener('visibilitychange', handleVisibilityChange)
window.removeEventListener('focus', handleFocus)
}
}, [id, router])

const shouldShowDocumentLockedModal =
documentIsLocked &&
currentEditor &&
Expand Down
Loading