@shopify/react-server

A simple library for React server-side rendering using @shopify/react-html
.
Table of contents
Installation
yarn add @shopify/react-server
Rails Usage
We provide a gem to automagically setup a proxy controller for react-server.
Node Usage
Node apps require a server entry point that calls the createServer
function. At the minimum, this function requires a render
function that renders the main <App />
component.
import React from 'react';
import {createServer} from '@shopify/react-server';
import {Context} from 'koa';
import App from '../app';
const app = createServer({
port: process.env.PORT ? parseInt(process.env.PORT, 10) : 8081,
ip: process.env.IP,
assetPrefix: process.env.CDN_URL || 'localhost:8080/assets/webpack',
render: (ctx: Context) => {
const whatever = ;
return <App server location={ctx.request.url} someCustomProp={whatever} />;
},
});
export default app;
If you already have an existing node server, you can opt in to using only the render middleware provided by this package. See createRender()
.
Webpack Plugin
We also provide a webpack plugin to automatically generate the server and client entries for an application.
Deployment (Shopify specific)
For Shopifolk, we have a walkthrough for getting an app ready to deploy.
API
createServer()
Creates a full Koa
server which renders an @shopify/react-html
application.
import {createServer} from '@shopify/react-server';
The createServer
function takes an Options
object of the following interface.
interface Options {
port?: number;
ip?: string;
assetPrefix?: string;
assetName?: string | (ctx: Context) => string;
serverMiddleware?: compose.Middleware<Context>[];
render: RenderFunction;
debug?: boolean;
renderError: RenderFunction;
renderRawErrorMessage?: boolean;
htmlProps?: HtmlProps | (ctx: Context) => HtmlProps
}
It returns a running Koa server.
createRender()
Creates a Koa middleware which renders an @shopify/react-html
application.
import {createRender} from '@shopify/react-server';
The createRender
function takes two arguments. The first is a render function that should return the main component at the top of the application tree in JSX. This function receives the full Koa server context which can be used to derive any necessary props to feed into the main component.
The second argument is a subset of @shopify/react-effect#extract
's options which are simply delegated to the extract
call within the createRender
middleware.
Options
It returns a Koa middleware.