Within my Meteor application, I am attempting to dynamically load a random image from an API which returns JSON data structured like this:
{
"id":2026
"url": "https:// ... " ,
"large_url":null,
"source_id":609,
"copyright":"CC0",
"site":"unsplash"
}
My current approach looks like the following:
if (Meteor.isClient) {
Template.body.helpers({
randomImage: function() {
Meteor.call("unImage", function(error, results) {
Session.set('url', results.data.url);
});
return Session.get('url');
}
});
}
if (Meteor.isServer) {
Meteor.methods({
unImage: function() {
this.unblock();
return Meteor.http.call("GET", "http://www.splashbase.co/api/v1/images/random");
}
});
}
In my HTML structure:
<div class="header" style="background-image: url('{{randomImage}}')">
...
</div>
Although this method is functional, it ends up refreshing the image approximately every second. This behavior leads me to believe that the unImage
function on the server side is constantly reloading due to some underlying process or configuration. Any suggestions on resolving this issue and understanding why it persists?