goreload

package module
v0.0.0-...-560d00e Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

README

goreload

A simple go library to add simple auto-reload capabilities to your Go web applications during development.

Usage

Check out the example.

Credit

Credit goes to @Cees-Kettenis for the original implementation in #19

License

This project is licensed under the Apache License 2.0.

Contributing

Contributions are welcome, but for bigger changes, please open an issue first to discuss what you would like to change.

Contact

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Logger is the logger used by the notifier.
	Logger *slog.Logger
	// Route is the HTTP route where the live reload handler is mounted.
	Route string
	// Enabled indicates whether live reload is enabled.
	Enabled bool
	// MaxAge is the maximum age for cached assets when live reload is enabled.
	MaxAge time.Duration
}

type Reloader

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

Reloader implements a live reload notifier that broadcasts reload signals to subscribed clients.

func New

func New(config Config) *Reloader

New creates a new Reloader with the provided configuration.

func (*Reloader) CacheMiddleware

func (r *Reloader) CacheMiddleware(handler http.Handler) http.Handler

CacheMiddleware is a middleware that sets Cache-Control headers to enable caching for the specified max age.

func (*Reloader) Close

func (r *Reloader) Close()

Close tears down the notifier and closes every subscriber channel, signalling to callers that no further reload events will arrive.

func (*Reloader) Enabled

func (r *Reloader) Enabled() bool

Enabled returns whether the notifier is currently enabled.

func (*Reloader) Handler

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

Handler streams server-sent events that instruct the browser to refresh whenever the dev watcher picks up a change on disk. The SSE connection stays open until the client disconnects or the server shuts down.

func (*Reloader) MustParseTemplate

func (r *Reloader) MustParseTemplate(t *template.Template) *template.Template

MustParseTemplate is like ParseTemplate but panics on error.

func (*Reloader) Notify

func (r *Reloader) Notify()

Notify broadcasts a reload signal to every active listener without blocking on slow readers. If a listener already has a pending notification we leave it untouched so it still reloads on its next poll.

func (*Reloader) ParseTemplate

func (r *Reloader) ParseTemplate(t *template.Template) (*template.Template, error)

ParseTemplate parses the reload template into the provided template.

func (*Reloader) SetEnabled

func (r *Reloader) SetEnabled(enabled bool)

SetEnabled sets whether the notifier is currently enabled.

func (*Reloader) Start

func (r *Reloader) Start(dir fs.FS)

Start begins polling the on-disk copy of server/web for changes. Any time the directory fingerprint flips we notify all reload subscribers via the provided notifier. The returned cancel function stops the watcher.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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