You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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)
4
+
React Component Caching is a component-level caching library for faster server-side rendering with React 16.
5
+
- Use any of React's four server-side rendering methods. Rendering is **asynchronous**.
6
+
- Cache components using a simple or template strategy.
7
+
- Choose from three cache implementations (LRU, Redis, or Memcached).
8
8
9
9
## Installation
10
10
Using npm:
11
11
```shell
12
-
$ npm install reactcc
12
+
$ npm install react-component-caching
13
13
```
14
14
15
15
## Usage
16
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.
17
+
Instantiate a cache and pass it to any rendering method (`renderToString`, `renderToStaticMarkup`, `renderToNodeStream`, or `renderToStaticNodeStream`) as a second argument. Wherever you would use `ReactDOM.renderToString`, use `ReactCC.renderToString`.
18
+
19
+
**Note: All of these methods are asynchronous, and return a promise. To use them, `await` the response before rendering**
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.
33
+
To flag a component for caching, simply add a `cache` property to it.
<ComponentToTemplatize cache templatized='props to templatize'/>
37
42
</div>
38
43
);
39
44
}
40
45
}
41
46
// ...
42
47
```
43
48
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.
49
+
## Templatizing Cached Components
50
+
The example above employs a simple caching strategy: a rendered component is saved with its prop values. Each time the component is rendered with different prop values, a separate copy is saved to the cache. If a component is frequently rendered with different prop values, you may prefer to cache a template of the component to save space in the cache. The template strategy stores a version of the component with placeholders (e.g. `{{0}}`, `{{1}}`) in place of actual prop values.
46
51
47
-
**ReactCC LRU Cache Example:**
52
+
To create a cache template, add both `cache` and `templatized` to the component along with an array of props to templatize. Templatized props should have **string** or **number** values. **Be aware that templates are not currently supported with the `renderToNodeStream` or `renderToStaticNodeStream` methods.**
To use streaming on the server side, use either the renderToStaticNodeStream or renderToNodeStream function. Both streaming option works with caching, but not yet compatible with templatization. To use the streaming functions, simply pass in these 5 arguments:
75
+
(
76
+
`component`: The React component being rendered
77
+
`cache`: The component cache object
78
+
`res`: The response object that Express provides
79
+
`htmlStart`: Start of html markup in string form
80
+
`htmlEnd`: End of html markup in string form
81
+
).
82
+
The benefit that comes with streaming is faster time to first byte, which translates to faster viewing of page content.
83
+
84
+
## Cache Options
85
+
React Component Caching 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.
//Make sure to pass in the lifetime of the data (in seconds) as a number.
109
+
//If using Memcached, make sure to pass in the lifetime of the data (in seconds) as a number.
77
110
ReactCC.renderToString(<App />, cache, 1000);
78
111
```
79
112
80
-
## Templatizing Cached Components
81
-
Insert description and implementation here
82
-
83
113
## API
84
114
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.
115
+
### React Component Caching
116
+
React Component Caching gives you access to all four of React 16's server-side rendering methods, as well as additional functionality. Available methods are described below.
87
117
88
118
### ComponentCache
89
119
-`size`: (*Optional*) An integer representing the maximum size (in characters) of the cache. Defaults to 1 million.
@@ -94,7 +124,7 @@ const cache = new ReactCC.ComponentCache();
94
124
```
95
125
96
126
### renderToString
97
-
-`component`: The React component being rendered.
127
+
-`component`: The React component being rendered
98
128
-`cache`: The component cache
99
129
-`memLife`: (*Only if using Memcached*) A number representing the lifetime (in seconds) of each Memcached entry. Defaults to 0.
0 commit comments