Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommended way to cache <img> elements #123

Open
bennlich opened this issue Jun 20, 2021 · 0 comments
Open

Recommended way to cache <img> elements #123

bennlich opened this issue Jun 20, 2021 · 0 comments

Comments

@bennlich
Copy link

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.

➿ ➿ ➿

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

let someIcon = html`<img src="icon.png">`

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

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant