Rax Server Driver

1.0.0 · abandoned · verified Sun Apr 19

driver-server is a core component within the Rax framework, specifically designed to enable Server-Side Rendering (SSR) for Rax applications in Node.js environments. Rax, developed by Alibaba, is a universal JavaScript library known for its React-compatible API, lightweight footprint (~6KB gzipped), and performance optimizations, particularly for low-end devices. It distinguishes itself by supporting "write once, run anywhere" development across Web, Weex, Node.js, Alibaba MiniApp, and WeChat MiniProgram. The current stable version of driver-server is v1.2.2, released in September 2021. However, the Rax ecosystem, including driver-server, has seen very limited activity since then and is widely considered to be an abandoned project. This driver is essential for Rax applications seeking benefits like improved initial page load times and better SEO by pre-rendering components on the server before sending them to the client. Its integration requires careful environment detection using utilities like universal-env to ensure the correct driver is used based on whether the code is executing in a Node.js server or a browser client. Due to its inactive maintenance status, new features, security patches, or compatibility updates for newer JavaScript environments are unlikely.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to use `driver-server` to perform server-side rendering (SSR) of a Rax component. It shows the setup of a basic Rax component and its rendering using `ServerDriver` conditioned on the execution environment.

import {createElement, Component, render} from 'rax';
import ServerDriver from 'driver-server';
import {isNode} from 'universal-env';

class Example extends Component {
  render() {
    return (
      <div>
        <img width="560" height="560" src="https://img.alicdn.com/tps/TB1z.55OFXXXXcLXXXXXXXXXXXX-560-560.jpg" alt="Example Image" />
      </div>
    );
  }
}

// Render the Rax component, specifying ServerDriver for Node.js environments
// In a real application, you would capture the output and send it as HTML.
const serverRenderResult = render(<Example />, null, {
  driver: isNode ? ServerDriver : null
});

if (isNode) {
  console.log('Server-rendered HTML output:');
  // In a typical SSR setup, this output would be sent to the client as the initial HTML.
  // The exact method to get the string might vary depending on Rax version, but render() handles it.
  // For simplicity, we assume render() directly returns a string in server context here.
  // You might need to check Rax's documentation for specific server-side output retrieval.
  // For illustration, assuming serverRenderResult holds the rendered content.
  // This exact output format of `render` in server context needs to be confirmed with Rax docs.
  // Rax's render for SSR usually directly outputs HTML to a stream or returns it.
  // The provided example shows `render(<Example />, null, { driver: ServerDriver })`
  // which implies ServerDriver intercepts the rendering and provides server-specific output.
  // Without more context on `render`'s server-side return, we'll demonstrate basic usage.
  // A common pattern is `ReactDOMServer.renderToString(element)` in React, for Rax it's part of the render function.
  console.log(serverRenderResult); // Placeholder, actual output retrieval might differ.
} else {
  console.log('This code path is for browser rendering, not active in this quickstart.');
}

view raw JSON →