Navigated to /docs/core/api/server

Server rendering and sessions API

Node request handling, rendering, static caches, image optimization, environment loading, and sessions.

Server rendering and sessions exports

27 public exports, grouped under their canonical import boundary.

@tavojs/core/server

Canonical import boundary for every symbol in this section.

createMemorySessionStore#

createMemorySessionStore<T extends Record<string, unknown>>(options?: MemorySessionStoreOptions | undefined): SessionStore<T> & { size(): number; }

Creates memory session store for the server runtime.

Related guide

createMemoryStaticCache#

createMemoryStaticCache(options?: MemoryStaticCacheOptions | undefined): SsrStaticCache & { size(): number; clear(): void; }

Creates the default process-local static SSR cache used by the Node request handler.

Related guide

createNodeRequestHandler#

createNodeRequestHandler(options: NodeHandlerOptions, runtime?: PagesRuntime | Promise<PagesRuntime> | undefined): ((req: { url?: string | undefined; method?: string | undefined; headers?: Record<string, string | undefined> | undefined; once?: ((event: string, listener: () => void) => unknown) | undefined; off?: ((event: string, listener: () => void) => unknown) | undefined; }, res: { writeHead: (status: number, headers: Record<string, string | string[]>) => void; write: (chunk: Uint8Array<ArrayBufferLike>) => boolean | void; end: (body?: string | Uint8Array<ArrayBufferLike> | undefined) => void; headersSent?: boolean | undefined; destroyed?: boolean | undefined; once?: ((event: string, listener: () => void) => unknown) | undefined; off?: ((event: string, listener: () => void) => unknown) | undefined; }) => Promise<void>) & { invalidateCache: (tags: string | string[]) => Promise<number>; clearCache: () => Promise<void>; }

Creates a Node-style request handler that renders framework pages to HTML.

Related guide

createPagesRuntimeAsync#

createPagesRuntimeAsync(modules: PageModules, options?: PageRuntimeOptions | undefined): Promise<PagesRuntime>

Async variant for SSR/build flows that use async plugin lifecycle hooks.

Related guide

createSessionStorage#

createSessionStorage<T extends Record<string, unknown>>(options: SessionStorageOptions<T>): SessionStorage<T>

Creates session storage for the server runtime.

Related guide

defineServerOnly#

defineServerOnly<T extends (...args: any[]) => unknown>(fn: T): T

Defines and type-checks server only for the server runtime.

Related guide

ImageOptimizerOptions#

type ImageOptimizerOptions = {
    enabled?: boolean;
    allowRemote?: boolean;
    remotePatterns?: Array<string | RemoteImagePattern>;
    publicDir?: string;
    quality?: number;
    cacheMaxAge?: number;
    defaultFormat?: ImageFormat;
    sizes?: number[];
    timeoutMs?: number;
    maxBytes?: number;
    memoryCacheMaxEntries?: number;
    maxConcurrentTransforms?: number;
    maxPendingTransforms?: number;
    allowInsecureRemote?: boolean;
    resolveHostname?: (hostname: string) => Promise<Array<{
        address: string;
    }>>;
};

Configures image optimizer in the server runtime.

Related guide

invalidateStaticCache#

invalidateStaticCache(cache: SsrStaticCache, tags: string | string[]): Promise<number>

Invalidates tagged entries when supported by a cache adapter.

Related guide

loadServerEnv#

loadServerEnv(options?: LoadServerEnvOptions | undefined): Record<string, string>

Loads server-only .env files into process.env without exposing them to client code.

Related guide

LoadServerEnvOptions#

type LoadServerEnvOptions = {
    mode?: string;
    root?: string;
};

Configures load server env in the server runtime.

Related guide

MemorySessionStoreOptions#

type MemorySessionStoreOptions = {
    /** Maximum process-local sessions. Set to 0 to disable persistence. */
    maxEntries?: number;
};

Configures memory session store in the server runtime.

Related guide

MemoryStaticCacheOptions#

type MemoryStaticCacheOptions = {
    /** Maximum process-local entries. Set to 0 to disable storage. */
    maxEntries?: number;
};

Configures memory static cache in the server runtime.

Related guide

NodeHandlerOptions#

type NodeHandlerOptions = PageRuntimeOptions & {
    modules: PageModules;
    /** Public origin used when Node runs behind TLS termination, e.g. https://app.example.com. */
    canonicalOrigin?: string;
    document?: RenderDocumentOptions;
    stream?: boolean;
    images?: ImageOptimizerOptions;
    staticCache?: SsrStaticCache;
    maxRequestBodyBytes?: number;
};

Configures node handler in the server runtime.

Related guide

renderDocument#

renderDocument(node: Child, options?: RenderDocumentOptions | undefined): string

Renders document for the server runtime.

Related guide

RenderDocumentOptions#

type RenderDocumentOptions = {
    lang?: string;
    title?: string;
    unsafeHeadHtml?: string;
    bodyAttributes?: Record<string, string | number | boolean>;
    htmlAttributes?: Record<string, string | number | boolean>;
    appAttributes?: Record<string, string | number | boolean>;
    doctype?: string;
    appContainerId?: string;
    initialState?: unknown;
    stateScriptId?: string;
    nonce?: string;
    beforeRender?: () => void;
    styleRegistry?: StyleRegistry;
};

Configures render document in the server runtime.

Related guide

renderDocumentStream#

renderDocumentStream(node: Child, options?: RenderDocumentOptions | undefined): ReadableStream<Uint8Array<ArrayBufferLike>>

Renders document stream for the server runtime.

Related guide

renderPagesResponseFromRuntimeAsync#

renderPagesResponseFromRuntimeAsync(runtime: PagesRuntime, pathname: string, options?: RenderPagesDocumentAsyncOptions | undefined): Promise<RenderPagesResponse>

Resolves route data/head with a prebuilt runtime and returns HTML with HTTP response metadata.

Related guide

Session#

type Session<T extends Record<string, unknown>> = {
    readonly data: T;
    readonly id: string;
    readonly isNew: boolean;
    readonly rotated: boolean;
    readonly secure: boolean;
    delete(key: keyof T & string): void;
    destroy(): void;
    get<K extends keyof T & string>(key: K): T[K] | undefined;
    has(key: keyof T & string): boolean;
    rotate(): void;
    set<K extends keyof T & string>(key: K, value: T[K]): void;
};

Defines the session contract used by the server runtime.

Related guide

SessionCommitOptions#

type SessionCommitOptions = {
    maxAge?: number;
};

Configures session commit in the server runtime.

Related guide

SessionCookieOptions#

type SessionCookieOptions = {
    domain?: string;
    httpOnly?: boolean;
    maxAge?: number;
    name: string;
    path?: string;
    sameSite?: SessionCookieSameSite;
    secrets: string[];
    secure?: boolean;
};

Configures session cookie in the server runtime.

Related guide

SessionCookieSameSite#

type SessionCookieSameSite = "lax" | "strict" | "none";

Defines the session cookie same site contract used by the server runtime.

Related guide

SessionStorage#

type SessionStorage<T extends Record<string, unknown>> = {
    commitSession(session: Session<T>, options?: SessionCommitOptions): Promise<string>;
    destroySession(session: Session<T>, options?: SessionCommitOptions): Promise<string>;
    getSession(contextOrRequest?: unknown): Promise<Session<T>>;
    redirect(to: string, session: Session<T>, init?: ResponseInit): Promise<Response>;
};

Defines the session storage contract used by the server runtime.

Related guide

SessionStorageOptions#

type SessionStorageOptions<T extends Record<string, unknown>> = {
    cookie: SessionCookieOptions;
    store?: SessionStore<T>;
};

Configures session storage in the server runtime.

Related guide

SessionStore#

type SessionStore<T extends Record<string, unknown>> = {
    get(id: string): Promise<SessionStoreEntry<T> | null> | SessionStoreEntry<T> | null;
    set(id: string, entry: SessionStoreEntry<T>): Promise<void> | void;
    delete(id: string): Promise<void> | void;
};

Defines storage behavior for session in the server runtime.

Related guide

SessionStoreEntry#

type SessionStoreEntry<T extends Record<string, unknown>> = {
    data: T;
    expiresAt: number | null;
};

Describes one entry in session store in the server runtime.

Related guide

SsrStaticCache#

type SsrStaticCache = {
    get(key: string): SsrStaticCacheEntry | null | Promise<SsrStaticCacheEntry | null>;
    set(key: string, entry: SsrStaticCacheEntry): void | Promise<void>;
    delete(key: string): void | Promise<void>;
    invalidateTags?(tags: string[]): number | Promise<number>;
    clear?(): void | Promise<void>;
};

Defines the ssr static cache contract used by the server runtime.

Related guide

SsrStaticCacheEntry#

type SsrStaticCacheEntry = {
    response: RenderPagesResponse;
    expiresAt: number | null;
    tags: string[];
};

Describes one entry in ssr static cache in the server runtime.

Related guide