React Suspense for static site generation

In 18.2 of React there is an addition of Suspense. This allows an interface for handling async data sources and interactions for smoother interface management and also handling concurrent rendering.

For static site generation we can use these new suspense functionality to process data and create static HTML pages. For my usage I want to use React as a templating language. Then we can use data (ex. JSON) to populate our template.

My initial approach was full of issues due to my knowledge of how Suspense works. Suspense works on async data but you need to wrap it in a sync function. The function will be executed until it completes without throwing an error.

Wrap promise

function wrapPromise(promise) {
  // Keep track of the status of the result
  let status = "pending";

  // We keep track of the result of the promise here
  let result;

  // activate the promise to wait for the response
  const suspense = promise.then(
    (r) => {
      status = "ok";
      result = r;
    (e) => {
      status = "error";
      result = e;

  // Returning an object here allows us to access the variables in this function
  return {
    read() {
      if (status == "pending") {
        // we are not complete, throw the promise again
        throw suspense;
      } else if (status == "ok") {
        return result;
      } else {
        throw e;
// We wrap our promise in our sync function
const fetchPhotos = wrapPromise(
    .then((resp) => resp.json())
    .then((results) =>,

Suspense components

File: photos.jsx

function Photos() {
    const photos =;

    return <div>
        { => {
            return <div>{photo.title}</div>

export default Photos

File: app.jsx

function App() {
  return (
        <meta charset="utf-8" />
        <Suspense fallback={<div>loading...</div>}>
          <Photos />

export default App;

A few different configurations for how these pieces could work together. We are not making a client version of React here we can make some concessions of how we organize it. If you are doing a hybrid solution, you may need to do other things.

Render to readable stream

In this example, I’m using bun but only small changes to make it work for node.js, Deno, Cloudflare workers or other runtimes.

const stream = await renderToReadableStream(<App />, {
  onError(error) {

// Wait for all the suspense data sources to complete
await stream.allReady;

// Convert the stream to text (using the Bun functionality)
const html = await Bun.readableStreamToText(stream);

Then you can use the HTML to save to a file or serve from the readable stream you want a dynamic interface.