Phoenix.ReactServer.Server (Phoenix.ReactServer NG v0.8.4)

Copy Markdown View Source

The React Render Server manages component rendering requests and caching.

This GenServer acts as the main interface for rendering React components, handling cache management, and coordinating with JavaScript runtimes.

Features

  • Intelligent Caching: ETS-based caching with configurable TTL
  • Runtime Coordination: Manages communication with JavaScript runtimes
  • Error Handling: Comprehensive error reporting and recovery
  • Performance Monitoring: Built-in metrics and health checks
  • Graceful Shutdown: Proper cleanup of resources

Configuration

Configure in your application supervisor:

config :phoenix_react_ng, Phoenix.ReactServer,
  runtime: Phoenix.ReactServer.Runtime.Bun,
  component_base: Path.expand("../assets/component", __DIR__),
  cache_ttl: 60

Server Lifecycle

The server goes through these phases:

  1. Startup: Initializes cache and runtime connections
  2. Ready: Accepts rendering requests
  3. Shutdown: Cleans up resources gracefully

Summary

Types

Time in milliseconds

Time in seconds

Configuration options for the React server

Server state containing runtime and configuration

Functions

Returns a specification to start this module under a supervisor.

Returns the React Render Server configuration.

Sets the runtime process PID.

Starts the React Render Server.

Types

millisecond()

@type millisecond() :: non_neg_integer()

Time in milliseconds

second()

@type second() :: non_neg_integer()

Time in seconds

server_config()

@type server_config() :: [
  cache_ttl: second(),
  component_base: Path.t(),
  render_timeout: millisecond(),
  runtime: module()
]

Configuration options for the React server

server_state()

@type server_state() :: %{
  runtime: module(),
  component_base: Path.t(),
  render_timeout: millisecond(),
  runtime_process: pid()
}

Server state containing runtime and configuration

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

config()

@spec config() :: server_config()

Returns the React Render Server configuration.

Retrieves configuration from Application.get_env(:phoenix_react_ng, Phoenix.ReactServer) and applies default values for missing options.

Returns

Keyword list containing:

  • :runtime - Runtime module (default: Phoenix.ReactServer.Runtime.Bun)
  • :component_base - Component directory path
  • :cache_ttl - Cache TTL in seconds (default: 600)
  • :render_timeout - Render timeout in milliseconds (default: 300_000)

Example

iex> Phoenix.ReactServer.Server.config()
[runtime: Phoenix.ReactServer.Runtime.Bun, component_base: "/path/to/components", ...]

set_runtime_process(pid)

@spec set_runtime_process(pid()) :: :ok

Sets the runtime process PID.

Used internally to update the runtime process reference.

Parameters

  • pid - The runtime process PID

start_link(init_arg)

@spec start_link(term()) :: GenServer.on_start()

Starts the React Render Server.

Parameters

  • init_arg - Initial arguments (typically [])

Returns

  • {:ok, pid} - Server started successfully
  • {:error, reason} - Failed to start server