Skip to content

fix(web): 修复历史消息加载时的滚动位置恢复问题#170

Open
0x7551 wants to merge 6 commits intotiann:mainfrom
0x7551:fix/scroll-position-restoration-v2
Open

fix(web): 修复历史消息加载时的滚动位置恢复问题#170
0x7551 wants to merge 6 commits intotiann:mainfrom
0x7551:fix/scroll-position-restoration-v2

Conversation

@0x7551
Copy link
Contributor

@0x7551 0x7551 commented Feb 10, 2026

问题描述

在加载历史消息时,自动滚动功能会干扰滚动位置的恢复。

修复内容

  1. 防止 autoScroll 在历史加载时干扰滚动位置
  2. 等待 DOM 更新完成后再恢复滚动位置

via HAPI

Co-Authored-By: HAPI noreply@hapi.run

tfq and others added 6 commits February 9, 2026 15:20
…l position

When loading older messages, the assistant-ui library's autoScroll feature
was interfering with our manual scroll position restoration, causing the
viewport to jump to the top instead of maintaining position.

Root cause: ThreadPrimitive.Viewport's autoScroll triggers scrollToBottom()
on content resize when autoScroll=true and isAtBottom=true. This conflicts
with our useLayoutEffect scroll position restoration.

Fix: Temporarily disable autoScroll during history load:
1. Set autoScrollEnabled=false when handleLoadMore starts
2. Restore scroll position in useLayoutEffect as before
3. Re-enable autoScroll after position is restored
4. Handle error cases to ensure autoScroll is always re-enabled

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>
Add detailed logging to understand why scroll position jumps to top
when loading older messages instead of maintaining position.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>
The previous implementation tried to restore scroll position immediately
when messagesVersion changed, but the new messages hadn't been rendered
to DOM yet, causing delta to be 0 and scroll position to remain at top.

Fix: Use requestAnimationFrame to wait for DOM update and check if
scrollHeight has actually changed before restoring position.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>
Remove temporary debug logging added during development.
The fix is working correctly - scroll position is now properly
maintained when loading older messages.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>
Address code review feedback:

1. Preserve autoScroll state across history loads
   - Save autoScrollEnabled state before disabling it
   - Restore previous state instead of forcing true
   - Prevents unwanted scroll-to-bottom when user is viewing middle content

2. Add max attempts limit to requestAnimationFrame loop
   - Limit to 5 attempts to prevent infinite loop
   - Gracefully handle cases where DOM doesn't update
   - Improves performance and prevents browser hang

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>
Wrap setAutoScrollEnabled calls in setTimeout to ensure they execute
after all DOM operations complete. This prevents 'removeChild' errors
that occur when React tries to reconcile while DOM is being modified.

- Use setTimeout(fn, 0) for max attempts case
- Use setTimeout(fn, 100) for normal case to ensure DOM stability

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>
loadLockRef.current = false
// Re-enable autoScroll if loading finished but useLayoutEffect didn't run
setAutoScrollEnabled(true)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[MAJOR] Correctness 强制开启 autoScroll 导致滚动位置被重置

Why this is a problem: 在 useLayoutEffect 未触发时,当前实现会无条件 setAutoScrollEnabled(true),即便用户在加载前并不在底部,也会被强制打开自动滚动,导致加载历史后滚动跳到底部/位置丢失。

Suggested fix:

// 恢复为加载前的状态,避免强制开启 autoScroll
setAutoScrollEnabled(prevAutoScrollEnabledRef.current)

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Findings

  • [Major] 回退路径强制打开 autoScroll,可能把用户滚动拉回底部,破坏历史位置恢复,evidence web/src/components/AssistantChat/HappyThread.tsx:298
    Suggested fix:
    setAutoScrollEnabled(prevAutoScrollEnabledRef.current)

Summary

  • 1 个问题:加载结束的兜底分支会强制开启 autoScroll,可能引起滚动跳底。

Testing

  • Not run (automation)

HAPI Bot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments