Skip to content

Conversation

@arturovt
Copy link
Contributor

@arturovt arturovt commented Dec 4, 2025

We've been encountering runtime errors in our production monitoring system: TypeError: Cannot read properties of null (reading 'scrollTop')

The error originates from ViewportRuler.getViewportScrollPosition when accessing document.body.scrollTop and document.body.scrollLeft.

Creating a minimal reproduction is impractical, as the issue occurs during edge cases in page navigation/unload cycles in a large application.

According to the WHATWG HTML specification, document.body can be null when the document element is not <html> or has no <body>/<frameset> child element. This typically occurs during page navigation or document unload when the DOM structure is being torn down.

Spec: https://html.spec.whatwg.org/multipage/dom.html#dom-document-body

TypeScript's lib.dom.d.ts incorrectly types document.body as non-nullable HTMLElement, which masks this issue at compile time but allows the runtime error to occur.

Related TypeScript issues:

Added optional chaining (?.) when accessing document.body.scrollTop and document.body.scrollLeft to safely handle the null case.

Note: This PR focuses on fixing the immediate issue in ViewportRuler. If accepted, similar patterns elsewhere in the codebase can be addressed in follow-up PRs.

We've been encountering runtime errors in our production monitoring system:
`TypeError: Cannot read properties of null (reading 'scrollTop')`

The error originates from `ViewportRuler.getViewportScrollPosition` when
accessing `document.body.scrollTop` and `document.body.scrollLeft`.

Creating a minimal reproduction is impractical, as the issue occurs during
edge cases in page navigation/unload cycles in a large application.

According to the WHATWG HTML specification, `document.body` can be `null`
when the document element is not `<html>` or has no `<body>`/`<frameset>`
child element. This typically occurs during page navigation or document
unload when the DOM structure is being torn down.

Spec: https://html.spec.whatwg.org/multipage/dom.html#dom-document-body

TypeScript's `lib.dom.d.ts` incorrectly types `document.body` as
non-nullable `HTMLElement`, which masks this issue at compile time but
allows the runtime error to occur.

Related TypeScript issues:
- microsoft/TypeScript#50078 (documentElement nullability)
- microsoft/TypeScript#22868 (body type issue)

Added optional chaining (`?.`) when accessing `document.body.scrollTop`
and `document.body.scrollLeft` to safely handle the null case.

Note: This PR focuses on fixing the immediate issue in `ViewportRuler`.
If accepted, similar patterns elsewhere in the codebase can be addressed
in follow-up PRs.
Copy link
Contributor

@mmalerba mmalerba left a comment

Choose a reason for hiding this comment

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

Thanks for including context & links with this!

@mmalerba mmalerba added action: merge The PR is ready for merge by the caretaker target: patch This PR is targeted for the next patch release labels Dec 5, 2025
@tjshiu tjshiu removed their request for review December 9, 2025 03:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: merge The PR is ready for merge by the caretaker area: cdk/scrolling target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants