Skip to content

k.site.pages ​

Create, read, update, delete, manage routes, and inspect version logs for site pages

Execution ContextPrerequisitesTypical Use
KScript API / Server scriptsCurrent site context; repository-level page operations (use k.page for current-page meta)Programmatically create/update site pages, resolve routes or localized URLs, and inspect change logs

Quick Start ​

Create a page with an explicit route and immediately query it via getByUrl() and getUrl():

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

    k.site.pages.add({
        name,
        url,
        body: `<layout id="main"><placeholder id="Main"><h1>${name}</h1></placeholder></layout>`
    })

    const page = k.site.pages.getByUrl(url)
    return {
        created: !!page,
        id: page?.id,
        url: page ? k.site.pages.getUrl(page.id) : null
    }
})

Quick Reference ​

Method / PropertyDescriptionReturns
add(page)Creates a page and registers its route (defaults to /{name})void
all()Returns all Page objects in the sitePage[]
get(nameOrId)Retrieves a page by name or GUID IDPage | null
getByUrl(url)Retrieves a page by route URL (resolves route aliases; throws NullReferenceException if no route matches)Page | null
update(page)Persists updates on a full Page objectvoid
updateBody(nameOrId, body)Replaces only the page HTML bodyvoid
delete(nameOrId)Deletes a page by name or IDvoid
getUrl(id) / getAbsUrl(id)Returns relative or absolute URL by page IDstring | null
getUrls(nameOrId)Returns the per-culture route map for a pageRecord<string, string>
updateRoute(id, url, culture?)Updates the page route URL for a given culturevoid
getLogs(nameOrId) / getByLog(logId)Lists version logs and restores a historical snapshotChangeLog[] | null / Page | null

Differences from k.page

APIPurpose
k.site.pagesCRUD and URL lookup for all pages in the site (repository scope)
k.pageCurrent rendering page setTitle / setMeta etc. (request scope)

Detailed Methods ​

add() ​

Create a page and register a route. If url is not passed, it defaults to /{name}.

ParameterTypeRequiredDescription
page.namestringyesPage name (unique)
page.bodystringyesPage content, which can be a <layout> placeholder structure
page.urlstringnoAccess path, such as /about; defaults to /{name} when omitted
page.layoutNamestringnoAssociated layout name

Returns: void

js
k.api.post(() => {
    const stamp = Date.now().toString()
    const name = "doc-add-" + stamp
    k.site.pages.add({ name, body: `<h1>${name}</h1>` })
    const page = k.site.pages.get(name)
    return { id: page?.id, url: page ? k.site.pages.getUrl(page.id) : null }
})

all() ​

Returns an array of all page objects on the site.

None

Returns: Page[]

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

get() ​

Get the page by Name or Id (GUID string).

ParameterTypeRequiredDescription
nameOrIdstringyesPage name or ID

Returns: Page | null

js
k.api.get(() => {
    const page = k.site.pages.get("home")
    return page ? { id: page.id, name: page.name } : null
})

getByUrl() ​

Get the page by routing URL (routing aliases will be resolved).

ParameterTypeRequiredDescription
urlstringyesPage route URL

Returns: Page | null

Throws NullReferenceException on unregistered URLs

When no route matches url, k.site.pages.getByUrl(url) does not return null—it throws NullReferenceException (Object reference not set to an instance of an object.). It only returns null when a matching route exists with an empty objectId or broken alias target. Check k.site.routes.getByUrl(url) first or wrap the call in try/catch (which also covers parameterized routes).

js
// "/" must exist as an exact registered route (does not fall back to /index or /home)
k.api.get(() => {
    const targetUrl = "/"
    if (!k.site.routes.getByUrl(targetUrl)) {
        return null
    }
    try {
        const page = k.site.pages.getByUrl(targetUrl)
        return page ? { id: page.id, name: page.name } : null
    } catch (e) {
        return null
    }
})

update() ​

Update with complete page object (usually get / getByUrl first and then change fields).

ParameterTypeRequiredDescription
pagePageyesModified full page object

Returns: void

js
k.api.post(() => {
    const stamp = Date.now().toString()
    const url = "/doc-upd-" + stamp
    k.site.pages.add({
        name: "doc-upd-" + stamp,
        url,
        body: '<layout id="main"><placeholder id="Main"><h1>v1</h1></placeholder></layout>'
    })

    const page = k.site.pages.getByUrl(url)
    page.body = '<layout id="main"><placeholder id="Main"><h1>v2</h1></placeholder></layout>'
    k.site.pages.update(page)

    const after = k.site.pages.getByUrl(url)
    return {
        verified: after?.body?.indexOf("v2") >= 0,
        snippet: after?.body?.substring(0, 80)
    }
})

updateBody() ​

Only the body field is updated.

ParameterTypeRequiredDescription
nameOrIdstringyesPage name or ID
bodystringyesNew page body HTML

Returns: void

js
k.api.post(() => {
    const stamp = Date.now().toString()
    const url = "/doc-body-" + stamp
    k.site.pages.add({
        name: "doc-body-" + stamp,
        url,
        body: "<h1>before</h1>"
    })

    const page = k.site.pages.getByUrl(url)
    k.site.pages.updateBody(page.id, "<h1>after</h1>")

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

delete() ​

Delete pages by name or ID.

ParameterTypeRequiredDescription
nameOrIdstringyesPage name or ID

Returns: void

WARNING

Confirm deletion with get(nameOrId). Deleting a page normally removes its associated route, so calling getByUrl on that URL afterwards throws NullReferenceException because the route is null (it only returns null if the route is preserved with an empty objectId because another object still references it).

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

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

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

    return {
        hadPage: !!before,
        deleted: !!before && !after
    }
})

getUrl() / getAbsUrl() ​

Get a relative path or absolute URL based on the page Id.

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 page = k.site.pages.all()[0]
    if (!page) return { error: "no pages" }
    return {
        id: page.id,
        relative: k.site.pages.getUrl(page.id),
        absolute: k.site.pages.getAbsUrl(page.id)
    }
})

getLogs() / getByLog() ​

getLogs returns the change log of the specified page; getByLog restores the version object according to the log ID.

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

Returns: ChangeLog[] | null or Page | null

js
k.api.get(() => {
    const page = k.site.pages.all()[0]
    if (!page) return { error: "no pages" }
    const logs = k.site.pages.getLogs(page.id)
    return { logCount: logs ? logs.length : 0 }
})

getUrls() ​

Returns the routing path dictionary of the page in each culture.

ParameterTypeRequiredDescription
nameOrIdstringyesPage name or ID

Returns: Record<string, string>

js
k.api.get(() => {
    const page = k.site.pages.all()[0]
    if (!page) return { error: "no pages" }
    return k.site.pages.getUrls(page.id)
})

updateRoute() ​

Update the routing path of the page in the specified culture.

ParameterTypeRequiredDescription
idstringyesPage ID
urlstringyesNew path
culturestringnoCulture code; defaults to the site's main culture

Returns: void

js
// Update the URL for a specific language in a multilingual site
k.api.post(() => {
    const page = k.site.pages.get("about")
    if (!page) return { error: "page not found" }
    k.site.pages.updateRoute(page.id, "/about-us", "en")
    return k.site.pages.getUrls(page.id)
})

Common Page Fields ​

FieldTypeDescription
idstringPage ID
namestringName
bodystringPage body
layoutNamestringLayout name
url—Get route via getUrl(id)
onlinebooleanWhether online
versionnumberVersion number

TypeScript Definitions ​

ts
interface PageRepository {
  add(page: PageInput): void;
  all(): Page[];
  get(nameOrId: string): Page | null;
  getByUrl(url: string): Page | null; // Throws NullReferenceException when no route matches url
  update(page: Page): 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): Page | null;
  getUrls(nameOrId: string): Record<string, string>;
  updateRoute(id: string, url: string, culture?: string): void;
}

interface PageInput {
  name: string;
  body: string;
  url?: string;
  layoutName?: string;
}