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.
Injectadds the script to a response; never call it on files you deploy. - One answer per request.
Handlerwaits 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.
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
endServing 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.
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)
endFunctions
Init
func Init(epoch: str, err_file: str)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()
endSetLang
func SetLang(code: str)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(): strSetLang, 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)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(): boolSALAM_DEV_HMR is set, and reads the error file path from SALAM_DEV_HMR_ERR. Returns whether it is on.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)
endEnabled
func Enabled(): boolThe example for this item is shown with the ones above: net-devreload-Basics.salam
Bump
func Bump(kind: int)The example for this item is shown with the ones above: net-devreload-Basics.salam
Script
func Script(): strThe example for this item is shown with the ones above: net-devreload-Basics.salam
Inject
func Inject(html: str): str</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)/__salam_hmr: answers with one server sent event.The example for this item is shown with the ones above: net-devreload-Server.salam