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.

native

On the server

main app.Route · app.Handler
→
app.Handler prerenders the route
→
HTML page markup · app.js · manifest
wasm

In the browser

prerendered markup shown at once
app.wasm loads →
RunWhenOnBrowser mounts the routed component
→
live tree a single-page app from here
One main, built twice: the server answers with prerendered HTML and the runtime files, then app.wasm takes over 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.css and to the handler's Styles (see Handler and Build );
  • wasm_exec.js , Go's WebAssembly support file, and app.js , which downloads app.wasm and registers the service worker app-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.

01