Module: Roda::RodaPlugins::LiveReload::ResponseMethods

Defined in:
lib/roda/plugins/live_reload.rb

Overview

:nodoc:

Constant Summary collapse

INJECT =

:nodoc:

<<~EOM # :nodoc:
<script>
  (function reconnect() {
    var xhr = new XMLHttpRequest();

    xhr.open("GET", "/_live_reload", true);

    xhr.onprogress = function() {
      window.location.reload();
    };

    xhr.onerror = function() {
      console.log("Reconnecting after error");
      setTimeout(reconnect, 1000);
    };

    xhr.onabort = function() {
      console.log("Reconnecting after abort");
      setTimeout(reconnect, 1000);
    };

    xhr.send();
  })();
</script>

Instance Method Summary collapse

Instance Method Details

#finishObject

:nodoc:



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/roda/plugins/live_reload.rb', line 48

def finish # :nodoc:
  status, headers, content = super

  content = content.map do |chunk|
    if chunk.include?("</head>")
      chunk.sub("</head>", INJECT + "</head>")
    else
      chunk
    end
  end

  headers["Content-Length"] = content.reduce(0) { |memo, chunk| memo + chunk.bytesize }.to_s

  [status, headers, content]
end