choojs / choojs/nanomorph

Recommended way to cache <img> elements

Open
#123 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
751
Forks
57
PR merge metrics
No merged PRs in 30d

Description

Hey there! I'm wondering if there are any recommended ways to cache elements. Some of my render loops happen in a requestAnimationFrame(), so I'd like to prevent tons of network requests for the same images.

:loop: :loop: :loop:

I tried declaring my icons once, outside of my render function. E.g.:

```
let someIcon = html``

let renderButton = () => {
return html`
${ someIcon } Click me
`
}

let renderAll = () => {
morph(oldButton, renderButton())
}
```

This prevents additional network requests, but it still causes unnecessary changes to the DOM because the cached `img` node can only live in one tree at a time. So when `morph` does a comparison, the `img` exists in one tree and not the other, and it does a replacement.

Now that I'm writing this, maybe what I need is exactly two copies of each image. One for the real DOM, and one for the comparison DOM?

Would love to hear if/how other people have handled this sort of thing!

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.