UNPKG

@dooboostore/simple-boot-http-server-ssr

Version:
72 lines 3.08 kB
import { __awaiter } from "tslib"; import { JsdomInitializer } from '../initializers/JsdomInitializer'; import { RandomUtils } from '@dooboostore/core/random/RandomUtils'; export class SSRWorker { constructor(factory, otherInstanceSim) { this.factory = factory; this.otherInstanceSim = otherInstanceSim; this.welcomUrl = 'http://localhost'; } run() { return __awaiter(this, void 0, void 0, function* () { this.simpleBootFront = yield this.makeFront(); }); } makeJsdom() { return __awaiter(this, void 0, void 0, function* () { const jsdom = yield new JsdomInitializer(this.factory.frontDistPath, this.factory.frontDistIndexFileName || 'index.html', { url: this.welcomUrl }).run(); return jsdom; }); } makeFront(jsdom) { return __awaiter(this, void 0, void 0, function* () { if (!jsdom) { jsdom = yield this.makeJsdom(); } const name = RandomUtils.uuid(); // const jsdom = await this.makeJsdom(); const window = jsdom.window; window.ssrUse = false; const option = this.factory.factorySimFrontOption(window); // option.name = name; const simpleBootFront = yield this.factory.factory.create(option, this.factory.using, this.factory.domExcludes); simpleBootFront.run(this.otherInstanceSim); simpleBootFront.jsdom = jsdom; return simpleBootFront; }); } getHtml(url) { return __awaiter(this, void 0, void 0, function* () { if (this.simpleBootFront && this.simpleBootFront.option.window.ssrUse) { } else if (this.simpleBootFront) { this.simpleBootFront.option.window.ssrUse = true; delete this.simpleBootFront.option.window.server_side_data; // run yield this.simpleBootFront.goRouting(url); let html = this.simpleBootFront.option.window.document.documentElement.outerHTML; const serverSideData = this.simpleBootFront.option.window.server_side_data; if (serverSideData) { const data = Object.entries(serverSideData).map(([k, v]) => { if (typeof v === 'string') { return `window.server_side_data.${k} = ${v}`; } else { return `window.server_side_data.${k} = ${JSON.stringify(v)}`; } }).join(';'); if (data) { html = html.replace('</head>', `<script> window.server_side_data={}; ${data}; </script></head>`); } } this.makeFront().then(it => { this.simpleBootFront = it; }); return html; } { } }); } } //# sourceMappingURL=SSRWorker.js.map