API.md

@lazarv/react-server

この API リファレンスはまだ翻訳されていません。以下は英語版の内容です。

The core runtime entry point. Exports the primitives every server component, middleware, and route handler relies on — HTTP context hooks, cookie helpers, caching utilities, and rendering controls.

function appendHeader(key: string, value: string): void;

Append a response header in the current request context.

Parameters

function clearHeaders(): void;

Clear all response headers in the current request context.

function cookie(): Cookies;

Get the request cookies.

Returns — The request cookies

function deleteCookie(name: string, options?: CookieSerializeOptions): void;

Delete a cookie.

Parameters

function deleteHeader(key: string): void;

Delete a response header in the current request context.

Parameters

function getRuntime<R = Record<string, unknown>>(): R; function getRuntime<R = unknown, K = string>(key?: K): R;

Get runtime context store.

Returns — The runtime context store.

function headers(headers?: Record<string, string> | Headers | [ string, string ][]): void;

Get the request headers or set the response headers.

Parameters

import { headers } from '@lazarv/react-server'; export default function App() { const requestHeaders = headers(); return <p>{requestHeaders.get('user-agent')}</p>; }
function invalidate(key?: string): Promise<void>; function invalidate(key: string[]): Promise<void>; function invalidate<T extends (...args: any[]) => any>(fn: T): Promise<void>;

Invalidates cached function.

Parameters

function isWorker(): boolean;

Returns true when the calling code is executing inside a worker spawned by a "use worker" module.

Works identically in both server-side and client-side "use worker" modules.

Returnstrue if inside a "use worker" worker, false otherwise.

Prefer importing from @lazarv/react-server/worker inside "use worker" modules — that sub-path has no server-only dependencies and works in both server Worker Threads and client Web Workers.

"use worker"; import { isWorker } from "@lazarv/react-server/worker"; export async function terminate() { if (isWorker()) { process.exit(0); } }
function outlet(target: string): string;

Get or set the active outlet.

Parameters

function redirect(url: string, status?: number, kind?: RedirectKind): void;

Redirects the request to the specified URL.

Parameters

import { redirect } from '@lazarv/react-server'; export default function App() { if (condition) { redirect('/login'); } // OAuth flow - force full browser navigation redirect('/api/oauth/authorize', 302, 'location'); // ... }
function reload(url?: URL | string, outlet?: string): void;

Instructs the framework to reload the page or the specified outlet after executing the current server function, using the component rendered at the specified URL. Only usable inside server functions!

Parameters

function revalidate(key?: string): void;

Revalidates the current request cache.

Parameters

function rewrite(pathname: string): URL;

Rewrites the current request URL to the specified pathname.

Parameters

Returns — The new URL object

function setCookie(name: string, value: string, options?: CookieSerializeOptions): void;

Set a cookie.

Parameters

function setHeader(key: string, value: string): void;

Set a response header in the current request context.

Parameters

function status(status?: number, statusText?: string): void;

Sets the status code and status text of the response.

Parameters

import { status } from '@lazarv/react-server'; export default function NotFound() { status(404, 'Not Found'); return <h1>404 Not Found</h1>; }
function useFormData(): FormData;

This hook returns the current form data if request Content-Type is application/x-www-form-urlencoded or multipart/form-data.

Returns — The current form data

This hook returns the current request context, including the request, response, URL and method.

Returns — The current request context

function useOutlet(): string;

Get the active outlet when using client navigation.

Returns — The outlet name or page root outlet name ("PAGE_ROOT")

function usePathname(): string;

This hook returns the current pathname.

Returns — The current pathname

function useRender(): { lock(task: () => Promise<void>): Promise<void>; lock(): () => void; isRemote: boolean; isFunction: boolean; type: "Unknown" | "HTML" | "RSC" | "Remote"; };

This function returns an object which contains helper functions to control the render process. lock() enables you to lock the render of the current component to wait for the specified task to complete before sending HTTP headers and cookies to the client.

Returns — An object with two methods: lock(task: () => Promise<void>) and lock(): () => void

function useRequest(): Request;

This hook returns the current request object.

Returns — The current request object

function useResponse(): Promise<Response>;

This hook returns the current response object.

Returns — The current response object

function useResponseCache(ttl?: number): void;

This hook enables caching the response for the specified time-to-live (TTL). If the TTL is not specified, the cache will be stored indefinitely.

Parameters

import { useResponseCache } from '@lazarv/react-server'; export default function App() { useResponseCache(3000); return <>{Math.random()}</>; }
function useSearchParams(): Record<string, string | string[]>;

This hook returns the current search params.

Returns — The current search params as an object

function useSignal(): AbortSignal | null;

This hook returns the current abort signal. The signal is aborted when the HTTP request is cancelled by the client. Available in both server components and workers. Pass this signal to fetch calls or any other API that accepts an AbortSignal to propagate cancellation.

Returns — The current AbortSignal, or null if not available

function useUrl(): URL;

This hook returns the current URL object.

Returns — The current URL object

import { useUrl } from '@lazarv/react-server'; export default function App() { const url = useUrl(); return <h1>{url.pathname}</h1>; }
function withCache<T extends React.FC>(Component: T, ttl?: number | true): T;

This function enables caching the response for the specified time-to-live (TTL). If the TTL is not specified, the cache will be stored indefinitely. This higher-order component (HOC) is just a wrapper to use the useResponseCache hook as a HOC.

Parameters

import { withCache } from '@lazarv/react-server'; function App() { return <>{Math.random()}</>; } export default withCache(App, 3000);
const logger: { info(msg: string, ...args: unknown[]): void; warn(msg: string, ...args: unknown[]): void; error(msg: string | Error, ...args: unknown[]): void; };

A logger proxy that resolves to the framework's logger at runtime. In development, this uses Vite's logger. In production, it falls back to console.

import { logger } from '@lazarv/react-server'; export default function App() { logger.info('Rendering App'); return <div>Hello</div>; }
const RenderType: { [K in "Unknown" | "HTML" | "RSC" | "Remote"]: K; };

Render types.

let version: string;

The current version of @lazarv/react-server.

interface ReactServerCache { get<T = unknown>(keys: string[]): Promise<T | undefined>; set<T = unknown>(keys: string[], value: T): Promise<void>; has(keys: string[]): Promise<boolean>; setExpiry(keys: string[], ttl: number): Promise<void>; hasExpiry(keys: string[], ttl: number): Promise<boolean>; delete(keys: string[]): Promise<void>; }
type RedirectKind = "navigate" | "push" | "location" | "error";

The kind of redirect to perform on the client.