UNPKG

@sigi/ssr

Version:

Server side rendering support for sigi framework

48 lines 2.3 kB
import { jsx as _jsx } from "react/jsx-runtime"; import { GLOBAL_KEY_SYMBOL, RETRY_KEY_SYMBOL } from '@sigi/core'; import serialize from 'serialize-javascript'; var ScriptId = 'sigi-persisted-data'; var StateToPersist = (function () { function StateToPersist(dataToPersist, actionsToRetry) { this.dataToPersist = dataToPersist; this.actionsToRetry = actionsToRetry; } StateToPersist.prototype.extractToScriptString = function (withScriptTag) { if (withScriptTag === void 0) { withScriptTag = true; } if (this.dataToPersist == null) { return ''; } var contentScript = this.serialize(); return withScriptTag ? "<script id=\"".concat(ScriptId, "\">").concat(contentScript, "</script>") : contentScript; }; StateToPersist.prototype.renderToJSX = function () { if (this.dataToPersist == null) { return null; } return _jsx("script", { id: ScriptId, dangerouslySetInnerHTML: { __html: this.serialize() } }); }; StateToPersist.prototype.renderToDocument = function (doc) { if (this.dataToPersist == null) { return doc; } var endBodyPosition = doc.indexOf('</body>'); if (endBodyPosition === -1) { return doc; } return doc.substr(0, endBodyPosition) + this.extractToScriptString() + doc.substring(endBodyPosition); }; StateToPersist.prototype.extractToJSONScriptString = function () { if (this.dataToPersist == null) { return ''; } var stateContent = serialize(this.dataToPersist, { isJSON: true }); return "<script id=\"".concat(GLOBAL_KEY_SYMBOL, "\" type=\"application/json\">").concat(stateContent, "</script><script id=\"").concat(RETRY_KEY_SYMBOL, "\" type=\"application/json\">").concat(JSON.stringify(this.actionsToRetry), "</script>"); }; StateToPersist.prototype.serialize = function () { var content = serialize(this.dataToPersist, { isJSON: true }); return "window['".concat(GLOBAL_KEY_SYMBOL, "']=").concat(content, ";window['").concat(RETRY_KEY_SYMBOL, "']=").concat(JSON.stringify(this.actionsToRetry)); }; return StateToPersist; }()); export { StateToPersist }; //# sourceMappingURL=state-to-persist.js.map