Scripts

Load ES modules through one importmap and reach them from Go through a typed wrapper.

An ES module is a JavaScript file that other code loads by name. Handler.Imports lists the modules a page needs. The handler writes one importmap for them, the browser loads them while app.wasm downloads, and Go code calls them through a typed wrapper from the script package.

Classic scripts, the kind that set globals, stay on Handler.Scripts , covered under Head resources .

ยง1 Declaring a module

                  
                    Imports
                     
                    [
                    ]
                    Module
                    


                    type
                     
                    Module
                     
                    interface
                     
                    {
                    
	
                    Source
                    (
                    )
                     
                    ModuleSource
                    

                    }
                    


                    type
                     
                    ModuleSource
                     
                    interface
                     
                    {
                    
	
                    ImportMap
                    (
                    )
                     
                    (
                    name
                    ,
                     
                    url
                     
                    string
                    )
                    

                    }
                  
                

A Module returns a ModuleSource , which gives the name the module is imported by and the URL it is served from. The script package builds the sources, and a wrapper type ties one to the Go code that calls it. This one loads Datadog's browser SDK. Init runs once and configures the module from it:

                  
                    type
                     
                    RUM
                     
                    struct
                    {
                     
                    ClientToken
                    ,
                     
                    AppID
                     
                    string
                     
                    }
                    


                    func
                     
                    (
                    RUM
                    )
                     
                    Source
                    (
                    )
                     
                    app
                    .
                    ModuleSource
                     
                    {
                     
                    return
                     
                    script
                    .
                    CDN
                    (
                    "npm"
                    ,
                     
                    "@datadog/browser-rum"
                    ,
                     
                    "5.20.0"
                    )
                     
                    }
                    


                    func
                     
                    (
                    r
                     
                    RUM
                    )
                     
                    Init
                    (
                    ctx
                     
                    app
                    .
                    Context
                    ,
                     
                    mod
                     
                    app
                    .
                    Value
                    )
                     
                    app
                    .
                    Value
                     
                    {
                    
	
                    rum
                     
                    :=
                     
                    mod
                    .
                    Get
                    (
                    "datadogRum"
                    )
                    
	
                    rum
                    .
                    Call
                    (
                    "init"
                    ,
                     
                    map
                    [
                    string
                    ]
                    any
                    {
                    "clientToken"
                    :
                     
                    r
                    .
                    ClientToken
                    ,
                     
                    "applicationId"
                    :
                     
                    r
                    .
                    AppID
                    }
                    )
                    
	
                    return
                     
                    rum
                    

                    }
                    


                    var
                     
                    rum
                     
                    =
                     
                    RUM
                    {
                    ClientToken
                    :
                     
                    "..."
                    ,
                     
                    AppID
                    :
                     
                    "..."
                    }
                    


                    // Handler
                    

                    Imports
                    :
                     
                    [
                    ]
                    app
                    .
                    Module
                    {
                    rum
                    }
                    


                    // a component
                    

                    if
                     
                    r
                    ,
                     
                    ok
                     
                    :=
                     
                    script
                    .
                    Use
                    (
                    ctx
                    ,
                     
                    rum
                    )
                    ;
                     
                    ok
                     
                    {
                    
	
                    r
                    .
                    Call
                    (
                    "addAction"
                    ,
                     
                    "clicked"
                    )
                    

                    }
                  
                

Declare the value once, as the package-level rum , and use it in both places. Init runs with whichever value reaches Use first, so a component that wrote RUM{} would configure Datadog with empty credentials.

Build checks the imports and returns one of these errors:

  • ErrMissingModule : a local module file is not under WebDir .
  • ErrDuplicateModule : two modules have the same name.
  • ErrInvalidModule : a module has no source or no name, or its source failed its own check, such as a CDN version that is not exact.
01