|
| 1 | +# ReactCC |
| 2 | + |
| 3 | +## Overview |
| 4 | +ReactCC is a component-level caching library for rendering React components on the server. |
| 5 | +- Use any of React's four server-side rendering methods |
| 6 | +- Cache simple components or templates |
| 7 | +- Choose from three cache implementations (LRU, Redis, or Memcached) |
| 8 | + |
| 9 | +## Installation |
| 10 | +Using npm: |
| 11 | +```shell |
| 12 | +$ npm install --save rapscallion |
| 13 | +``` |
| 14 | + |
| 15 | +## Usage |
| 16 | +### In Node rendering server: |
| 17 | +Instantiate a cache and pass it into any rendering method as a second argument. Wherever you would use ReactDOM.renderToString, use ReactCC.renderToString. |
| 18 | +```javascript |
| 19 | +const ReactCC = require("reactcc"); |
| 20 | +const cache = ReactCC.ComponentCache(); |
| 21 | +ReactCC.renderToString(<App />, cache>) |
| 22 | + |
| 23 | +// ... |
| 24 | +``` |
| 25 | + |
| 26 | +### In React app: |
| 27 | +To flag a component for caching, simply add a 'cache' property to it. To create a cache template, add both 'cache' and 'templatized', along with an array of props to templatize. |
| 28 | + |
| 29 | +```javascript |
| 30 | +export default class App extends Component { |
| 31 | + render() { |
| 32 | + return ( |
| 33 | + <div> |
| 34 | + <ComponentNotToBeCached /> |
| 35 | + <ComponentToCache cache /> |
| 36 | + <ComponentToTemplatize cache templatized='props to templatize' /> |
| 37 | + </div> |
| 38 | + ); |
| 39 | + } |
| 40 | +} |
| 41 | +// ... |
| 42 | +``` |
| 43 | + |
| 44 | +## Cache Options |
| 45 | +ReactCC provides its own cache implementation as well as support for Redis and Memcached. Simply create your preferred cache and pass it into one of the rendering methods. |
| 46 | + |
| 47 | +**ReactCC LRU Cache Example:** |
| 48 | + |
| 49 | +```javascript |
| 50 | +const ReactCC = require("reactcc"); |
| 51 | + |
| 52 | +const cache = ReactCC.ComponentCache(); |
| 53 | + |
| 54 | +ReactCC.renderToString(<App />, cache); |
| 55 | +``` |
| 56 | + |
| 57 | +**Redis Example:** |
| 58 | + |
| 59 | +```javascript |
| 60 | +const ReactCC = require("reactcc"); |
| 61 | +const redis = require("redis"); |
| 62 | + |
| 63 | +const cache = redis.createClient(); |
| 64 | + |
| 65 | +ReactCC.renderToString(<App />, cache); |
| 66 | +``` |
| 67 | + |
| 68 | +**Memcached Example:** |
| 69 | + |
| 70 | +```javascript |
| 71 | +const ReactCC = require("reactcc"); |
| 72 | +const Memcached = require("memcached"); |
| 73 | + |
| 74 | +const cache = new Memcached(server location, options); |
| 75 | + |
| 76 | +// Make sure to pass in the lifetime of the data (in seconds) as a number. |
| 77 | +ReactCC.renderToString(<App />, cache, 1000); |
| 78 | +``` |
| 79 | + |
| 80 | +## Templatizing Cached Components |
| 81 | +Insert description and implementation here |
| 82 | + |
| 83 | +## API |
| 84 | + |
| 85 | +### ReactCC |
| 86 | +ReactCC gives you access to all four of React 16's server-side rendering methods, as well as additional functionality. ReactCC methods are described below. |
| 87 | + |
| 88 | +### ComponentCache |
| 89 | +- `size`: (Optional) An integer representing the maximum size (in characters) of the cache. Defaults to 1 million. |
| 90 | + |
| 91 | +**Example:** |
| 92 | +```javascript |
| 93 | +const cache = new ReactCC.ComponentCache(); |
| 94 | +``` |
| 95 | + |
| 96 | +### renderToString |
| 97 | +- `component`: The React component being rendered. |
| 98 | +- `cache`: The component cache |
| 99 | +- `memLife`: (Only if using Memcached) A number representing the lifetime (in seconds) of each Memcached entry. Defaults to 0. |
| 100 | + |
| 101 | +**Example:** |
| 102 | +```javascript |
| 103 | +ReactCC.renderToString(<App />, cache); |
| 104 | +``` |
| 105 | + |
| 106 | +### renderToStaticMarkup |
| 107 | +- `component`: The React component being rendered. |
| 108 | +- `cache`: The component cache |
| 109 | +- `memLife`: (Only if using Memcached) An integer representing the lifetime (in seconds) of each Memcached entry. Defaults to 0. |
| 110 | + |
| 111 | +**Example:** |
| 112 | +```javascript |
| 113 | +ReactCC.renderToStaticMarkup(<App />, cache); |
| 114 | +``` |
| 115 | + |
| 116 | +### renderToNodeStream |
| 117 | +- `component`: The React component being rendered. |
| 118 | +- `cache`: The component cache |
| 119 | +- `memLife`: (Only if using Memcached) An integer representing the lifetime (in seconds) of each Memcached entry. Defaults to 0. |
| 120 | + |
| 121 | +**Example:** |
| 122 | +```javascript |
| 123 | +ReactCC.renderToNodeStream(<App />, cache); |
| 124 | +``` |
| 125 | + |
| 126 | +### renderToStaticNodeStream |
| 127 | +- `component`: The React component being rendered. |
| 128 | +- `cache`: The component cache |
| 129 | +- `memLife`: (Only if using Memcached) An integer representing the lifetime (in seconds) of each Memcached entry. Defaults to 0. |
| 130 | + |
| 131 | +**Example:** |
| 132 | +```javascript |
| 133 | +ReactCC.renderToStaticNodeStream(<App />, cache); |
| 134 | +``` |
0 commit comments