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: 60Server Lifecycle
The server goes through these phases:
- Startup: Initializes cache and runtime connections
- Ready: Accepts rendering requests
- 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
@type millisecond() :: non_neg_integer()
Time in milliseconds
@type second() :: non_neg_integer()
Time in seconds
@type server_config() :: [ cache_ttl: second(), component_base: Path.t(), render_timeout: millisecond(), runtime: module() ]
Configuration options for the React server
@type server_state() :: %{ runtime: module(), component_base: Path.t(), render_timeout: millisecond(), runtime_process: pid() }
Server state containing runtime and configuration
Functions
Returns a specification to start this module under a supervisor.
See Supervisor.
@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", ...]
@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
@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