reload

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2025 License: MIT Imports: 12 Imported by: 0 modules, 1 package

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

reload

Auto-reload browser when assets change

How to use

  1. Embed the JS script into your index.html or equivalent, for example, using Go templates:

    const templateName = "index.html"
    var indexTemplate = template.Must(template.New(templateName).Parse(
    	`<!doctype html>
    <html lang="en" class="h-full bg-gray-100">
    <head>
       <meta charset="UTF-8" />
       <meta name="viewport" content="width=device-width, initial-scale=1.0" />
       <meta http-equiv="X-UA-Compatible" content="ie=edge" />
       <title>My Website</title>
       <link rel="stylesheet" href="index.css" />
       <link rel="icon" href="favicon.svg" />
       <script src="index.js" defer></script>
    </head>
    <body class="h-full">
       <div id="root" class="h-full"></div>
    </body>
    {{ .ReloadScript }}
    </html>
    `))
    func ServeIndexHTML(w http.ResponseWriter, r *http.Request) {
    	indexTemplate.ExecuteTemplate(w, templateName, map[string]any{
    		"ReloadScript": reload.Script,
    	})
    }
    
  2. Wrap your normal HTML handler in the middleware:

    var srvHandler http.Handler // Handler to serve your HTML
    var log *slog.Logger // Your slog logger
    srvHandler, err = reload.NewMiddleware(
    	// Pass in the old HTML handler.
    	srvHandler,
    	// Pass in the directory you want to watch.
    	// When a file in this directory changes, the browser will reload.
    	serveDir,
    	// Optionally, pass in a logger to be notified of detected file changes.
    	log,
    )
    if err != nil {
    	return fmt.Errorf("failed to create reload middleware: %w", err)
    }
    

For a full example of this in use, see https://github.com/catzkorn/trail-tools.

Inspiration

Greatly based on https://github.com/aarol/reload, but with some different dependencies and design choices.

Documentation

Index

Constants

View Source
const Script = `` /* 484-byte string literal not displayed */

Script is the JavaScript snippet that enables auto-reloading in the browser. Embed this into your index.html or equivalent. Heavily inspired by https://github.com/aarol/reload, licensed under MIT.

Variables

This section is empty.

Functions

func NewMiddleware

func NewMiddleware(next http.Handler, root string, log *slog.Logger) (*autoReloader, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

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