UNPKG

5.16 kBMarkdownView Raw
1# `@shopify/react-server`
2
3[![Build Status](https://travis-ci.org/Shopify/quilt.svg?branch=master)](https://travis-ci.org/Shopify/quilt)
4[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](LICENSE.md) [![npm version](https://badge.fury.io/js/%40shopify%2Freact-server.svg)](https://badge.fury.io/js/%40shopify%2Freact-server.svg)
5
6A simple library for React server-side rendering using [`@shopify/react-html`](https://github.com/Shopify/quilt/tree/master/packages/react-html).
7
8## Table of contents
9
101. [Installation](#installation)
111. [Node usage](#node-usage)
121. [Rails usage](#rails-usage)
13 1. [Deployment](#deployment)
141. [Webpack plugin](#webpack-plugin)
151. [API](#api)
16
17## Installation
18
19```bash
20$ yarn add @shopify/react-server
21```
22
23## Rails Usage
24
25We provide a [gem](https://github.com/Shopify/quilt/blob/master/gems/quilt_rails/README.md#L2) to automagically setup a proxy controller for react-server.
26
27## Node Usage
28
29Node 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.
30
31```tsx
32import React from 'react';
33import {createServer} from '@shopify/react-server';
34import App from '../app';
35
36const app = createServer({
37 render: ctx => <App location={ctx.request.url} />,
38});
39```
40
41If you already have an existing node server, you can opt in to using only the render middleware provided by this package. See `createRender()`.
42
43## Webpack Plugin
44
45We also provide a [webpack plugin](https://github.com/Shopify/quilt/blob/master/packages/react-server-webpack-plugin) to automatically generate the server and client entries for an application.
46
47### Deployment (Shopify specific)
48
49For Shopifolk, we have a [walkthrough](https://docs.shopifycloud.com/getting_started/rails-with-node-walkthrough) for getting an app ready to deploy.
50
51## API
52
53### `createServer()`
54
55Creates a full `Koa` server which renders an `@shopify/react-html` application.
56
57```tsx
58import {createServer} from '@shopify/react-server';
59```
60
61The `createServer` function takes an `Options` object of the following interface.
62
63```tsx
64interface Options {
65 // the port to bind
66 port?: number;
67 // the ip to run the application on
68 ip?: string;
69 // the full base url for the cdn if applicable
70 assetPrefix?: string;
71 // any additional Koa middleware to mount on the server
72 serverMiddleware?: compose.Middleware<Context>[];
73 // a function of `(ctx: Context, data: {locale: string}): React.ReactElement<any>`
74 render: RenderFunction;
75 // whether to run in debug mode
76 debug?: boolean;
77}
78```
79
80It returns a running [Koa](https://github.com/koajs/koa/) server.
81
82### `createRender()`
83
84Creates a Koa middleware which renders an `@shopify/react-html` application.
85
86```tsx
87import {createRender} from '@shopify/react-server';
88```
89
90The `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](https://github.com/koajs/koa/) server context which can be used to derive any necessary props to feed into the main component.
91
92The second argument is a subset of [`@shopify/react-effect#extract`](../react-effect/README.md#extract)'s options which are simply delegated to the `extract` call within the `createRender` middleware.
93
94#### Options
95
96- `afterEachPass?(pass: Pass): any` see [`@shopify/react-effect#extract`](../react-effect/README.md#extract)
97
98- `betweenEachPass?(pass: Pass): any` see [`@shopify/react-effect#extract`](../react-effect/README.md#extract)
99
100It returns a [Koa](https://github.com/koajs/koa/) middleware.
101
102### `createDefaultProvider()`
103
104This function return a set of providers based on a given set of options, defined below.
105
106```ts
107interface Options {
108 // determines whether to render a `CsrfUniversalProvider`, default is true.
109 csrf: boolean;
110}
111```
112
113_Example_
114
115```tsx
116import React from 'react';
117import {createDefaultProvider} from '@shopify/react-server`;
118
119const MyDefaultProvider = createDefaultProvider({csrf: false});
120
121function App() {
122 return (
123 <MyDefaultProvider>
124 {/* rest of app */}
125 </MyDefaultProvider>
126 )
127}
128```
129
130### `<DefaultProvider />`
131
132A single component that renders all of the providers and context required within a typical React application, including a [CookieUniversalProvider](../packages/react-cookie/README.md#client) and a [CrsfUniversalProvider](../packages/react-csrf-universal-provider/README.md).
133
134_Basic Cookie Example_
135
136```tsx
137// app/ui/server.tsx
138
139import React from 'react';
140import {createServer} from '@shopify/react-server';
141import App from './App.tsx';
142
143const app = createServer({
144 render(ctx) {
145 return (
146 <DefaultProviders>
147 <App location={ctx.request.url} />
148 </DefaultProviders>
149 );
150 },
151});
152
153export default app;
154```
155
156```tsx
157// app/ui/App.tsx
158
159import React from 'react';
160import {useCookie} from '@shopify/react-cookie';
161
162export default function App() {
163 // this works
164 const [someCookie] = useCookie('SomeCookieKey');
165
166 return (
167 <>
168 The cookie is {someCookie}
169 {/* rest of app */}
170 </>
171 );
172}
173```