1 WebAssembly
Virgil edited this page 2026-02-19 16:42:07 +00:00

WebAssembly

go-html compiles to WebAssembly for browser-side rendering, exposing a window.gohtml JavaScript API.

See also: Home | Architecture | Node-API | Pipeline | Code-Generation

Build

make wasm
# GOOS=js GOARCH=wasm go build -ldflags="-s -w" -o dist/gohtml.wasm ./cmd/wasm/

Size Budget

Metric Limit Notes
Raw .wasm 3 MB Go WASM runtime is ~2 MB floor
Gzip transfer 1 MB CDN/browser transfer size

The Makefile enforces these limits and fails the build if exceeded.

JavaScript API

Load the WASM module, then use window.gohtml:

<script src="wasm_exec.js"></script>
<script>
const go = new Go();
WebAssembly.instantiateStreaming(fetch('gohtml.wasm'), go.importObject)
  .then(result => go.run(result.instance));
</script>

renderToString

const html = gohtml.renderToString(variant, locale, slots)
Parameter Type Description
variant string Layout variant: "HLCRF", "HCF", "C", etc.
locale string Language code for i18n: "en", "fr", etc.
slots object Content per region: {H: "...", C: "...", F: "..."}

Returns a complete HTML string with semantic landmarks and data-block IDs.

registerComponents

gohtml.registerComponents(slotsJSON)

Takes a JSON string mapping slot names to custom element tags. Generates Web Component classes and registers them in the browser's custom element registry.

gohtml.registerComponents(JSON.stringify({
  "H": "app-header",
  "C": "app-content",
  "F": "app-footer"
}))
// Registers <app-header>, <app-content>, <app-footer>

WASM Entrypoint

cmd/wasm/main.go — Binds Go functions to window.gohtml via js.Global().Set().

cmd/wasm/register.go — Pure-Go Web Component bundle builder. Testable without WASM (used by register_test.go). Generates JavaScript class definitions and customElements.define() calls.

Deployment

The WASM binary is designed for CDN deployment:

  1. Build: make wasm
  2. Copy dist/gohtml.wasm + Go's wasm_exec.js to CDN
  3. Load in HTML with standard Go WASM bootstrap
  4. Call gohtml.renderToString() for server-side-rendering parity in the browser

The same layout definitions work in Go (server) and WASM (browser), ensuring consistent rendering across environments.