@shopify/react-server
Utilities for React server-side rendering.
Last updated 10 days ago by shopify-dep .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install @shopify/react-server 
SYNC missed versions from official npm registry.

@shopify/react-server

Build Status License: MIT npm version

A simple library for React server-side rendering using @shopify/react-html.

Table of contents

  1. Installation
  2. Node usage
  3. Rails usage
    1. Deployment
  4. Webpack plugin
  5. API

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 App from '../app';

const app = createServer({
  render: ctx => <App location={ctx.request.url} />,
});

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 {
  // the port to bind
  port?: number;
  // the ip to run the application on
  ip?: string;
  // the full base url for the cdn if applicable
  assetPrefix?: string;
  // any additional Koa middleware to mount on the server
  serverMiddleware?: compose.Middleware<Context>[];
  // a function of `(ctx: Context, data: {locale: string}): React.ReactElement<any>`
  render: RenderFunction;
  // whether to run in debug mode
  debug?: boolean;
}

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.

createDefaultProvider()

This function return a set of providers based on a given set of options, defined below.

interface Options {
  // determines whether to render a `CsrfUniversalProvider`, default is true.
  csrf: boolean;
}

Example

import React from 'react';
import {createDefaultProvider} from '@shopify/react-server`;

const MyDefaultProvider = createDefaultProvider({csrf: false});

function App() {
  return (
    <MyDefaultProvider>
      {/* rest of app */}
    </MyDefaultProvider>
  )
}

<DefaultProvider />

A single component that renders all of the providers and context required within a typical React application, including a CookieUniversalProvider and a CrsfUniversalProvider.

Basic Cookie Example

// app/ui/server.tsx

import React from 'react';
import {createServer} from '@shopify/react-server';
import App from './App.tsx';

const app = createServer({
  render(ctx) {
    return (
      <DefaultProviders>
        <App location={ctx.request.url} />
      </DefaultProviders>
    );
  },
});

export default app;
// app/ui/App.tsx

import React from 'react';
import {useCookie} from '@shopify/react-cookie';

export default function App() {
  // this works
  const [someCookie] = useCookie('SomeCookieKey');

  return (
    <>
      The cookie is {someCookie}
      {/* rest of app */}
    </>
  );
}

Current Tags

  • 0.7.0-beta.2                                ...           beta (4 months ago)
  • 0.8.8                                ...           latest (10 days ago)

47 Versions

  • 0.8.8                                ...           10 days ago
  • 0.8.7                                ...           17 days ago
  • 0.8.6                                ...           18 days ago
  • 0.8.5                                ...           2 months ago
  • 0.8.4                                ...           2 months ago
  • 0.8.3                                ...           2 months ago
  • 0.8.2                                ...           3 months ago
  • 0.8.1                                ...           3 months ago
  • 0.8.0                                ...           3 months ago
  • 0.7.11                                ...           3 months ago
  • 0.7.10                                ...           3 months ago
  • 0.7.9                                ...           3 months ago
  • 0.7.8                                ...           3 months ago
  • 0.7.7                                ...           3 months ago
  • 0.7.6                                ...           3 months ago
  • 0.7.5                                ...           4 months ago
  • 0.7.4                                ...           4 months ago
  • 0.7.3                                ...           4 months ago
  • 0.7.2                                ...           4 months ago
  • 0.7.0                                ...           4 months ago
  • 0.7.1                                ...           4 months ago
  • 0.7.0-beta.2                                ...           4 months ago
  • 0.7.0-beta.1                                ...           4 months ago
  • 0.6.0                                ...           4 months ago
  • 0.5.1                                ...           4 months ago
  • 0.5.0                                ...           4 months ago
  • 0.5.0-beta.1                                ...           4 months ago
  • 0.4.1                                ...           5 months ago
  • 0.4.0                                ...           5 months ago
  • 0.3.6                                ...           5 months ago
  • 0.3.5                                ...           5 months ago
  • 0.3.4                                ...           5 months ago
  • 0.3.3                                ...           5 months ago
  • 0.3.2                                ...           5 months ago
  • 0.3.1                                ...           5 months ago
  • 0.3.0                                ...           5 months ago
  • 0.2.2                                ...           5 months ago
  • 0.2.0                                ...           5 months ago
  • 0.1.8                                ...           5 months ago
  • 0.1.7                                ...           5 months ago
  • 0.1.6                                ...           5 months ago
  • 0.1.5                                ...           5 months ago
  • 0.1.4                                ...           5 months ago
  • 0.1.2                                ...           5 months ago
  • 0.1.3                                ...           5 months ago
  • 0.1.1                                ...           5 months ago
  • 0.1.0                                ...           6 months ago
Downloads
Today 0
This Week 47
This Month 97
Last Day 0
Last Week 14
Last Month 89
Dev Dependencies (8)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |