Components
A component is a Go struct that embeds `app.Compo` and returns its markup from `Render`.
This page covers the
app
package's component model: how a WebGo program runs on the server and in the browser, how a component describes its markup, how it reacts to events, and when it renders again. This is go-app's component model, now WebGo's
app
package.
Choosing which component a URL shows is on the
Routing
page.
app.Screen
,
app.Shell
and
app.Model
, built on these components, are on the
Pages, layouts and state
page.
§1 Architecture
A WebGo app is one Go program built twice: once for the server, and once with
GOOS=js GOARCH=wasm
into
web/app.wasm
for the browser. The same
main
runs in both.
app.IsServer
and
app.IsClient
report which build is running.
On the server
In the browser
The server
The server build serves an
app.Handler
over
net/http
, or writes the same files to disk with
app.GenerateStaticWebsite
. For every route, the handler answers with an HTML page that contains:
- the markup of the routed component, prerendered on the server;
-
links to
app.cssand to the handler'sStyles(see Handler and Build ); -
wasm_exec.js, Go's WebAssembly support file, andapp.js, which downloadsapp.wasmand registers the service workerapp-worker.js; -
manifest.webmanifest, which makes the app installable.
Files under
/web/
(
app.wasm
, images, stylesheets) are static resources. The handler's
Resources
field decides where they are served from: a local directory by default, a bucket with
app.RemoteBucket
, or a prefixed path with
app.GitHubPages
.
The browser
The browser shows the prerendered markup at once. When
app.wasm
has loaded,
app.RunWhenOnBrowser
creates the component routed to the current URL, mounts it, and replaces the prerendered markup with the live tree. From then on the app is a single-page application: link clicks and back/forward are handled in Go without a page load.
func
RunWhenOnBrowser
(
)
On the server,
RunWhenOnBrowser
returns immediately, so the code after it (starting the handler) runs. In the browser it never returns. Routes must be registered before it is called, and they must be registered on both sides, because the server needs them to prerender.