ampproject / ampproject/worker-dom
Failed to execute 'insertBefore' on 'Node': This node type does not support this method
- Dominant language
- TypeScript
- Stars
- 3.3k
- Forks
- 154
- PR merge metrics
- No merged PRs in 30d
Description
I get the `DOMException: Failed to execute 'insertBefore' on 'Node': This node type does not support this method.` in [child-list.ts:57](https://github.com/ampproject/worker-dom/blob/main/src/main-thread/commands/child-list.ts#L57): `target.insertBefore(newNode, (nextSibling && getNode(nextSibling)) || null);`. The `target` and `newNode` are text nodes (`nodeName === "#text"`) and `nextSibling` is undefined. You cannot add children to a text node.
Could you please fix it by checking `if (target.nodeType !== 3)`?
Stacktrace:
> Uncaught DOMException: Failed to execute 'insertBefore' on 'Node': This node type does not support this method.
> at workerdom.min.js:2:7552
> at Uint16Array.forEach ()
> at Object.execute (workerdom.min.js:2:7505)
> at workerdom.min.js:2:10802
> at Array.forEach ()
> at syncFlush (workerdom.min.js:2:10604)
I am calling `el.innerHTML = "..."` in a loop.
Contributor guide
Research direction
Start with src/main-thread/commands/child-list.ts at line 57, where insertBefore is called with the reported text-node target. Reproduce the failure using an innerHTML loop and verify that the resulting behavior no longer raises the DOMException for text-node targets.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- frontend, web-dev
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100