Sign in

    #Stella

    Shard generation module for Island embedding.

    #Overview

    A Shard is a hydratable HTML fragment in Island Architecture. Marked with luna:* attributes, the loader detects and hydrates them on the client.

    #File Structure

    FileResponsibility
    types.mbtShard config and output type definitions
    html_builder.mbtHTML generation utilities
    serializer.mbtJSON/HTML escape processing

    #Shard Output Example

    <div luna:id="counter" luna:url="/static/counter.js" luna:state='{"count":0}' luna:client-trigger="load"> <!-- SSR content --> </div>

    #Main Types

    #ShardConfig

    pub struct ShardConfig {
    id : String // Component ID (luna:id)
    script_url : String // Hydration script (luna:url)
    trigger : TriggerType // Trigger (luna:client-trigger)
    state : StateConfig // State config
    ssr_content : String? // SSR HTML
    include_loader : Bool // Embed loader
    loader_url : String // Loader URL
    }

    #StateConfig

    pub enum StateConfig {
    Empty // No state
    Inline(String) // Embed in luna:state attribute
    ScriptRef(String) // Reference to <script id="...">
    Url(String) // Fetch from external URL
    }

    #Security

    • XSS escape processing in serializer.mbt
    • </script> in JSON is escaped
    • HTML attribute values are properly quoted

    #References

    • Luna Core - TriggerType definition
    • Sol - Island integration implementation

    ShardConfig

    pub struct ShardConfig {
    id : String
    script_url : String
    trigger :
    TriggerType

    state : StateConfig
    ssr_content : String?
    include_loader : Bool
    loader_url : String
    }

    Configuration for generating a shard snippet

    ShardConfig::new

    fn ShardConfig::new(id : String, script_url : String) -> ShardConfig

    Create a minimal shard config

    ShardConfig::with_default_loader

    fn ShardConfig::with_default_loader(self : ShardConfig) -> ShardConfig

    Builder pattern: include loader script with default URL

    ShardConfig::with_loader

    fn ShardConfig::with_loader(self : ShardConfig, url : String) -> ShardConfig

    Builder pattern: include loader script

    ShardConfig::with_ssr_content

    fn ShardConfig::with_ssr_content(self : ShardConfig, content : String) -> ShardConfig

    Builder pattern: set SSR content

    ShardConfig::with_state

    fn ShardConfig::with_state(self : ShardConfig, json : String) -> ShardConfig

    Builder pattern: set inline state from JSON string

    ShardConfig::with_state_ref

    fn ShardConfig::with_state_ref(self : ShardConfig, script_id : String) -> ShardConfig

    Builder pattern: set state from script reference

    ShardConfig::with_state_url

    fn ShardConfig::with_state_url(self : ShardConfig, url : String) -> ShardConfig

    Builder pattern: set state from URL

    ShardConfig::with_trigger

    Builder pattern: set trigger

    ShardOutput

    pub struct ShardOutput {
    html : String
    head_scripts : Array[String]
    state_scripts : Array[String]
    }

    Output from shard generation

    StateConfig

    pub(all) enum StateConfig {
    Empty
    Inline(String)
    ScriptRef(String)
    Url(String)
    }

    How state is provided

    auto_select_state_format

    fn auto_select_state_format(json : String, base_config : ShardConfig) -> ShardConfig

    Estimate the recommended state format based on JSON size

    default_loader_url

    let default_loader_url : String

    Default loader URL

    format_state_attr

    fn format_state_attr(state : StateConfig) -> String?

    Format state for luna:state attribute Returns the attribute value based on StateConfig

    generate_lazy_shard

    fn generate_lazy_shard(id : String, script_url : String, state_json : String, ssr_content : String) -> String

    Generate shard with visible trigger

    generate_minimal_shard

    fn generate_minimal_shard(id : String, script_url : String, state_json : String, ssr_content : String) -> String

    Generate minimal shard (no loader, inline state)

    generate_shard

    fn generate_shard(config : ShardConfig) -> ShardOutput

    Generate the complete shard HTML

    generate_shard_group

    fn generate_shard_group(loader_url : String, configs : Array[ShardConfig]) -> String

    Generate multiple shards that share a single loader

    generate_shard_with_state_script

    fn generate_shard_with_state_script(config : ShardConfig, state_json : String) -> ShardOutput

    Generate shard with automatic state script Use this when you have large state that should be in a separate script tag

    generate_standalone_shard

    fn generate_standalone_shard(id : String, script_url : String, loader_url : String, state_json : String, ssr_content : String) -> String

    Generate standalone shard (with loader)

    generate_state_script

    fn generate_state_script(id : String, json : String) -> String

    Generate a element for state Used when state is too large for inline attribute

    recommend_state_format

    fn recommend_state_format(json_size : Int) -> String

    Recommended state format based on size
    • < 1KB: Inline in attribute
    • 1KB - 100KB: Script reference
    • 100KB: URL reference (needs server support)

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io