开发者

Refresh image with Javascript, but only if changed on server

I want to reload an image on a page if it has been updated on the server. In other questions it has been suggested to do something like

newImage.src = "http://localhost/image.jpg?" + new Date().getTime();

to force the image to be re-loaded, but that means that it will get downloaded again even if it really hasn't changed.

Is there any Javascript c开发者_运维百科ode that will cause a new request for the same image to be generated with a proper If-Modified-Since header so the image will only be downloaded if it has actually changed?

UPDATE: I'm still confused: if I just request the typical URL, I'll get the locally cached copy. (unless I make the server mark it as not cacheable, but I don't want to do that because the whole idea is to not re-download it unless it really changes.) if I change the URL, I'll always re-download, because the point of the new URL is to break the cache. So how do I get the in-between behavior I want, i.e. download the file only if it doesn't match the locally cached copy?


Javascript can't listen for an event on the server. Instead, you could employ some form of long-polling, or sequential calls to the server to see if the image has been changed.


You should have a look at the xhr.setRequestHeader() method. It's a method of any XMLHttpRequest object, and can be used to set headers on your Ajax queries. In jQuery, you can easily add a beforeSend property to your ajax object and set up some headers there.

That being said, caching with Ajax can be tricky. You might want to have a look at this thread on Google Groups, as there's a few issues involved with trying to override a browser's caching mechanisms. You'll need to ensure that your server is returning the proper cache control headers in order to be able to get something like this to work.


One way of doing this is to user server-sent events to have the server push a notification whenever the image has been changed. For this you need a server-side script that will periodically check for the image having been notified. The server-side script below ensures that the server sends an event at least once every (approximately) 60 seconds to prevent timeouts and the client-side HTML handles navigation away from and to the page:

sse.py

#!/usr/bin/env python3

import time
import os.path

print("Content-Type: text/event-stream\n\n", end="")

IMG_PATH = 'image.jpg'

modified_time = os.path.getmtime(IMG_PATH)
seconds_since_last_send = 0

while True:
    time.sleep(1)
    new_modified_time = os.path.getmtime(IMG_PATH)
    if new_modified_time != modified_time:
        modified_time = new_modified_time
        print('data: changed\n\n', end="", flush=True)
        seconds_since_last_send = 0
    else:
        seconds_since_last_send += 1
        if seconds_since_last_send == 60:
            print('data: keep-alive\n\n', end="", flush=True)
            seconds_since_last_send = 0

And then your HTML would include some JavaScript code:

sse.html

<html>
<head>
   <meta charset="UTF-8">
   <title>Server-sent events demo</title>
</head>
<body>
  <img id="img" src="image.jpg">

<script>
  const img = document.getElementById('img');
  let evtSource = null;

  function setup_sse()
  {
    console.log('Creating new EventSource.');
    evtSource = new EventSource('sse.py');

    evtSource.onopen = function() {
      console.log('Connection to server opened.');
    };

    // if we navigate away from this page:
    window.onbeforeunload = function() {
      console.log('Closing connection.');
      evtSource.close();
      evtSource = null;
    };

    evtSource.onmessage = function(e) {
      if (e.data == 'changed')
        img.src = 'image.jpg?version=' + new Date().getTime();
    };

    evtSource.onerror = function(err) {
      console.error("EventSource failed:", err);
    };
  }


  window.onload = function() {
    // if we navigate back to this page:
    window.onfocus = function() {
      if (!evtSource)
        setup_sse();
    };

    setup_sse(); // first time
  };
</script>
</body>
</html>


Here am loading an image, tree.png, as binary data dynamically with AJAX and saving the Last-Modified header. Periodically (every 5 second in the code below). I issue another download request sending backup a If-Modified-Since header using the saved last-modified header. I check to see if data has been returned and re-create the image with the data if present:

<!doctype html>
<html>
<head>
<title>Test</title>
<script>
window.onload = function() {

  let image = document.getElementById('img');
  var lastModified = ''; // 'Sat, 11 Jun 2022 19:15:43 GMT'

  function _arrayBufferToBase64(buffer) {
    var binary = '';
    var bytes = new Uint8Array(buffer);
    var len = bytes.byteLength;
    for (var i = 0; i < len; i++) {
        binary += String.fromCharCode(bytes[i]);
    }
    return window.btoa( binary );
}

  function loadImage()
  {
    var request = new XMLHttpRequest();
    request.open("GET", "tree.png", true);
    if (lastModified !== '')
      request.setRequestHeader("If-Modified-Since", lastModified);
    request.responseType = 'arraybuffer';
    request.onload = function(/* oEvent */) {
      lastModified = request.getResponseHeader('Last-Modified');
      var response = request.response;
      if (typeof response !== 'undefined' && response.byteLength !== 0) {
        var encoded = _arrayBufferToBase64(response);
        image.src = 'data:image/png;base64,' + encoded;
      }
      window.setTimeout(loadImage, 5000);
    };
    request.send();
  }

  loadImage();
};
</script>
</head>
<body>
  <img id="img">
</body>
</html>


You can write a server side method which just returns last modified date of the image resource,
Then you just use polling to check for the modified date and then reload if modified date is greater than previous modified date.

pseudo code (ASP.NET)

//server side ajax method
[WebMethod]
public static string GetModifiedDate(string resource)
{
    string path = HttpContext.Current.Server.MapPath("~" + resource);
    FileInfo f = new FileInfo(path);
    return f.LastWriteTimeUtc.ToString("yyyy-dd-MMTHH:mm:ss", CultureInfo.InvariantCulture);//2020-05-12T23:50:21
}

var pollingInterval = 5000;
function getPathFromUrl(url) {
    return url.split(/[?#]/)[0];
}
function CheckIfChanged() {
    $(".img").each(function (i, e) {
        var $e = $(e);
        var jqxhr = $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/Default.aspx/GetModifiedDate",
            data: "{'resource':'" + getPathFromUrl($e.attr("src")) + "'}"
        }).done(function (data, textStatus, jqXHR) {
            var dt = jqXHR.responseJSON.d;
            var dtCurrent = $e.attr("data-lastwrite");
            if (dtCurrent) {
                var curDate = new Date(dtCurrent);
                var dtLastWrite = new Date(dt);
                //refresh if modified date is higher than current date
                if (dtLastWrite > curDate) {
                    $e.attr("src", getPathFromUrl($e.attr("src")) + "?d=" + new Date());//fool browser with date querystring to reload image
                }
            }
            $e.attr("data-lastwrite", dt);
        });
    }).promise().done(function () {
        window.setTimeout(CheckIfChanged, pollingInterval);
    });
}
$(document).ready(function () {
    window.setTimeout(CheckIfChanged, pollingInterval);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img class="img" src="/img/rick.png" alt="rick" />

Refresh image with Javascript, but only if changed on server


If you are going to check whether files has changed on the server you have to make http request from the server for the file time, because there is no other way for your check the file time once page get loaded to the browser.

So that time check script will like

filetimecheck.php

<?php
    echo filemtime(string $filename);
?>

Then you can check the file time using your Javascript. BTW I have put jQuery $.get for check the file time.

dusplayimage.php

<img id="badge" src="image.jpg"> />
<script>
var image_time = <?php echo filemtime(string $filename); ?>;
var timerdelay = 5000;
function imageloadFunction(){
    $.get("filetimecheck.php", function(data, status){
        console.log("Data: " + data + "\nStatus: " + status);
        if(image_time < parseInt(data)) {
            document.getElementById('yourimage').src = "image.jpg?random="+new Date().getTime();
        }
      });

    setTimeout(imageloadFunction, timerdelay);
}

imageloadFunction();

</script>

You will be using extra call to the server to check the file time which you can't avoid however you can use the time delay to fine-tune the polling time.


Yes, you can customize this behavior. Even with virtually no change to your client code.

So, you will need a ServiceWorker (caniuse 96.59%).

ServiceWorker can proxy your http requests. Also, ServiceWorker has already built-in storage for the cache. If you have not worked with ServiceWorker, then you need to study it in detail. The idea is the following:

When requesting a picture (in fact, any file), check the cache. If there is no such picture in the cache, send a request and fill the cache storage with the date of the request and the file. If the cache contains the required file, then send only the date and path of the file to the special API to the server. The API returns either the file and modification date at once (if the file was updated), or the response that the file has not changed {"changed": false}. Then, based on the response, the worker either writes a new file to the cache and resolves the request with the new file, or resolves the request with the old file from the cache.

Here is an example code (not working, but for understanding)

s-worker.js

self.addEventListener('fetch', (event) => {
  if (event.request.method !== 'GET') return;

  event.respondWith(
    (async function () {
      const cache = await caches.open('dynamic-v1');
      const cachedResponse = await cache.match(event.request);

      if (cachedResponse) {
        // check if a file on the server has changed
        const isChanged = await fetch('...');
        if (isChanged) {
          // give file, and in the background write to the cache
        } else {
          // return data
        }
        return cachedResponse;
      } else {
        // request data, send from the worker and write to the cache in the background
      }
    })()
  );
});

In any case, look for "ways to cache statics using ServiceWorker" and change the examples for yourself.


WARNING this solution is like taking a hammer to crush a fly

You can use sockets.io to pull information to browser.

In this case you need to monitor image file changes on the server side, and then if change occur emit an event to indicate the file change.

On client (browser) side listen to the event and then then refresh image each time you get the event.


set your image source in a data-src property,

and use javascript to periodicaly set it to the src attribute of that image with a anchor (#) the anchor tag in the url isn't send to the server.

Your webserver (apache / nginx) should respond with a HTTP 304 if the image wasn't changed, or a 200 OK with the new image in the body, if it was

setInterval(function(){ 
  l= document.getElementById('logo'); 
  l.src = l.dataset.src+'#'+ new Date().getTime();
  },1000);
 <img id="logo" alt="awesome-logo" data-src="https://upload.wikimedia.org/wikipedia/commons/1/11/Test-Logo.svg" />


EDIT

Crhome ignores http cache-control headers, for subsequent image reloads.

but the fetch api woks as expected

fetch('https://upload.wikimedia.org/wikipedia/commons/1/11/Test-Logo.svg', { cache: "no-cache" }).then(console.log);

the no-cache instructs the browser to always revalidate with the server, and if the server responds with 304, use the local cached version.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜