@platform/react.ssr
Version:
An SSR (server-side-render) system for react apps bundled with ParcelJS and hosted on S3.
104 lines (103 loc) • 4.02 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var common_1 = require("../common");
var Route = (function () {
function Route(args) {
this.site = args.site;
this.def = args.route;
}
Route.format = function (args) {
var input = args.input;
if (typeof input !== 'object') {
return undefined;
}
var entry = common_1.util.asString(input.entry);
var paths = Array.isArray(input.path) ? input.path : [input.path];
var path = paths.filter(function (path) { return Boolean(path) && typeof path === 'string'; });
return { entry: entry, path: path };
};
Object.defineProperty(Route.prototype, "paths", {
get: function () {
return this.def.path;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Route.prototype, "version", {
get: function () {
return common_1.util.firstSemver(this.site.bundle) || '0.0.0';
},
enumerable: true,
configurable: true
});
Object.defineProperty(Route.prototype, "bundleUrl", {
get: function () {
var base = common_1.util.stripSlashes(this.site.baseUrl);
var path = common_1.util.stripSlashes(this.site.bundle);
return base + "/" + path;
},
enumerable: true,
configurable: true
});
Route.prototype.entry = function (args) {
if (args === void 0) { args = {}; }
return tslib_1.__awaiter(this, void 0, void 0, function () {
var filename, url, res, status, html, version, ok, entry;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (this._entry && !args.force) {
return [2, this._entry];
}
filename = this.def.entry;
url = this.bundleUrl + "/" + filename;
return [4, common_1.http.get(url)];
case 1:
res = _a.sent();
status = 200;
if (!res.ok) {
status = res.status;
}
html = res.ok ? res.body : '';
version = this.version;
html = this.formatHtml({ html: html, filename: filename, version: version });
ok = status.toString().startsWith('2');
entry = {
ok: ok,
status: status,
url: url,
html: html,
};
this._entry = entry;
return [2, entry];
}
});
});
};
Route.prototype.isMatch = function (path) {
if (!this._regexps) {
this._regexps = this.paths.map(function (pattern) { return common_1.pathToRegex(pattern); });
}
return this._regexps.some(function (regex) { return Boolean(regex.exec(path)); });
};
Route.prototype.toObject = function () {
return tslib_1.__assign({}, this.def);
};
Route.prototype.formatHtml = function (args) {
var filename = args.filename, html = args.html, version = args.version;
var entry = this.site.entries.find(function (item) { return item.file === filename; });
if (!html || !entry) {
return html;
}
var $ = common_1.cheerio.load(html);
var root = $("div#" + entry.id);
root.attr('data-version', version);
root.html(entry.html);
$('head').append("<style>" + entry.css + "</style>");
return $.html();
};
Route.create = function (args) { return new Route(args); };
return Route;
}());
exports.Route = Route;