When preloading an image with Javascript, is there a difference between using new Image() vs document.createElement(link) with re="preload"?
E.g., is there a difference between:
const loadImage = (src) =>
new Promise(r => {
const image = new Image()
image.onload = r
image.onerror = r
image.src = src
})
and
const loadImage = (src) =>
new Promise((resolve, reject) => {
const link = document.createElement("link")
link.rel = "preload"
link.as = "image"
link.href = src
link.onload = () => {
document.head.removeChild(link)
resolve(undefined)
}
link.onerror = reject
document.head.appendChild(link)
})
I couldn't notice anything when testing. My use case is I want to load an image before updating the page to avoid a flash as the image loads.