@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
key— The header keyvalue— The header value
function clearHeaders(): void;
Clear all response headers in the current request context.
Get the request cookies.
Returns — The request cookies
function deleteCookie(name: string, options?: CookieSerializeOptions): void;
Delete a cookie.
Parameters
name— The name of the cookieoptions— The options for the cookie
function deleteHeader(key: string): void;
Delete a response header in the current request context.
Parameters
key— The header key
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.
Get the request headers or set the response headers.
Parameters
headers— The headers to set
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
key— The cache key, compound key or cached function to invalidate.
function isWorker(): boolean;
Returns true when the calling code is executing inside a worker spawned
by a "use worker" module.
- Server — checks whether the current Node.js thread is a
framework-managed Worker Thread. Returns
falsein Edge builds where"use worker"functions run in-process. - Client — checks whether the current context is a Web Worker.
Works identically in both server-side and client-side "use worker" modules.
Returns — true 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.
function outlet(target: string): string;
Get or set the active outlet.
Parameters
target— The outlet name
function redirect(url: string, status?: number, kind?: RedirectKind): void;
Redirects the request to the specified URL.
Parameters
url— The URL to redirect tostatus— The status code to use for the redirectkind— The kind of redirect to perform on the client
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
url— Render the component at the specified URLoutlet— The outlet to reload after rendering the component (defaults to page root)
function revalidate(key?: string): void;
Revalidates the current request cache.
Parameters
key— The cache key to revalidate, if not specified, the key will be the current request URL
function rewrite(pathname: string): URL;
Rewrites the current request URL to the specified pathname.
Parameters
pathname— The new pathname to use
Returns — The new URL object
function setCookie(name: string, value: string, options?: CookieSerializeOptions): void;
Set a cookie.
Parameters
name— The name of the cookievalue— The value of the cookieoptions— The options for the cookie
function setHeader(key: string, value: string): void;
Set a response header in the current request context.
Parameters
key— The header keyvalue— The header value
Sets the status code and status text of the response.
Parameters
status— The status code to setstatusText— The status text to set
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
function useHttpContext(): HttpContext;
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
ttl— The time-to-live (TTL) in milliseconds
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
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
Component— The component to cachettl— The time-to-live (TTL) in milliseconds
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.
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 Cookies = RequestContextExtensions["cookie"];
type RedirectKind = "navigate" | "push" | "location" | "error";
The kind of redirect to perform on the client.
navigate- RSC client-side navigation using replaceState (default)push- RSC client-side navigation using pushStatelocation- Full browser navigation via window.locationerror- Throw an error on the client instead of redirecting, allowing custom handling via try/catch