browserOnly

TypeScript
function browserOnly(fn: () => T, fallback: T): T

Execute a function only in browser environment Returns the fallback value in server environment

Parameter

Type

Description

fn

() => T

Function to execute in browser

fallback

T

Value to return in server environment

Returns: T

TypeScript
const visitorId = browserOnly(
  () => localStorage.getItem('visitor_id'),
  null
)

browserOnlyAsync

TypeScript
function browserOnlyAsync(fn: () => Promise<T>, fallback: T): Promise<T>

Execute an async function only in browser environment Returns the fallback value in server environment

Parameter

Type

Description

fn

() => Promise<T>

Async function to execute in browser

fallback

T

Value to return in server environment

Returns: Promise<T>

TypeScript
const config = await browserOnlyAsync(
  () => fetchConfig(),
  defaultConfig
)

createClient

TypeScript
function createClient(config: ClientConfig): Client

Creates a Lynkow client instance (SDK v3)

Parameter

Type

Description

config

ClientConfig

Client configuration

Returns: Client

TypeScript
const lynkow = createClient({
  siteId: 'your-site-uuid',
  locale: 'fr',
  debug: true
})

const posts = await lynkow.contents.list()

createLynkowClient

TypeScript
function createLynkowClient(config: LynkowConfig): LynkowClient

Creates a Lynkow client instance (legacy naming)

Parameter

Type

Description

config

LynkowConfig

Client configuration

Returns: LynkowClient

TypeScript
// Deprecated -- use createClient() instead:
const lynkow = createClient({ siteId: 'your-site-uuid', locale: 'fr' })
const posts = await lynkow.contents.list()

detectSiteTheme

TypeScript
function detectSiteTheme(): "light" | "dark"

Detect the website's theme by inspecting DOM indicators.

Detection cascade:

  1. data-theme / data-mode / data-color-scheme attributes on <html> or <body>

  2. "dark" class on <html> or <body> (Tailwind convention)

  3. CSS color-scheme property on <html>

  4. Background color luminance of <body>

  5. Fallback: OS-level prefers-color-scheme media query

Returns: "light" | "dark"

TypeScript
// Apply a conditional class based on the site's current theme
const theme = detectSiteTheme()
document.body.classList.add(theme === 'dark' ? 'inverted-text' : 'default-text')

isCategoryResolve

TypeScript
function isCategoryResolve(response: ResolveResponse): response

Checks if a resolution response is a category

Parameter

Type

Description

response

ResolveResponse

Returns: response

TypeScript
// Resolve a URL path and render the appropriate template
const resolved = await lynkow.paths.resolve('/blog/tutorials')
if (isCategoryResolve(resolved)) {
  renderCategoryPage(resolved.data) // TypeScript narrows to CategoryResolveResponse
}

isContentResolve

TypeScript
function isContentResolve(response: ResolveResponse): response

Checks if a resolution response is a content

Parameter

Type

Description

response

ResolveResponse

Returns: response

TypeScript
// Resolve a URL path and render the appropriate template
const resolved = await lynkow.paths.resolve('/blog/my-article')
if (isContentResolve(resolved)) {
  renderArticle(resolved.data) // TypeScript narrows to ContentResolveResponse
}

isLynkowError

TypeScript
function isLynkowError(error: unknown): error

Type guard to check if an unknown value is a LynkowError. Use this in catch blocks to safely access LynkowError properties.

Parameter

Type

Description

error

unknown

The caught value to check

Returns: error

TypeScript
try {
  await lynkow.contents.getBySlug('not-found')
} catch (error) {
  if (isLynkowError(error) && error.code === 'NOT_FOUND') {
    // Handle 404 -- error.status is 404
  }
}

onSiteThemeChange

TypeScript
function onSiteThemeChange(callback: (theme: "light" | "dark") => void): () => void

Observe site theme changes in real-time.

Watches for:

  • Attribute changes on <html> and <body> (data-theme, data-mode, class, style)

  • OS-level prefers-color-scheme media query changes

Parameter

Type

Description

callback

(theme: "light" | "dark") => void

Called with the new theme when a change is detected

Returns: () => void

TypeScript
// Update a widget's appearance when the site theme changes
const stopObserving = onSiteThemeChange((theme) => {
  widget.setTheme(theme)
})

// Stop observing when no longer needed
stopObserving()

renderJsonLdGraph

TypeScript
function renderJsonLdGraph(nodes: object[] | null | undefined): string

Render a resolved JSON-LD @graph as a single <script type="application/ld+json"> tag ready to inject into a page <head>.

The input is the array returned by the Lynkow public API at content.structuredData.graph (for articles) or page.structuredData.graph (for site blocks of type page). Each entry is already a fully-formed schema.org object with @context, @id, and @type. This helper strips the per-node @context and wraps the array in a top-level @context + @graph to keep the emitted script as compact as possible.

No HTML escaping is performed on the JSON body itself, but </script> sequences that would otherwise break the enclosing tag are guarded against via a Unicode-safe replacement.

Parameter

Type

Description

nodes

object[] | null | undefined

Array of JSON-LD node objects. null, undefined, or an<br> empty array returns an empty string so you can safely<br> spread the result into server-rendered HTML without<br> conditional branches.

Returns: string

TypeScript
// Next.js App Router (server component)
import { createClient, renderJsonLdGraph } from 'lynkow'

const client = createClient({ siteId: process.env.LYNKOW_SITE_ID! })

export default async function ArticlePage({ params }: { params: { slug: string } }) {
  const article = await client.contents.getBySlug(params.slug)
  return (
    <>
      <div
        dangerouslySetInnerHTML={{
          __html: renderJsonLdGraph(article.structuredData?.graph),
        }}
      />
      <h1>{article.title}</h1>
      <article dangerouslySetInnerHTML={{ __html: article.body }} />
    </>
  )
}