Skip to content

k.site.scripts ​

Create, read, update, delete, resolve route URLs, and inspect version logs for site JavaScript resources

Execution ContextPrerequisitesTypical Use
KScript API / Server scriptsCurrent site context; sidebar item "js" corresponds to this API (distinct from server k.site.codes)Manage standalone routable .js browser script files, update script bodies, and view change logs

Quick Start ​

Create a standalone JS asset with a registered URL route and verify it via getByUrl() and getUrl():

js
k.api.post(() => {
    const stamp = Date.now().toString()
    const name = "ai-script-" + stamp
    const url = "/ai-script-" + stamp + ".js"

    k.site.scripts.add({
        name,
        url,
        body: `console.log("ai-script-${stamp}");`
    })

    const script = k.site.scripts.getByUrl(url)
    return {
        verified: !!script && script.name === name,
        id: script?.id,
        route: script ? k.site.scripts.getUrl(script.id) : null
    }
})

Quick Reference ​

Method / PropertyDescriptionReturns
add(script)Creates a Script asset and registers its route (defaults to /{name})void
all()Returns all Script resources in the siteScript[]
get(nameOrId)Retrieves a Script by name or IDScript | null
getByUrl(url)Retrieves a Script by route URL (such as /main.js)Script | null
update(script)Persists updates on a full Script objectvoid
updateBody(nameOrId, body)Replaces only the JavaScript body stringvoid
delete(nameOrId)Deletes a Script by name or ID (verify with get)void
getUrl(id) / getAbsUrl(id)Returns relative or absolute URL for a routed Scriptstring | null
getLogs(nameOrId) / getByLog(logId)Lists version logs or restores a snapshot by log IDChangeLog[] | null / Script | null

Differences from layouts and views

k.site.scripts supports getByUrl, getUrl, getAbsUrl and other routing-related APIs (similar to k.site.pages); k.site.layouts and k.site.views have no routing API.

Detailed Methods ​

add() ​

Create the script and register the route. If url is not passed, it defaults to /{name}, and if the path does not contain an extension, the script extension (usually .js) is appended automatically.

ParameterTypeRequiredDescription
script.namestringyesScript name (unique)
script.bodystringyesJavaScript content
script.urlstringnoAccess path, such as /assets/app.js
script.asyncbooleannoWhether to load with async
script.deferbooleannoWhether to load with defer

Returns: void

js
k.api.post(() => {
    const name = "analytics-helper-" + Date.now()
    k.site.scripts.add({
        name,
        url: "/assets/" + name + ".js",
        body: "window.__ready = true;",
        defer: true
    })
    const created = k.site.scripts.get(name)
    return { id: created?.id, route: created ? k.site.scripts.getUrl(created.id) : null }
})

all() ​

Returns all scripts.

None

Returns: Script[]

js
k.api.get(() => {
    const scripts = k.site.scripts.all()
    return { count: scripts.length, names: scripts.slice(0, 10).map((s) => s.name) }
})

get() ​

Get the script by name or ID.

ParameterTypeRequiredDescription
nameOrIdstringyesScript name or ID

Returns: Script | null

js
k.api.get(() => {
    const script = k.site.scripts.get("main")
    return script ? { id: script.id, name: script.name, extension: script.extension } : null
})

getByUrl() ​

Get the script by routing URL (e.g. /main.js).

ParameterTypeRequiredDescription
urlstringyesScript route URL

Returns: Script | null

js
k.api.get(() => {
    const script = k.site.scripts.getByUrl("/main.js")
    return script ? { id: script.id, name: script.name } : null
})

update() ​

Update the complete script object.

ParameterTypeRequiredDescription
scriptScriptyesModified full Script object

Returns: void

js
k.api.post(() => {
    const stamp = Date.now().toString()
    const name = "ai-script-upd-" + stamp
    const url = "/ai-script-upd-" + stamp + ".js"

    k.site.scripts.add({ name, url, body: "// v1" })
    const script = k.site.scripts.getByUrl(url)
    script.body = "// v2"
    k.site.scripts.update(script)

    const after = k.site.scripts.getByUrl(url)
    return {
        verified: after?.body?.indexOf("v2") >= 0,
        hasV1: after?.body?.indexOf("v1") >= 0
    }
})

updateBody() ​

Only updates body.

ParameterTypeRequiredDescription
nameOrIdstringyesScript name or ID
bodystringyesNew JavaScript content

Returns: void

js
k.api.post(() => {
    const stamp = Date.now().toString()
    const name = "ai-script-body-" + stamp
    const url = "/ai-script-body-" + stamp + ".js"

    k.site.scripts.add({ name, url, body: "// before" })
    k.site.scripts.updateBody(name, "// after-body")

    const after = k.site.scripts.getByUrl(url)
    return { verified: after?.body?.indexOf("after-body") >= 0 }
})

delete() ​

Delete by Name or ID. Verify with get(name) after deletion.

ParameterTypeRequiredDescription
nameOrIdstringyesScript name or ID

Returns: void

js
k.api.post(() => {
    const stamp = Date.now().toString()
    const name = "ai-script-del-" + stamp
    const url = "/ai-script-del-" + stamp + ".js"

    k.site.scripts.add({ name, url, body: "// del" })
    const before = k.site.scripts.getByUrl(url)

    k.site.scripts.delete(name)
    const after = k.site.scripts.get(name)

    return {
        verified: !!before && !after,
        hadBefore: !!before
    }
})

getUrl() / getAbsUrl() ​

Only scripts with registered routes can return relative or absolute URLs.

MethodParametersReturnsDescription
getUrl(id)id: stringstring | nullGets the relative URL
getAbsUrl(id)id: stringstring | nullGets the absolute URL

Returns: string | null

js
k.api.get(() => {
    const scripts = k.site.scripts.all()
    for (const script of scripts) {
        const rel = k.site.scripts.getUrl(script.id)
        if (!rel) continue
        const abs = k.site.scripts.getAbsUrl(script.id)
        return { relative: rel, absolute: abs }
    }
    return null
})

getLogs() / getByLog() ​

Inspect script version logs or restore a snapshot by log entry ID.

MethodParametersReturnsDescription
getLogs(nameOrId)nameOrId: stringChangeLog[] | nullGets version logs
getByLog(logId)logId: numberScript | nullGets a historical version by log ID

Returns: ChangeLog[] | null or Script | null

js
k.api.get(() => {
    const scripts = k.site.scripts.all()
    if (!scripts.length) return { error: "no scripts" }

    const logs = k.site.scripts.getLogs(scripts[0].id)
    return {
        verified: logs === null || Array.isArray(logs),
        logCount: logs ? logs.length : 0
    }
})

Common Script Fields ​

FieldTypeDescription
idstringScript ID
namestringScript name
bodystringJavaScript content
extensionstringFile extension, usually js
asyncbooleanWhether to load asynchronously (async)
deferbooleanWhether to defer execution (defer)
crossOriginstringCORS attribute
onlinebooleanWhether it is online
versionnumberVersion number

TypeScript Definitions ​

ts
interface ScriptRepository {
  add(script: ScriptInput): void;
  all(): Script[];
  get(nameOrId: string): Script | null;
  getByUrl(url: string): Script | null;
  update(script: Script): void;
  updateBody(nameOrId: string, body: string): void;
  delete(nameOrId: string): void;
  getUrl(id: string): string | null;
  getAbsUrl(id: string): string | null;
  getLogs(nameOrId: string): ChangeLog[] | null;
  getByLog(logId: number): Script | null;
}

interface ScriptInput {
  name: string;
  body: string;
  url?: string;
  async?: boolean;
  defer?: boolean;
}