Skip to content

Appending to innerHTML in a directive appears to break rendering #7207

@Pezmc

Description

@Pezmc

Vue version

3.2.45

Link to minimal reproduction

https://github.com/Pezmc/vuejs-append-to-innerHTML-issue

Steps to reproduce

git clone https://github.com/Pezmc/vuejs-append-to-innerHTML-issue
npm install
npm run dev

Click the toggle button

What is expected?

Vue.js should continue to be able to run render updates even after content has been appended to the HTML
Or throw a clearer error message warning not to modify innerHTML.

What is actually happening?

Vue.js appears to crash entirely throwing the below error. This breaks reactivity not only the current Component, but all components on the page.

Error: TypeError: Cannot read properties of null (reading 'insertBefore')

Section of Vue.js code that throws the error:

const nodeOps = {
    // Both parent and anchor are null
    insert: (child, parent, anchor) => {
        parent.insertBefore(child, anchor || null);
    },

System Info

System:
    OS: macOS 13.0
    CPU: (10) arm64 Apple M1 Max
    Memory: 3.92 GB / 64.00 GB
    Shell: 3.5.1 - /opt/homebrew/bin/fish
  Binaries:
    Node: 16.17.0 - ~/.volta/tools/image/node/16.17.0/bin/node
    Yarn: 1.22.19 - ~/.volta/tools/image/yarn/1.22.19/bin/yarn
    npm: 8.15.0 - ~/.volta/tools/image/node/16.17.0/bin/npm
  Browsers:
    Brave Browser: 107.1.45.131
    Chrome: 107.0.5304.110
    Firefox: 105.0.1
    Safari: 16.1
    Safari Technology Preview: 16.4
  npmPackages:
    vue: ^3.2.45 => 3.2.45

Any additional comments?

Perhaps there is a better best practice for adding data to the DOM in a directive.

I originally came across this as we're using a directive to append a tooltip that shows on hover to the DOM.

For what it's worth it seems that using append() rather than innerHTML += sidesteps the issue (see https://github.com/Pezmc/vuejs-append-to-innerHTML-issue/compare/vuejs-append-to-dom-sidestep?expand=1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions