Standard library · Network

net.devreload

The event stream and browser script that reload a page while you develop it.

import net.devreload

Dev reload tells an open browser tab that the page has changed. The server keeps one endpoint, /__salam_hmr, and the tab listens to it with EventSource. Each answer is one event: swap (replace the app code in place and keep its state), css (reload the style sheets only), style (replace the inline styles and nothing else), reload, or error with the build log, which the script shows over the page.

salam serve uses this package for pages and salam serve --render uses it inside the generated server. You only need it directly when you write your own development server.

What trips people up

  • Development only. Inject adds the script to a response; never call it on files you deploy.
  • One answer per request. Handler waits up to 15 seconds for a change and answers once; the browser reconnects by itself and sends the id of the last event it saw.
  • A new instance reloads every tab. The id carries the epoch given to Init, so a restarted server makes open tabs reload.

Starting the state and injecting the script

Init or InitLocal starts the state. Bump then announces each change, and Inject adds the script to any page you serve. The program below runs these calls without a server.

net-devreload-guide-basics.salam
import net.devreload

func main:
    println devreload.Enabled()
    devreload.InitLocal("run1", "")
    println devreload.Enabled()
    devreload.Bump(1)
    page := devreload.Inject("<html><head></head><body><h1>Hi</h1></body></html>")
    println page.Len() > 1000
    println devreload.Script().Len() > 500
end
Outputfalse true true true

Serving the event endpoint

A development server registers Handler under /__salam_hmr and starts only when InitFromEnv says dev reload is on, so a release run is not affected.

net-devreload-guide-server.salam
import net.devreload
import net.http
import net.router

func main:
    if not devreload.InitFromEnv():
        println "set SALAM_DEV_HMR=1 to turn dev reload on"
        ret 0
    end
    mut r := router.NewRouter()
    router.Get(r, "/__salam_hmr", devreload.Handler)
    mut s := http.NewServer(8080)
    http.Use(s, r)
    http.Run(s)
end

Functions

Init

func Init(epoch: str, err_file: str)
Starts the state with an epoch (any non-empty text without a colon that differs between server runs; colons are replaced with dashes) and the path of a file whose contents are the current build error, or an empty path.
net-devreload-Basics.salam
import net.devreload

func main:
    println devreload.Enabled()
    devreload.InitLocal("run1", "")
    println devreload.Enabled()
    devreload.Bump(1)
    page := devreload.Inject("<html><head></head><body><h1>Hi</h1></body></html>")
    println page.Len() > 1000
    println devreload.Script().Len() > 500
    devreload.SetLang("fa")
    println devreload.Lang()
end
Outputfalse true true true fa

SetLang

func SetLang(code: str)
Sets the language of the overlay and its messages: fa (right to left) or en (left to right); anything else means en. Every event carries it.

The example for this item is shown with the ones above: net-devreload-Basics.salam

Lang

func Lang(): str
The language set with SetLang, en by default.

The example for this item is shown with the ones above: net-devreload-Basics.salam

InitLocal

func InitLocal(epoch: str, err_file: str)
Like Init, but a change is announced only by Bump; use it when the builder lives in the same process as the server.

The example for this item is shown with the ones above: net-devreload-Basics.salam

InitFromEnv

func InitFromEnv(): bool
Turns dev reload on when the environment variable SALAM_DEV_HMR is set, and reads the error file path from SALAM_DEV_HMR_ERR. Returns whether it is on.
net-devreload-Server.salam
import net.devreload
import net.http
import net.router

func main:
    if not devreload.InitFromEnv():
        println "set SALAM_DEV_HMR=1 to turn dev reload on"
        ret 0
    end
    mut r := router.NewRouter()
    router.Get(r, "/__salam_hmr", devreload.Handler)
    mut s := http.NewServer(8080)
    http.Use(s, r)
    http.Run(s)
end

Enabled

func Enabled(): bool
Whether dev reload has been started.

The example for this item is shown with the ones above: net-devreload-Basics.salam

Bump

func Bump(kind: int)
Announces a change: 0 reloads the page, 1 reloads only the style sheets, 2 asks the page to swap its code in place, 3 asks it to replace only its styles.

The example for this item is shown with the ones above: net-devreload-Basics.salam

Script

func Script(): str
The browser script, with its start up code, as HTML.

The example for this item is shown with the ones above: net-devreload-Basics.salam

Inject

func Inject(html: str): str
The HTML with the script added before </body> and a small recorder after <head>.

The example for this item is shown with the ones above: net-devreload-Basics.salam

Handler

func Handler(ctx: i64)
The request handler for /__salam_hmr: answers with one server sent event.

The example for this item is shown with the ones above: net-devreload-Server.salam