stew/* Packages / 14.1

📦 stew/data

The stew/data package provides access to state data sent by the server during the initial page rendering.

Global data Structure

When you import this package, a global data variable is automatically injected into your client script. It corresponds to the WasmPageData struct:

<goscript client>
type WasmPageData struct {
    URL    string              // Initial URL path
    Params map[string]string   // Dynamic path parameters
    Query  map[string][]string // Search parameters
    Store  map[string]any      // Custom data from handlers
}
</goscript>
data.URL string
The pathname of the URL at the time of server rendering (e.g., /users/42).
data.Params map[string]string
Contains dynamic parameters captured in the path (e.g., id).
data.Query map[string][]string
The decoded search parameters (query string).
data.Store map[string]any
Data manually injected by the server via handlers.PageData.

Usage Example

<goscript client>
    import "stew/data"
    import "stew/io"

    onInit := func() {
        userID := data.Params["id"]
        source := data.Query["ref"][0]
        
        Console.Log("User " + userID + " coming from: " + source)
        
        if val, ok := data.Store["is_admin"]; ok && val.(bool) {
             Alert("Administrator access detected!")
        }
    }
</goscript>
⚖️ Binary Weight

Importing this package includes JSON handling (encoding/json) in the Wasm bundle, adding approximately 200 to 300 KB. Use it only if accessing server state is indispensable.