piral-ssr-utils
v0.15.13
Published
Utilities for server-side rendering of a Piral instance.
Downloads
63
Maintainers
Readme
Piral SSR Utils ·
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.