Intermittent error – 'Cannot read property 'nodeType' of undefined' – possibly caused by Decoration oddness on our part

At the moment, in a reasonably complex prosemirror-setup, we’re getting a very intermittent error – Cannot read property 'nodeType' of undefined. It’s thrown as a result of dom not being defined here. I logged an issue to describe this, but I thought I’d better post here, as it was pointed out that this is likely our problem, not Prosemirror’s!

After adding additional logging, we have more information about what’s going on. We added the following line to prosemirror-view just after the line linked above:

if (!dom) {
      throw new TypeError(("Tried to construct WidgetViewDesc with spec " + (JSON.stringify(widget.type.spec)) + ", but toDOM was falsy (should be (() => Element)|Element)"))
}

The error messages coming back are thus:

Tried to construct WidgetViewDesc with spec {"type":"DECORATION_MATCH","id":"8136e48c-cb39-44f1-8468-8177b15dd4b0","categoryId":"Typography","inclusiveStart":false,"inclusiveEnd":false}, but toDOM was falsy (should be (() => Element)|Element)

This is very odd – the decoration spec in question is added to an inline decoration, not a widget decoration, here. As far as I can tell there’s no dom property on an inline node, and so prosemirror-view tries to access something that isn’t there. It feels like an inline decoration has gotten somewhere it shouldn’t.

Stepping through the way PM renders decorations with a debugger, it looks like the relevant code to distinguish between inline and widget decos is here, where iterDeco calls onWidget for widget decos and onNode when it encounters a node – but the logic for how this distinction is made beyond this is very complicated to me at present.

No. 1 candidate for me at the moment is something mutating some property, possibly decoration related, that it shouldn’t. I’ve got to admit I’m quite stumped! I’ll continue to debug, but if anyone’s encountered a similar problem before, or knows much about these internals, any advice would be very much appreciated.

Do you know if you ever resolved this @jsh ? Hitting a similar thing but with node decorations attempting to render as widgets, triggering the same error – running the latest prosemirror-view.

Edit: Took a look at your OSS repo, looks like probably not resolved

Ah, should have cross-posted – I logged another issue when I discovered the root cause, which in my case was a zero-width decoration not being stripped from a DecorationSet when add() was used. We should really strip them from our plugin, it’s in our backlog – but Marijn’s patch has fixed this for us in the short term.

Odd that in your case that doesn’t appear to have solved the problem. As Marijn has mentioned in previous correspondence, it seems like there might a few ways to get into this state. Be interested to know more.