25 lines
1.3 KiB
JavaScript
25 lines
1.3 KiB
JavaScript
// bootstrap.js — boots the Go/Wasm client, and supports flash-free hot swaps.
|
|
//
|
|
// On first load the server has already rendered the page's HTML into #app and
|
|
// the wasm app hydrates it (see wasm/main.go). The dev server's livereload
|
|
// script hot-swaps a freshly built module WITHOUT a full page reload or a blank
|
|
// flash: it calls __gowasmPrepare() to fetch + compile the new module while the
|
|
// current page is still visible, then (in one synchronous step) __gowasmDispose()
|
|
// to tear down the old instance and start() to run the new one.
|
|
(function () {
|
|
// prepare fetches + compiles the module and returns a SYNCHRONOUS start()
|
|
// thunk. Separating the async work (network + compile) from start (which
|
|
// renders synchronously) is what lets a swap avoid an intermediate blank #app.
|
|
async function prepare() {
|
|
const go = new Go();
|
|
// cache:no-store so a hot swap always fetches the freshly built bytes.
|
|
const resp = await fetch("/app.wasm", { cache: "no-store" });
|
|
const result = await WebAssembly.instantiateStreaming(resp, go.importObject);
|
|
return function start() { go.run(result.instance); }; // runs main() (renders), then parks on select{}
|
|
}
|
|
async function boot() { (await prepare())(); }
|
|
window.__gowasmPrepare = prepare;
|
|
window.__gowasmBoot = boot;
|
|
boot();
|
|
})();
|