npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@shopify/react-server

v5.2.3

Published

Utilities for React server-side rendering

Downloads

19,216

Readme

@shopify/react-server

Build Status 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 {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 = /* do something special with the koa context */;
    // any special data we add to the incoming request in our rails controller we can access here to pass into our component
    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 {
  // 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;
  // the name of the asset on the cdn, or a function of Koa.Context to a name
  assetName?: string | (ctx: Context) => 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;
  // a function similar to option but used to customize the production SSR error page
  // note: using this assumes that there is an entry point named `error` in your project
  renderError: RenderFunction;
  // Control when to render raw stack trace, defaults to development only.
  renderRawErrorMessage?: boolean;
  // additional props to pass into the Html component, or a function that takes a Koa.Context and returns a props object
  // See https://github.com/Shopify/quilt/blob/main/packages/react-html/README.md#html-
  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.