Skip to content

insertBefore crashes with "null is not an object (evaluating 'insertBefore')" when parent is null #6822

Description

@iterox

Prerequisites

Stencil Version

4.43.5 (also present on main/nightly 4.44.0-dev)

Current Behavior

We see ~20 crashes/week, exclusively on iOS Safari. We strongly suspect Safari's page-translation/Reader feature mutates light-DOM slotted nodes, detaching them so parentNode becomes null during a re-render (same class of problem as Google-Translate-breaks-React). The unguarded access then throws. Suggested fix: if (parent?.insertBefore) { … } (or parent != null && parent.insertBefore), consistent with the already null-safe fallback branch. Repro: No deterministic repro; occurs on shadow: false/scoped components under Safari-side DOM mutation.

Expected Behavior

In src/runtime/vdom/vdom-render.ts, the insertBefore helper guards the fallback path against a null parent, but not the insertBefore path:

if (parent.__insertBefore) {                 // ← unguarded, throws if parent === null
  return parent.__insertBefore(newNode, reference);
} else {
  return parent?.insertBefore(newNode, reference);   // ← already null-safe
}

Steps to Reproduce

We see ~20 crashes/week, exclusively on iOS Safari. We strongly suspect Safari's page-translation/Reader feature mutates light-DOM slotted nodes, detaching them so parentNode becomes null during a re-render (same class of problem as Google-Translate-breaks-React). The unguarded access then throws. Suggested fix: if (parent?.insertBefore) { … } (or parent != null && parent.insertBefore), consistent with the already null-safe fallback branch. Repro: No deterministic repro; occurs on shadow: false/scoped components under Safari-side DOM mutation.

Code Reproduction URL

https://github.com/dso-toolkit/dso-toolkit/

PR with patch

dso-toolkit/dso-toolkit#3921

Additional Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions