UNPKG

@platform/react.ssr

Version:

A lightweight SSR (server-side-rendering) system for react apps bundled with ParcelJS and hosted on S3.

121 lines (120 loc) 4.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Route = void 0; var tslib_1 = require("tslib"); var common_1 = require("../common"); var path_to_regexp_1 = require("path-to-regexp"); 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: false, configurable: true }); Object.defineProperty(Route.prototype, "version", { get: function () { return common_1.util.firstSemver(this.site.bundle) || '0.0.0'; }, enumerable: false, 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: false, 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.text : ''; 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 path_to_regexp_1.pathToRegexp(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; if (!html) { return html; } var site = this.site; var files = site.files; var entry = site.entries.find(function (item) { return item.file === filename; }); var $ = common_1.cheerio.load(html); $('html').attr('data-version', version); $('html').attr('data-size', site.size); if (entry) { $("div#" + entry.id).html(entry.html); $('head').append("<style>" + entry.css + "</style>"); } files .filter(function (file) { return file.path.endsWith('.js'); }) .forEach(function (file) { return sizeAttr(file.bytes, $("script[src=\"" + common_1.fs.basename(file.path) + "\"]")); }); files .filter(function (file) { return file.path.endsWith('.css'); }) .forEach(function (file) { return sizeAttr(file.bytes, $("link[href=\"" + common_1.fs.basename(file.path) + "\"]")); }); return $.html(); }; Route.create = function (args) { return new Route(args); }; return Route; }()); exports.Route = Route; function sizeAttr(bytes, el) { if (el.length > 0) { el.attr('data-size', common_1.fs.size.toString(bytes, { round: 0 })); } }