Docs/Advanced Features/Streaming SSR & Suspense

Streaming SSR & Suspense

Stream HTML shells immediately while slow asynchronous database queries resolve in parallel goroutines.

Traditional server-side rendering (SSR) is blocked until the slowest database query or 3rd party API call completes. GoKS solves this withStreaming SSRandcomponent.Suspense.

How Streaming Works in GoKS

  1. The server sends the primary HTML shell and loading skeleton fallbacks immediately overchunked HTTP transfer encoding.
  2. Slow asynchronous blocks execute in parallel in independent Go goroutines.
  3. When each goroutine finishes, GoKS streams the resolved HTML chunk along with a zero-dependency inline<template>script that replaces the skeleton DOM node in place.

Suspense Component Example

app/dashboard/page.gox
go
package dashboard

import (
	"time"
	"github.com/misbakhul29/goks/pkg/component"
	"github.com/misbakhul29/goks/pkg/html"
)

type Page struct {
	component.ComponentBase
}

func (p *Page) Render() *component.Node {
	return html.Div(html.H1("User Analytics").Class("text-3xl font-bold text-white mb-6"), "{component.Suspense(\n\t\t\t\t&component.SuspenseConfig{\n\t\t\t\t\tFallback: (", html.Div("Loading real-time revenue metrics...").Class("p-6 rounded-xl bg-slate-900 animate-pulse text-slate-500"), "),\n\t\t\t\t\tContent: func() *component.Node {\n\t\t\t\t\t\ttime.Sleep(500 * time.Millisecond) // Simulated DB latency\n\t\t\t\t\t\treturn (", html.Div("Monthly Revenue: $128,400.00 (+14.2%)").Class("p-6 rounded-xl bg-emerald-950/40 border border-emerald-500/30 text-emerald-300"), ")\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t)}").Class("p-8")
}

Key Benefits

  • Sub-Millisecond TTFB:The initial document shell flushes to the browser almost instantly.
  • Zero Client JS Required:Out-of-order template swapping uses a tiny inline DOM replacement script (under 100 bytes) without requiring WebAssembly or virtual DOM.
  • SEO Friendly:Search engine crawlers supporting streaming responses receive the fully resolved document without timeouts.
Powered byGoKS WASM16 topics