Skip to content

bootstrapImportedTrackedChangeComments crashes on docs with comments but no tracked changes #3223

@bjohas

Description

@bjohas

Summary

Opening a .docx that has Word comments but no tracked changes throws an uncaught TypeError during deferred bootstrap. The editor recovers (mounts and renders normally), but the error fires on every load.

Version

SuperDoc 1.31.2 (not fixed in 1.32.0 source — getTrackChanges.js still dereferences state.doc without a guard).

Repro

  1. Open any .docx that has at least one comment anchor and zero w:ins/w:del tracked-change marks.
  2. Mount it in SuperDoc.

Concrete case that triggered this:

  • A doctoral thesis .docx with comments: 88, trackedChanges: 0.
  • Opened verbatim (not saved through SuperDoc first).
  • Error appears once per load, inside a setTimeout queued from processLoadedDocxComments.

Stack trace

Uncaught TypeError: Cannot read properties of undefined (reading 'doc')
    at getTrackChanges (superdoc.js:95046)
    at createCommentForTrackChanges (superdoc.js:239074)
    at bootstrapImportedTrackedChangeComments (superdoc.js:239017)
    at superdoc.js:239068    ← inside a setTimeout

Root cause

getTrackChanges in packages/super-editor/src/editors/v1/extensions/track-changes/trackChangesHelpers/getTrackChanges.js dereferences state.doc unconditionally on the first line:

export const getTrackChanges = (state, id = null) => {
  const trackedChanges = [];
  const allInlineNodes = findInlineNodes(state.doc);   // ← throws when state is null/undefined
  ...
};

The deferred comment-bootstrap path (bootstrapImportedTrackedChangeComments, called via setTimeout after onCommentsLoaded) appears to call this with a null or not-yet-populated state. It runs unconditionally whenever there are imported comments, even when there are no tracked-change marks to process.

Suggested fix

Option A — guard getTrackChanges against a missing state (minimal):

 export const getTrackChanges = (state, id = null) => {
   const trackedChanges = [];
+  if (!state || !state.doc) return trackedChanges;
   const allInlineNodes = findInlineNodes(state.doc);

Option B — skip the bootstrap when there are no tracked-change marks (semantically correct):

 function bootstrapImportedTrackedChangeComments(editor, ...) {
+  if (!editor?.state?.doc) return;
+  if (getTrackChanges(editor.state).length === 0) return;
   ...
 }

Both fixes are complementary: A eliminates the crash class, B avoids the unnecessary work. A docs-with-comments-and-no-tracked-changes file is a perfectly normal Word document and should load cleanly.

Impact

Non-fatal — the editor mounts and behaves correctly. The bootstrap merely fails to attach tracked-change context to comments, which is fine since there are none. But the console error fires on every load and could mask real errors.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions