livereload

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: May 26, 2025 License: MIT Imports: 10 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

livereload

Go Reference

Live-reload middleware for your server. Meant to be drop-n-go, so it's fairly opinionated. Built on top of the lower-level sse package.

Features

  • Minimal API for serving, watching and live reloading.
  • Customize how livereload behaves a custom event listener.
  • Detects the content type and ignores non-HTML files.
  • Robust file watching built on top of fsnotify.

Install

go get github.com/matthewmueller/livereload

Usage

func main() {
  ctx := context.Background()
  fsys := http.FileServer(http.Dir("example/public"))
  fmt.Println("Server started at http://localhost:3000")
  lr := livereload.New(slog.Default())
  go lr.Watch(ctx, ".")
  http.ListenAndServe(":3000", lr.Middleware(fsys))
}
Customize live-reload behavior

By default, any HTML or text document will be reloaded upon change. If you'd like to better control this behavior, you can implement your own reload event handler:

<!DOCTYPE html>
<html lang="en">
  <head> </head>
  <body>
    <h1>Hello</h1>
  </body>
  <script>
    window.addEventListener("reload", (e) => {
      // Prevent the default livereload handler from running
      e.stopImmediatePropagation()
      // Listen all the events the watcher discovered
      const events = e.detail.events || []
      for (let event of events) {
        console.log(event.op, event.path)
      }
    })
  </script>
</html>
Publishing your own events

You can publish your own events with lr.Publish, for example:

watcher.Watch(ctx, "example", func(path string) error {
  return lr.Publish(ctx, &livereload.Event{
    Type: "reload",
    Data: []byte(path),
  })
})

This will trigger the reload event on the client-side.

Contributors

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event added in v0.0.3

type Event = sse.Event

Event is an server-sent event (SSE) that you can send to the browser

type Reloader

type Reloader struct {
	Path string
	// contains filtered or unexported fields
}

func New

func New(log *slog.Logger) *Reloader

func (*Reloader) ListenAndServe added in v0.0.6

func (r *Reloader) ListenAndServe(ctx context.Context, addr string) error

ListenAndServe serves the client and the server-sent events at the given address. Unlike Middleware, it's meant to be used as a standalone server.

func (*Reloader) Middleware

func (r *Reloader) Middleware(next http.Handler) http.Handler

Middleware that rewrites the response body to include the livereload script for HTML or plain text responses. It also serves the livereload server-sent events at the given path. Unlike ListenAndServe, this is meant to be embedded into an existing HTTP server.

func (*Reloader) Publish added in v0.0.3

func (r *Reloader) Publish(ctx context.Context, event *Event) error

Reload sends a message to browser with the given event. Event data should follow the format "op:path;op:path" format in Watch.

func (*Reloader) Watch

func (r *Reloader) Watch(ctx context.Context, watchDir string) error

Watch a directory for changes and send the events to the browser

Directories

Path Synopsis
example
embedded command
external command
external/watch command

Jump to

Keyboard shortcuts

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