0.7.11 • Published 2 years ago

@rest-hooks/ssr v0.7.11

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 years ago

Rest Hooks Server Side Rendering helpers

CircleCI Coverage Status npm downloads bundle size npm version PRs Welcome Chat

📖Read The Docs

Hydrate/dehydration utilities for Rest Hooks

Server side

import express from 'express';
import { renderToPipeableStream } from 'react-dom/server';
import { createPersistedCacheProvder } from '@rest-hooks/ssr';

const app = express();
app.get('/*', (req: any, res: any) => {
  const [ServerCacheProvider, controller] = createPersistedCacheProvder();

  controller.fetch(NeededForPage, { id: 5 });

  const { pipe, abort } = renderToPipeableStream(
    <Document assets={assets}>
      <ServerCacheProvider>{children}</ServerCacheProvider>
    </Document>,

    {
      onCompleteShell() {
        // If something errored before we started streaming, we set the error code appropriately.
        res.statusCode = didError ? 500 : 200;
        res.setHeader('Content-type', 'text/html');
        pipe(res);
      },
      onError(x: any) {
        didError = true;
        console.error(x);
        res.statusCode = 500;
        pipe(res);
      },
    },
  );
  // Abandon and switch to client rendering if enough time passes.
  // Try lowering this to see the client recover.
  setTimeout(abort, 1000);
});

app.listen(3000, () => {
  console.log(`Listening at ${PORT}...`);
});

Client

import { hydrateRoot } from 'react-dom';
import { getDatafromDOM } from '@rest-hooks/ssr';

const data = getDatafromDOM();
hydrateRoot(
  document,
  <Document assets={assets}>
    <CacheProvider initialState={data}>
      {children}
      <ServerDataComponent data={data} />
    </CacheProvider>
  </Document>,
);

API

createPersistedStore(managers) => ServerCacheProvider, controller

Used to server side render cache. Renders <ServerDataComponent/> inside to serialize cache so client can hydrate.

ServerDataComponent

Hydrate/dehydrates Rest Hooks cache. Contents are a script with JSON encoding of cache state sent from server.

getDatafromDOM()

Loads data from ServerDataComponent to initialize cache.

0.7.11

2 years ago

0.7.10

2 years ago

1.0.0-next.0

2 years ago

0.7.9

2 years ago

0.7.6

2 years ago

0.7.8

2 years ago

0.7.7

2 years ago

0.7.5

2 years ago

0.7.2

3 years ago

0.7.1

3 years ago

0.7.4

2 years ago

0.7.3

3 years ago

0.7.0-beta.0

3 years ago

0.7.0-beta.1

3 years ago

0.3.0-beta.2

3 years ago

0.3.0

3 years ago

0.3.0-beta.0

3 years ago

0.3.0-beta.1

3 years ago

0.2.9

3 years ago

0.2.8

3 years ago

0.5.0

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.7.0

3 years ago

0.6.0

3 years ago

0.2.7

3 years ago

0.2.7-beta.1

3 years ago

0.2.7-beta.0

3 years ago

0.2.6

3 years ago

0.2.5

3 years ago

0.2.7-beta.2

3 years ago

0.2.1

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.4

3 years ago

0.2.0

3 years ago

0.1.2

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.1

3 years ago

0.1.1-beta.0

3 years ago

0.1.0

3 years ago