I’m working on a Twitch chat widget and need help with image handling. Right now, I’m fetching user profile pictures from the Twitch API. Here’s a simplified version of what I’m doing:
function getProfilePic(username) {
fetch(`https://api.twitch.tv/helix/users?login=${username}`, {
headers: { 'Client-ID': 'my-client-id' }
})
.then(response => response.json())
.then(data => {
let picUrl = data.data[0].profile_image_url || 'default-pic-url';
displayProfilePic(picUrl);
});
}
function displayProfilePic(url) {
let img = document.createElement('img');
img.src = url;
document.body.appendChild(img);
}
This works, but I want to save these images locally to reduce API calls. How can I:
- Check if we already have the image saved
- If not, download and save it with the username as the filename
- Use the local file instead of the API URL
Any tips on implementing this in JavaScript?