# React ESI: Blazing-fast Server-Side Rendering for React and Next.js ![CI status](https://github.com/github/docs/actions/workflows/test.yml/badge.svg) [![Coverage Status](https://coveralls.io/repos/github/dunglas/react-esi/badge.svg?branch=master)](https://coveralls.io/github/dunglas/react-esi?branch=master) [![npm version](https://badge.fury.io/js/react-esi.svg)](https://badge.fury.io/js/react-esi) [![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php) React ESI is a super powerful cache library for vanilla [React](https://reactjs.org/) and [Next.js](https://nextjs.org/) applications, that can make highly dynamic applications as fast as static sites. It provides a straightforward way to boost your application's performance by storing **fragments** of server-side rendered pages in **edge cache servers**. It means that after the first rendering, fragments of your pages will be served in a few milliseconds by servers close to your end users! It's a very efficient way to improve the performance and the SEO of your websites and to dramatically reduce both your hosting costs and the energy consumption of these applications. Help the planet, use React ESI! Because it is built on top of the [Edge Side Includes (ESI)](https://www.w3.org/TR/esi-lang) W3C specification, React ESI natively supports most of the well-known cloud cache providers including [Cloudflare Workers](https://blog.cloudflare.com/edge-side-includes-with-cloudflare-workers/), [Akamai](https://www.akamai.com/us/en/support/esi.jsp) and [Fastly](https://docs.fastly.com/guides/performance-tuning/using-edge-side-includes). Of course, React ESI also supports the open source [Varnish cache server](https://varnish-cache.org/intro/index.html#intro) that you can use in your own infrastructure for free ([configuration example](https://github.com/zeit/next.js/blob/canary/examples/with-react-esi/docker/varnish/default.vcl)). Also, React ESI allows the specification of different Time To Live (TTL) per React component and generates the corresponding HTML asynchronously using a secure (signed) URL. The cache server fetches and stores in the cache all the needed fragments (the HTML corresponding to every React component), builds the final page and sends it to the browser. React ESI also allows components to (re-)render client-side without any specific configuration. ![ESI example](https://raw.githubusercontent.com/varnish/Varnish-Book/3bd8894181f5e42f628967d04f40116498d1f7f2/ui/img/esi.png) > Schema from [The Varnish Book](https://info.varnish-software.com/resources/varnish-6-by-example-book) **[Discover React ESI in depth with this presentation](https://dunglas.fr/2019/04/react-esi-blazing-fast-ssr/)** ## Examples - [Next.js, Express, and Varnish](https://github.com/dunglas/react-esi/tree/main/examples/next) - [React, Express, and Varnish](https://github.com/dunglas/react-esi/tree/main/examples/express) ## Install Using NPM: $ npm install react-esi Or using Yarn: $ yarn add react-esi Or using PNPM: $ pnpm add react-esi ## Usage React ESI provides a convenient [Higher Order Component](https://reactjs.org/docs/higher-order-components.html) that will: - replace the wrapped component with an ESI tag server-side (don't worry React ESI also provides the tooling to generate the corresponding fragment); - render the wrapped component client-side, and feed it with the server-side computed props (if any). React ESI automatically calls a `static async` method named `getInitialProps()` to populate the initial props of the component. Server-side, this method can access to the HTTP request and response, for instance, to set the `Cache-Control` header, or some [cache tags](https://api-platform.com/docs/core/performance/#enabling-the-built-in-http-cache-invalidation-system). These props returned by `getInitialProps()` will also be injected in the server-side generated HTML (in a `