Adding SVG to DOM from server via javascript sometimes fails

I’m trying to append SVG to the DOM, like so:

let avatarContainer: Element = document.querySelector('#avatarContainer');

avatarContainer.innerHTML = this.user.MainCharacterAvatarContent;

console.log( avatarContainer );

It works maybe 9 times out of 10, but sometimes the avatar is not there. I can see the SVG code as expected in the console, but when I inspect the div#avatarContainer it is empty. Can anyone suggest any reason this might be happening, or an alternative?

Note: I need to be able to reference elements in the SVG with elements