{"id":21893303,"url":"https://github.com/rooklab/react-component-caching","last_synced_at":"2025-04-04T19:07:57.016Z","repository":{"id":66159576,"uuid":"124128028","full_name":"rookLab/react-component-caching","owner":"rookLab","description":"Speedier server-side rendering with component caching in React 16","archived":false,"fork":false,"pushed_at":"2019-03-25T03:49:20.000Z","size":487,"stargazers_count":388,"open_issues_count":8,"forks_count":26,"subscribers_count":15,"default_branch":"master","last_synced_at":"2024-05-02T02:12:41.228Z","etag":null,"topics":["caching","component-caching","lru-cache","memcached","node-streams","nodejs","react","react-dom-server","redis","server-side-rendering","serverside-rendering"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rookLab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-03-06T19:29:28.000Z","updated_at":"2024-06-19T16:01:48.279Z","dependencies_parsed_at":"2023-06-07T21:45:27.838Z","dependency_job_id":null,"html_url":"https://github.com/rookLab/react-component-caching","commit_stats":{"total_commits":92,"total_committers":5,"mean_commits":18.4,"dds":"0.40217391304347827","last_synced_commit":"f4f1a273cea7ba06b6886f538169dd4511d130c4"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rookLab%2Freact-component-caching","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rookLab%2Freact-component-caching/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rookLab%2Freact-component-caching/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rookLab%2Freact-component-caching/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rookLab","download_url":"https://codeload.github.com/rookLab/react-component-caching/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247234921,"owners_count":20905854,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["caching","component-caching","lru-cache","memcached","node-streams","nodejs","react","react-dom-server","redis","server-side-rendering","serverside-rendering"],"created_at":"2024-11-28T13:13:21.022Z","updated_at":"2025-04-04T19:07:56.993Z","avatar_url":"https://github.com/rookLab.png","language":"JavaScript","readme":"# React Component Caching\n\n## Overview\nReact Component Caching is a component-level caching library for faster server-side rendering with React 16.  \n- Use any of React's four server-side rendering methods. Rendering is **asynchronous**.\n- Cache components using a simple or template strategy.\n- Choose from three cache implementations (LRU, Redis, or Memcached).\n\n## Installation\nUsing npm:\n```shell\n$ npm install react-component-caching\n```\n\n## Usage\n### In Node rendering server:\nInstantiate 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`.\n\n**Note: All of these methods are asynchronous, and return a promise. To use them, `await` the response before rendering**\n```javascript\nconst ReactCC = require(\"react-component-caching\");\nconst cache = new ReactCC.ComponentCache();\n\napp.get('/example', async (req,res) =\u003e {\n    const renderString = await ReactCC.renderToString(\u003cApp /\u003e, cache);\n    res.send(renderString);\n});\n\n// ...\n```\n\n### In React app:\nTo flag a component for caching, simply add a `cache` property to it. \n\n```javascript\nexport default class App extends Component {\n    render() {\n        return (\n            \u003cdiv\u003e\n                \u003cComponentNotToBeCached /\u003e\n                \u003cComponentToCache cache /\u003e\n            \u003c/div\u003e\n        );\n    }\n}\n// ...\n```\n\n## Templatizing Cached Components\nThe 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. \n\nTo 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.**\n\n```javascript\nexport default class App extends Component {\n    render() {\n        return (\n            \u003cdiv\u003e\n                \u003cComponentNotToBeCached /\u003e\n                \u003cComponentToCache cache /\u003e\n                \u003cComponentToTemplatize\n                    templatizedProp1=\"value1\"\n                    templatizedProp2=\"value2\"\n                    nonTemplatizedProp=\"anotherValue\"\n                    cache\n                    templatized={[\"templatizedProp1\", \"templatizedProp2\"]} /\u003e\n            \u003c/div\u003e\n        );\n    }\n}\n// ...\n```\n## Streaming HTML Markup\nTo 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:\n(\n`component`: The React component being rendered\n`cache`: The component cache object\n`res`: The response object that Express provides\n`htmlStart`: Start of html markup in string form\n`htmlEnd`: End of html markup in string form\n).\nThe benefit that comes with streaming is faster time to first byte, which translates to faster viewing of page content. \n\n## Cache Options\nReact 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.\n\n**Standard (LRU) Cache Example:**\n\n```javascript\nconst ReactCC = require(\"react-component-caching\");\nconst cache = new ReactCC.ComponentCache();\n```\n\n**Redis Example:**\n\n```javascript\nconst ReactCC = require(\"react-component-caching\");\nconst redis = require(\"redis\");\nconst cache = redis.createClient();\n```\n\n**Memcached Example:**\n\n```javascript\nconst ReactCC = require(\"react-component-caching\");\nconst Memcached = require(\"memcached\");\nconst cache = new Memcached(server location, options);\n\n// If using Memcached, make sure to pass in the lifetime of the data (in seconds) as a number.\nReactCC.renderToString(\u003cApp /\u003e, cache, 1000);\n```\n\n## API\n\n### React Component Caching\nReact 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.\n\n### ComponentCache\n- `size`: (*Optional*) An integer representing the maximum size (in characters) of the cache. Defaults to 1 million.\n\n**Example:**\n```javascript\nconst cache = new ReactCC.ComponentCache();\n```\n\n### renderToString\n- `component`: The React component being rendered\n- `cache`: The component cache\n- `memLife`: (*Only if using Memcached*) A number representing the lifetime (in seconds) of each Memcached entry. Defaults to 0.\n\n**Example:**\n```javascript\nReactCC.renderToString(\u003cApp /\u003e, cache);\n```\n\n### renderToStaticMarkup\n- `component`: The React component being rendered\n- `cache`: The component cache\n- `memLife`: (*Only if using Memcached*) An integer representing the lifetime (in seconds) of each Memcached entry. Defaults to 0.\n\n**Example:**\n```javascript\nReactCC.renderToStaticMarkup(\u003cApp /\u003e, cache);\n```\n\n### renderToNodeStream\n- `component`: The React component being rendered\n- `cache`: The component cache object\n- `res`: The response object that Express provides\n- `htmlStart`: Start of html markup in string form\n- `htmlEnd`: End of html markup in string form\n- `memLife`: (*Only if using Memcached*) An integer representing the lifetime (in seconds) of each Memcached entry. Defaults to 0.\n\n**Example:**\n```javascript\nlet htmlStart = '\u003chtml\u003e\u003chead\u003e\u003ctitle\u003ePage\u003c/title\u003e\u003c/head\u003e\u003cbody\u003e\u003cdiv id=\"react-root\"\u003e';\nlet htmlEnd =  '\u003c/div\u003e\u003c/body\u003e\u003c/html\u003e';\nReactCC.renderToNodeStream(\u003cApp /\u003e, cache, res, htmlStart, htmlEnd);\n```\n\n### renderToStaticNodeStream\n- `component`: The React component being rendered\n- `cache`: The component cache object\n- `res`: The response object that Express provides\n- `htmlStart`: Start of html markup in string form\n- `htmlEnd`: End of html markup in string form\n- `memLife`: (*Only if using Memcached*) An integer representing the lifetime (in seconds) of each Memcached entry. Defaults to 0.\n\n**Example:**\n```javascript\nlet htmlStart = '\u003chtml\u003e\u003chead\u003e\u003ctitle\u003ePage\u003c/title\u003e\u003c/head\u003e\u003cbody\u003e\u003cdiv id=\"react-root\"\u003e';\nlet htmlEnd = '\u003c/div\u003e\u003c/body\u003e\u003c/html\u003e';\nReactCC.renderToStaticNodeStream(\u003cApp /\u003e, cache, res, htmlStart, htmlEnd);\n```\n\n## Authors\n- [Mejin Leechor](https://github.com/mejincodes)\n- [Annie DiFiore](https://github.com/adifiore)\n- [Steven Lee](https://github.com/stevedorke)\n- [Tim Hong](https://github.com/tjhong30)\n- [Zihao Li](https://github.com/kodakyellow)","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frooklab%2Freact-component-caching","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frooklab%2Freact-component-caching","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frooklab%2Freact-component-caching/lists"}