0Pricing
React Academy · Lesson

React on Cloudflare Workers with Hono

Set up a Cloudflare Worker with Hono and renderToReadableStream to serve React pages from the edge.

React on Cloudflare Workers with Hono is a free React Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is Hono?

Hono is a lightweight, ultra-fast web framework designed to run on edge runtimes including Cloudflare Workers, Deno Deploy, Bun, and Node.js. Its API is inspired by Express but built from the ground up for the Web Standards (Fetch API, Request/Response), making it a natural fit for edge environments.

Creating a Cloudflare Worker with Hono

Scaffolding a new Hono project for Cloudflare Workers is straightforward: npm create hono@latest prompts you to choose a template, select "cloudflare-workers", and creates a project pre-configured with wrangler (Cloudflare's CLI) and the Hono dependency.

Adding React SSR

To server-render React inside a Hono Worker, install react, react-dom, and @types/react, then import renderToReadableStream from react-dom/server. This function is part of React 18's streaming API and is designed for environments that support the Web Streams API — which Cloudflare Workers do.

The Hono Route Handler

A typical SSR route handler calls renderToReadableStream with your root React component, awaits the stream, then uses c.body(stream, { headers: { 'Content-Type': 'text/html' } }) or c.html(htmlString) to send the response. The stream produces HTML chunks that the browser can progressively render.

renderToReadableStream: Return Type

renderToReadableStream returns a Promise that resolves to a ReadableStream of HTML chunks. This ReadableStream conforms to the Web Streams API and can be passed directly as the body of a Response object, making it a perfect fit for the Cloudflare Workers fetch handler.

Wrapping in an HTML Shell

The React stream produces the component tree HTML but not the surrounding document. You must inject the stream into an HTML shell: a string containing the doctype, head with meta tags and the CSS link, opening body tag — then the React stream — then closing body and html tags.

Serving Static Assets

A Vite-built React client bundle needs to be served alongside the Worker. Cloudflare Pages or the Workers Assets feature handles this: c.env.ASSETS.fetch(request) routes asset requests (JS, CSS, images) to the static files uploaded with your Worker deployment.

The worker.js Entry Point

The Worker entry file exports a default object with a fetch handler: export default { fetch: app.fetch }. Hono's app.fetch method is already a compatible fetch handler that receives a Request and returns a Response, so it plugs directly into the Workers runtime.

Local Development with Wrangler

wrangler dev starts a local development server that emulates the Cloudflare Workers runtime on your machine. It watches for file changes and reloads automatically, providing a fast local development loop before deploying to production.

Deploying to Cloudflare

wrangler deploy bundles and uploads your Worker code to Cloudflare's network, making it live at all 300+ edge locations within seconds. The wrangler.toml configuration file specifies your account ID, Worker name, and any KV namespace or D1 database bindings.

Hydration with bootstrapScripts

For client-side interactivity, pass bootstrapScripts: ['/assets/client.js'] to renderToReadableStream. React will inject a script tag into the HTML that loads the client bundle and calls hydrateRoot on the server-rendered DOM, making the page interactive after the JS loads.

renderToReadableStream Return Type

What does renderToReadableStream return?

Lesson Recap

Hono provides a lightweight, edge-native HTTP framework that integrates cleanly with React's renderToReadableStream. The Worker entry exports app.fetch directly to the Workers runtime, static assets are served via ASSETS bindings, and wrangler handles both local dev and production deployment. bootstrapScripts connect the streamed HTML to the client-side hydration bundle.

Frequently asked questions

Is the “React on Cloudflare Workers with Hono” lesson free?

Yes — the full text of “React on Cloudflare Workers with Hono” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.

What will I learn in “React on Cloudflare Workers with Hono”?

Set up a Cloudflare Worker with Hono and renderToReadableStream to serve React pages from the edge. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start React Academy?

No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “React on Cloudflare Workers with Hono” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this React Academy lesson?

Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. What Is Edge Rendering and Why It Matters
  2. React on Cloudflare Workers with Hono
  3. Streaming SSR from the Edge
  4. Edge Caching Strategies for React Apps
← Back to React Academy