hotreload

package
v0.0.0-...-c6b3ac7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 6, 2021 License: ISC Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// EndpointPath is a path to websocket endpoint
	EndpointPath = "/.gostatic.hotreload"
	// ScriptPath is a path to hotreload support js
	ScriptPath = "/.gostatic.hotreload.js"
)

Variables

View Source
var Script = []byte(`
(function() {
  if (!('WebSocket' in window)) return;

  var ws = new WebSocket(((window.location.protocol === "https:") ? "wss://" : "ws://") +
                         window.location.host +
                         "/.gostatic.hotreload");

  ws.onmessage = function(e) {
    localStorage.hotreloaddebug && console.log(e.data);
    enqueue(e.data);
  };

  window.addEventListener('beforeunload', function(e) {
    ws.close();
  });

  var MODES = new Set();
  var timeout, timeoutS;

  function enqueue(mode) {
    MODES.add(mode);
    if (!timeout) timeoutS = 32;
    if (timeout) {
      clearTimeout(timeout);
      timeoutS = Math.min(timeoutS * 2, 1000);
    }
    timeout = setTimeout(hotreload, timeoutS);
  }
  function hotreload() {
    localStorage.hotreloaddebug && console.log('reload', MODES);
    MODES.forEach(mode => RELOADERS[mode]());
    MODES = new Set();
    timeout = null;
  }

  var RELOADERS = {
    page: function reloadpage() {
      fetch(window.location.href,
            {mode:    'same-origin',
             headers: {'X-With': 'hotreload'}})
        .then(res => res.text())
        .then(text => {
          document.documentElement.innerHTML = text;
          var e = new Event('load', {'bubbles': true});
          window.dispatchEvent(e);
        })
        .catch(e => {
          if (e.message != "The operation was aborted. ") {
            console.log(e);
          }
        });
    },
    css: function reloadcss() {
      // This snippet pinched from quickreload, under the MIT license:
      // https://github.com/bjoerge/quickreload/blob/master/client.js
      var killcache = '__gostatic=' + new Date().getTime();
      var stylesheets = Array.prototype.slice.call(
        document.querySelectorAll('link[rel="stylesheet"]')
      );
      stylesheets.forEach(function (el) {
        var href = el.href.replace(/(&|\?)__gostatic\=\d+/, '');
        el.href = '';
        el.href = href + (href.indexOf("?") == -1 ? '?' : '&') + killcache;
      });
    }};
})();
`)

Script is a JS to connect to websocket and hot reload stuff synchronized by hand from assets/hotreload.js

Functions

func ServeHTTP

func ServeHTTP(source, port string, hotreload bool) error

ServeHTTP serves files from `dir` and adds hotreload support to html files

func Watch

func Watch(dirs, files []string, callback func()) error

Watch starts file watcher

Types

type Injector

type Injector struct {
	http.ResponseWriter
}

func (*Injector) Write

func (inj *Injector) Write(data []byte) (int, error)

func (*Injector) WriteHeader

func (inj *Injector) WriteHeader(statusCode int)

type Server

type Server struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewServer

func NewServer(path string) (*Server, error)

func (*Server) Upgrade

func (s *Server) Upgrade(w http.ResponseWriter, r *http.Request)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL