Registry / devops / react-router-server

react-router-server

JSON →
library4.2.3jsnpmunverified

Server-side rendering library for React Router v4 (version 4.2.3). Provides utilities for SSR including state fetching, code splitting via Webpack 2, and preloading modules. Requires peer dependencies react (^15.3 || ^16), react-dom, and react-router (^4.0). Ships TypeScript types. The project is in maintenance mode and seeking maintainers. Key differentiators: integrates tightly with React Router v4, offers a custom renderToString that returns a promise, and includes a Module component for code splitting.

npm install react-router-server
INSTALL
IMPORT
SIG · REACT-ROUTER-SERVE
R
react-router-server
devopsjavascriptv4.2.3
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

renderToString
import { renderToString } from 'react-router-server'
const renderToString = require('react-router-server')
Named export only; no default export.
Module
import { Module } from 'react-router-server'
import Module from 'react-router-server'
Named export, not default.
fetchState
import { fetchState } from 'react-router-server'
Named export for fetching state on the client side.
extractModules
import { extractModules } from 'react-router-server'
import * as routerServer from 'react-router-server'
Named export; avoid namespace import unless needed.
withDone
import { withDone } from 'react-router-server'
Named export for higher-order component.

Server-side render a React Router v4 app with async state fetching.

import { renderToString } from 'react-router-server'; import { StaticRouter } from 'react-router'; import React from 'react'; import App from './App'; const context = {}; renderToString( <StaticRouter location="/some/path" context={context}> <App /> </StaticRouter> ).then(({ html }) => { console.log(html); });
Debug
Known issues
deprecatedreact-router-server is in maintenance mode and no longer actively developed. Authors recommend using React Router v6's built-in SSR support.
fix
Migrate to React Router v6 and use its data loading APIs.
affects: >=4.0
gotcharenderToString returns a promise, not a string. Not awaiting the promise will lead to undefined behavior.
fix
Always await or .then() on renderToString result.
affects: >=1.0
gotchaThe Module component requires a System.import call (or dynamic import) as the module prop. Using a plain string will not work.
fix
Use: <Module module={() => import('./SomeComponent')}>...</Module>
affects: >=1.0
gotchafetchState must be called on the server before renderToString. On the client, it is called after mount.
fix
On the server, only call fetchState once; on the client, call it in componentDidMount.
affects: >=1.0
Errors
Common errors & fixes
Cannot find module 'react-router-server'
Package not installed or incorrect import path.
fix
npm install react-router-server, then import named exports.
renderToString is not a function
Default import used instead of named import.
fix
Use import { renderToString } from 'react-router-server';
Module: "module" prop must be a function that returns a promise
The module prop must be a function returning a dynamic import.
fix
Change to: <Module module={() => import('./MyComponent')}>...</Module>
Upgrade
Version history
4.2.3latest on npm
Audit
Dependencies
reactrequiredpeer dependency for rendering
react-domrequiredpeer dependency for server rendering
react-routerrequiredpeer dependency for routing features
Agent activity
2 hits · last 30 days
node
2
Resources
react-router-server — npm install react-router-server · libregistry