piral-ssr-utils
TypeScript icon, indicating that this package has built-in type declarations

0.15.13 • Public • Published

Piral Logo

Piral SSR Utils · GitHub License npm version tested with jest Gitter Chat

This is a utility library that only has a peer dependency to piral-core.

What piral-ssr-utils offers are utilities that can be used for easily implementing server-side rendering of a Piral instance.

Documentation

The utilities should be used as follows.

On the server use renderFromServer:

import { renderFromServer } from 'piral-ssr-utils';

async function sendIndex(_: express.Request, res: express.Response) {
  const content = await renderFromServer(<App />, {
    getPilet(url) {
      return readRemoteText(url);
    },
    getPiletsMetadata() {
      return readRemoteJson(feedUrl);
    },
    fillTemplate(body, script) {
      return `
        <!doctype html>
        <head><meta charset="utf-8"><title>React SSR Sample</title></head>
        <body>
          <div id="app">${body}</div>${script}
          <script src="index.js"></script>
        </body>
      `;
    },
  });
  res.send(content);
}

Remark: Use, e.g., the incoming request for retrieving custom pilet metadata responses (e.g., using feature flags). Otherwise, fully cache the response. Pilets can/should be cached in any case.

The provided snippet assumes that readRemoteText and readRemoteJson trigger an HTTP request with the method of your choice. While the former just returns the content of the response, the latter already parses the response body's JSON.

The given component App can be as simple as <Piral />, however, for a full alignment with the client-side a custom configuration should be used.

In any case (e.g., for the client hydration) use configForServerRendering when configuring your Piral instance:

import { configForServerRendering } from 'piral-ssr-utils/runtime';

const instance = createInstance(configForServerRendering({
  // ... put your normal configuration here
}));

For more information on using piral-ssr-utils, see our sample repository.

License

Piral is released using the MIT license. For more information see the license file.

Package Sidebar

Install

npm i piral-ssr-utils

Homepage

piral.io

Weekly Downloads

524

Version

0.15.13

License

MIT

Unpacked Size

42.4 kB

Total Files

40

Last publish

Collaborators

  • florianrappl