Skip to content

fix: message list loads the entire room history while hidden - #41787

Open
ndo84bw wants to merge 1 commit into
RocketChat:developfrom
ndo84bw:fix/message-list-history-loop-while-hidden
Open

fix: message list loads the entire room history while hidden#41787
ndo84bw wants to merge 1 commit into
RocketChat:developfrom
ndo84bw:fix/message-list-history-loop-while-hidden

Conversation

@ndo84bw

@ndo84bw ndo84bw commented Aug 14, 2026

Copy link
Copy Markdown

Proposed changes (including videos or screenshots)

RoomLayout hides the entire message body with display: none once a contextual bar takes the full room width, which happens below 600px of room width - not viewport width, since the breakpoints come from a ResizeObserver on the room element:

const contextualbarSize = breakpoints.includes('sm') ? (breakpoints.includes('xl') ? '38%' : '380px') : '100%';
const hideBody = aside && contextualbarSize === '100%';

A hidden element reports clientHeight and scrollTop as 0, so the load-older-messages check in
useGetMore is permanently true:

const { scrollTop, clientHeight, scrollHeight } = getBoundingClientRect(element);
...
if (hasMore === true && lastScrollTopRef <= height / 3) {   // 0 <= 0 / 3
    await RoomHistoryManager.getMore(rid);

The MutationObserver and ResizeObserver attached to that same element then drive the loading themselves: every page that arrives mutates the DOM, the observer fires, the next page loads.
Nothing stops it until the room has no history left. The client pulls the whole room 50 messages at a time and runs into the DDP rate limiter - the tail of the request list is loadHistory returning HTTP 400 too-many-requests.

RoomHistoryManager.restoreScroll cannot compensate, because it measures scrollHeight on the same hidden element and gets 0 - 0 as the height difference. So when the panel closes, the scroll offset is unchanged while the list is several times taller, and the user is left far in the past.

This PR bails out when the list is not rendered, and adds a unit test for it.

Before - one pixel narrower, thread opened, nobody scrolling: a stream of loadHistory calls
ending in HTTP 400, and the list is far up when the thread closes.

2026-08-14-nachladeschleife-880-vs-879-develop.mp4

After - same steps: 5 requests for the whole open-wait-close cycle (because scroll in thread), none of them
loadHistory, and the list stays where it was.

2026-08-14-nach-fix-879-develop.mp4

Control at 1220px room width: list height unchanged in both cases, no jump - so the trigger is the
hidden body, not opening a panel as such.

Issue(s)

Fixes #41132

Related to #41159, which addresses a different threshold - see Further comments.

Steps to test or reproduce

Four conditions have to hold at once, and two of them are easy to destroy by accident, which is
probably why this has looked flaky:

  1. hasMore must still be true - reload the page first. Once a room's history has been fully
    pulled in a session, that room is immune for the rest of the session.
  2. userInteracted must be true - scroll the main list once with the wheel, or click in it. The
    flag is a per-mount closure variable and resets on every room switch.
  3. The room area must be under 600px with a thread open, so the body is hidden.
  4. The list must not be at the very bottom - scroll up a little first. At the bottom the
    virtualizer keeps the list anchored there and the growth stays invisible.

Then:

  1. Narrow the window until the thread view takes the full chat width. With a default sidebar the
    threshold sits exactly between 880px and 879px of window width:
    window room area message body
    880px 600px visible
    879px 599px hidden
  2. Reload.
  3. Open a room with a few thousand messages spanning a long period, so the jump is visible by date.
  4. Scroll the main list up a little with the wheel.
  5. Open a thread from the message list.
  6. Wait 20-30 seconds with the window in the foreground, watching the network panel.
  7. Close the thread.

Sanity checks that should not trigger it: a wide window, closing the thread again within a
second, a second attempt without reloading, or opening the thread from the Threads contextual bar
without touching the main list first.

Tested on 8.8.0-develop at 02e633cf27 and on 8.6.1 at bfd782302d, in a room with 5162
messages spanning five years and a thread with 60 replies, at 580px room width.

Further comments

The guard reads the clientHeight that getBoundingClientRect already returned rather than asking
the element again. That is the exact value the position check uses, and it keeps the hook testable:
jsdom performs no layout and reports 0 for every element, so a guard reading element.clientHeight
directly would make the two existing tests in useGetMore.spec.tsx fail.

Alternatives considered:

  • Guarding inside gatedCheck so only observer-driven calls are skipped. That leaves the same
    always-true condition in place for any other caller, and a hidden element cannot emit a scroll
    event anyway, so the guard belongs where the value is read.
  • Not observing the element at all while it is hidden. That is more code for the same effect and
    would need the layout state threaded into this hook.
  • Removing hideBody so the situation cannot arise. That is what fix: prevent scroll jump when closing thread in narrow layout #41159 approaches from the layout
    side, but the 0 <= 0 fragility would remain for any other case that hides the list.

On #41159: it takes the aside out of the flex flow, which addresses a different threshold. Between
600px and 768px of room width the body is not hidden but squeezed - at 600px room width the message
list drops to 220px, text rewraps, cached row heights go stale, and the scroll position is off by a
noticeable amount when the panel closes. That is a separate defect with its own trigger; this change
does not touch that path and does not conflict with it, and the hideBody branch below 600px stays
as it is.

Review in cubic

Summary by CodeRabbit

  • Bug Fixes

    • Prevented hidden message lists from repeatedly loading older room history.
    • Avoided unnecessary pagination requests and potential server rate limiting when a contextual bar obscures the message list.
    • Preserved the correct scroll position when the message list becomes visible again.
  • Tests

    • Added coverage for hidden message containers with zero dimensions.

`RoomLayout` hides the whole message body with `display: none` once a
contextual bar takes the full room width, which happens below 600px of room
width. A hidden element reports `clientHeight` and `scrollTop` as 0, so the
position check in `useGetMore`, `scrollTop <= clientHeight / 3`, is
permanently true.

The MutationObserver and ResizeObserver on that same element then drive the
loading themselves: each loaded page mutates the DOM, the observer fires, the
next page loads. Nothing stops it until the room has no history left. On a
room with a few thousand messages the client pulls all of them, 50 at a time,
and runs into the DDP rate limiter.

`RoomHistoryManager.restoreScroll` cannot compensate either, because it
measures `scrollHeight` on the same hidden element and gets `0 - 0` as the
height difference. So when the panel closes, the scroll offset is unchanged
while the list is several times taller, and the user is left far in the past.

Bail out when the list is not rendered. The guard reads the `clientHeight`
that `getBoundingClientRect` already returned rather than the element again,
so it is the exact value the check uses, and it stays testable: jsdom performs
no layout and reports 0 for every element.

Measured on a room with 5162 messages at 580px room width: the list grew from
2738px to 29018px within 30 seconds of having a thread open; with this guard
it stays at 2738px and no `loadHistory` call is made at all.

Assisted-by: claude-code:claude-opus-5
@ndo84bw
ndo84bw requested a review from a team as a code owner August 14, 2026 08:52
@dionisio-bot

dionisio-bot Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Looks like this PR is not ready to merge, because of the following issues:

  • This PR is missing the 'stat: QA assured' label
  • This PR is missing the required milestone or project

Please fix the issues and try again

If you have any trouble, please check the PR guidelines

@changeset-bot

changeset-bot Bot commented Aug 14, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3f92dfb

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@rocket.chat/meteor Patch
@rocket.chat/core-typings Patch
@rocket.chat/rest-typings Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 48558a81-5d6d-44c8-a0b5-78a7ce98494a

📥 Commits

Reviewing files that changed from the base of the PR and between 02e633c and 3f92dfb.

📒 Files selected for processing (3)
  • .changeset/message-list-history-loop-while-hidden.md
  • apps/meteor/client/views/room/body/hooks/useGetMore.spec.tsx
  • apps/meteor/client/views/room/body/hooks/useGetMore.ts
📜 Recent review details
⏰ Context from checks skipped due to timeout. (1)
  • GitHub Check: cubic · AI code reviewer
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{ts,tsx,js}

📄 CodeRabbit inference engine (.cursor/rules/playwright.mdc)

**/*.{ts,tsx,js}: Write concise, technical TypeScript/JavaScript with accurate typing in Playwright tests
Avoid code comments in the implementation

Files:

  • apps/meteor/client/views/room/body/hooks/useGetMore.spec.tsx
  • apps/meteor/client/views/room/body/hooks/useGetMore.ts
apps/meteor/**

📄 CodeRabbit inference engine (CLAUDE.md)

The main Rocket.Chat Meteor application resides in apps/meteor/; place its application code there rather than in other monorepo areas.

Files:

  • apps/meteor/client/views/room/body/hooks/useGetMore.spec.tsx
  • apps/meteor/client/views/room/body/hooks/useGetMore.ts
🧠 Learnings (8)
📚 Learning: 2026-03-16T21:50:37.589Z
Learnt from: amitb0ra
Repo: RocketChat/Rocket.Chat PR: 39676
File: .changeset/migrate-users-register-openapi.md:3-3
Timestamp: 2026-03-16T21:50:37.589Z
Learning: For changes related to OpenAPI migrations in Rocket.Chat/OpenAPI, when removing endpoint types and validators from rocket.chat/rest-typings (e.g., UserRegisterParamsPOST, /v1/users.register) document this as a minor changeset (not breaking) per RocketChat/Rocket.Chat-Open-API#150 Rule 7. Note that the endpoint type is re-exposed via a module augmentation .d.ts in the consuming package (e.g., packages/web-ui-registration/src/users-register.d.ts). In reviews, ensure the changeset clearly states: this is a non-breaking change, the major version should not be bumped, and the changeset reflects a minor version bump. Do not treat this as a breaking change during OpenAPI migrations.

Applied to files:

  • .changeset/message-list-history-loop-while-hidden.md
📚 Learning: 2026-03-06T18:10:15.268Z
Learnt from: tassoevan
Repo: RocketChat/Rocket.Chat PR: 39397
File: packages/gazzodown/src/code/CodeBlock.spec.tsx:47-68
Timestamp: 2026-03-06T18:10:15.268Z
Learning: In tests (especially those using testing-library/dom/jsdom) for Rocket.Chat components, the HTML <code> element has an implicit ARIA role of 'code'. Therefore, screen.getByRole('code') or screen.findByRole('code') will locate <code> elements even without a role attribute. Do not flag findByRole('code') as invalid in reviews; prefer using the implicit role instead of adding role="code" unless necessary for accessibility.

Applied to files:

  • apps/meteor/client/views/room/body/hooks/useGetMore.spec.tsx
📚 Learning: 2026-03-27T14:52:56.865Z
Learnt from: dougfabris
Repo: RocketChat/Rocket.Chat PR: 39892
File: apps/meteor/client/views/room/contextualBar/Threads/Thread.tsx:150-155
Timestamp: 2026-03-27T14:52:56.865Z
Learning: In Rocket.Chat, there are two different `ModalBackdrop` components with different prop APIs. During review, confirm the import source: (1) `rocket.chat/fuselage` `ModalBackdrop` uses `ModalBackdropProps` based on `BoxProps` (so it supports `onClick` and other Box/DOM props) and does not have an `onDismiss` prop; (2) `rocket.chat/ui-client` `ModalBackdrop` uses a narrower props interface like `{ children?: ReactNode; onDismiss?: () => void }` and handles Escape keypress and outside mouse-up, and it does not forward arbitrary DOM props such as `onClick`. Flag mismatched props (e.g., `onDismiss` passed to the fuselage component or `onClick` passed to the ui-client component) and ensure the usage matches the correct component being imported.

Applied to files:

  • apps/meteor/client/views/room/body/hooks/useGetMore.spec.tsx
📚 Learning: 2026-05-06T12:21:44.083Z
Learnt from: juliajforesti
Repo: RocketChat/Rocket.Chat PR: 40256
File: apps/meteor/client/components/CreateDiscussion/CreateDiscussion.tsx:121-149
Timestamp: 2026-05-06T12:21:44.083Z
Learning: Field wrappers in rocket.chat/fuselage-forms (Field, FieldLabel, FieldRow, FieldError, FieldHint) auto-create htmlFor/id associations, aria-describedby, and role="alert" for errors. Do not manually set htmlFor, id, aria-describedby, or role attributes when using these wrappers. This automatic wiring does not apply to plain rocket.chat/fuselage components, which require explicit ID wiring per the accessibility docs. In code reviews, prefer using fuselage-forms wrappers for form fields and verify there is no unnecessary manual ID/aria wiring in files that use these wrappers. If a component uses plain fuselage components, ensure proper id wiring as per docs.

Applied to files:

  • apps/meteor/client/views/room/body/hooks/useGetMore.spec.tsx
  • apps/meteor/client/views/room/body/hooks/useGetMore.ts
📚 Learning: 2026-02-10T16:32:42.586Z
Learnt from: tassoevan
Repo: RocketChat/Rocket.Chat PR: 38528
File: apps/meteor/client/startup/roles.ts:14-14
Timestamp: 2026-02-10T16:32:42.586Z
Learning: In Rocket.Chat's Meteor client code, DDP streams use EJSON and Date fields arrive as Date objects; do not manually construct new Date() in stream handlers (for example, in sdk.stream()). Only REST API responses return plain JSON where dates are strings, so implement explicit conversion there if needed. Apply this guidance to all TypeScript files under apps/meteor/client to ensure consistent date handling in DDP streams and REST responses.

Applied to files:

  • apps/meteor/client/views/room/body/hooks/useGetMore.ts
📚 Learning: 2026-05-11T20:30:35.265Z
Learnt from: tassoevan
Repo: RocketChat/Rocket.Chat PR: 40480
File: apps/meteor/client/meteor/startup/accounts.ts:59-61
Timestamp: 2026-05-11T20:30:35.265Z
Learning: In Rocket.Chat’s Meteor client code, when calling `dispatchToastMessage` with `{ type: 'error' }`, pass the raw caught error object as `message` without manual normalization. `dispatchToastMessage` is designed to accept `message: unknown` for error toasts, so avoid converting errors to strings (e.g., `String(error)`) or extracting `error.message` before passing them.

Applied to files:

  • apps/meteor/client/views/room/body/hooks/useGetMore.ts
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In the Rocket.Chat repository, do not reference Biome lint rules in code review feedback. Biome is not used even if biome.json exists; only reference Biome rules if there is explicit, project-wide usage documented. For TypeScript files, review lint implications without Biome guidance unless the project enables Biome rules.

Applied to files:

  • apps/meteor/client/views/room/body/hooks/useGetMore.ts
📚 Learning: 2026-02-26T19:25:44.063Z
Learnt from: gabriellsh
Repo: RocketChat/Rocket.Chat PR: 38778
File: packages/ui-voip/src/providers/useMediaSession.ts:192-192
Timestamp: 2026-02-26T19:25:44.063Z
Learning: In this repository (RocketChat/Rocket.Chat), Biome lint rules are not used even if a biome.json exists. When reviewing TypeScript files (e.g., packages/ui-voip/src/providers/useMediaSession.ts), ensure lint suggestions do not reference Biome-specific rules. Rely on general ESLint/TypeScript lint rules and project conventions instead.

Applied to files:

  • apps/meteor/client/views/room/body/hooks/useGetMore.ts
🔇 Additional comments (3)
apps/meteor/client/views/room/body/hooks/useGetMore.ts (1)

44-46: 📐 Maintainability & Code Quality | ⚡ Quick win

Remove the new implementation comment.

The clientHeight === 0 guard is correct and runs before both pagination branches. Remove the three-line rationale from the implementation. The changeset already documents the reason for this guard.

As per coding guidelines, files matching **/*.{ts,tsx,js} must avoid code comments in the implementation.

[ suggest_recommended_refactor]

Source: Coding guidelines

.changeset/message-list-history-loop-while-hidden.md (1)

1-6: LGTM!

apps/meteor/client/views/room/body/hooks/useGetMore.spec.tsx (1)

98-100: 🎯 Functional Correctness

No change required. withThrottling({ wait: 100 }) invokes the first call immediately with real timers, and this test does not queue an earlier call.

			> Likely an incorrect or invalid review comment.

Walkthrough

The message list now skips older-message pagination when its scroll container has zero height. A regression test covers hidden containers, and a patch changeset documents the fix.

Changes

Hidden message pagination

Layer / File(s) Summary
Zero-height pagination guard
apps/meteor/client/views/room/body/hooks/useGetMore.ts, apps/meteor/client/views/room/body/hooks/useGetMore.spec.tsx, .changeset/message-list-history-loop-while-hidden.md
checkPositionAndGetMore returns when the message container has zero height. The regression test verifies that hidden containers do not call RoomHistoryManager.getMore. The changeset documents the fix.

Estimated code review effort: 2 (Simple) | ~10 minutes

Merge Risk: ⚪ Minimal · up to 3f92d

This is a localized guard preventing message history pagination while the list is hidden, with focused test coverage. No actionable merge-blocking risk remains beyond normal checks.

Suggested labels: type: bug

Suggested reviewers: ggazzo, martinschoeler

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the primary fix for hidden message lists loading the entire room history.
Linked Issues check ✅ Passed The guard and regression test address issue #41132 by preventing hidden message lists from loading history and changing scroll position.
Out of Scope Changes check ✅ Passed All changes support the linked issue and PR objective; the changeset, guard, and regression test are in scope.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

Warning

Review ran into problems

🔥 Problems

Errors were encountered while retrieving linked issues.

Errors (1)
  • A895A78D-7135: Request failed with status code 401

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 3 files

Re-trigger cubic

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Thread close scrolls main chat back after opening thread in narrow responsive layout

1 participant