Skip to content

Instantly share code, notes, and snippets.

@pjbrown11
Created December 2, 2019 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjbrown11/6bf4be4ac19135308b79aa7cb246ca57 to your computer and use it in GitHub Desktop.
Save pjbrown11/6bf4be4ac19135308b79aa7cb246ca57 to your computer and use it in GitHub Desktop.
export const lazyLoadFullImg = (node, data) => {
const loaded = new Map();
// use bigger image file if warranted
const fileSize = window.devicePixelRatio > 1 ? '-2x' : '-1x'
const correctImg = `${data.path + fileSize}.${data.type}`
if (loaded.has(correctImg)) {
node.setAttribute("src", correctImg);
} else {
const img = new Image();
img.src = correctImg;
// TODO: get appropriate fallback image
// if the full size image fails to load use fallback
// img.onerror = () => {
// loaded.set('img/bio-background-design.svg', img);
// node.setAttribute("src", 'img/bio-background-design.svg');
// };
img.onload = () => {
loaded.set(correctImg, img);
node.setAttribute("src", correctImg);
};
}
return {
destroy() { } // noop
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment