kaazing / http2-cache.js

2 stars 11 forks source link

XHR event for when response get updated #109

Open hthetiot opened 6 years ago

hthetiot commented 6 years ago

Currently we use an event 'push' internally to update http2-cache Configuration.cache to last response and app need to perform new XHR to get updated content from cache.

I know that the clean way would be to shim SSE on http2-cache.js but that would required developer to change from XHR to SSE on their application also.

That why this issue is more for addressing the issue in term of XHR use case.

Note: the future way would be to have a http2-cache.js in pair with ServiceWorker and use push event

Note2: Another alternative would be to implement Fetch and Stream

fetch("https://www.example.org/").then((response) => {
  const reader = response.body.getReader();
  const stream = new ReadableStream({
    start(controller) {
      // The following function handles each data chunk
      function push() {
        // "done" is a Boolean and value a "Uint8Array"
        reader.read().then(({ done, value }) => {
          // Is there no more data to read?
          if (done) {
            // Tell the browser that we have finished sending data
            controller.close();
            return;
          }

          // Get the data and send it to the browser via the controller
          controller.enqueue(value);
          push();
        });
      };

      push();
    }
  });

  return new Response(stream, { headers: { "Content-Type": "text/html" } });
});

Note3: SSE ref https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events

hthetiot commented 6 years ago

Possible solutions: