UNPKG

@platform/react.ssr

Version:

An SSR (server-side-render) system for react apps bundled with ParcelJS and hosted on S3.

113 lines (112 loc) 5.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var url_1 = require("url"); var common_1 = require("../common"); var manifest_1 = require("../manifest"); function init(args) { var _this = this; var router = args.router, manifestUrl = args.manifestUrl, baseUrl = args.baseUrl; var manifestFromCacheOrS3 = function (args) { if (args === void 0) { args = {}; } var force = args.force; return manifest_1.Manifest.get({ manifestUrl: manifestUrl, baseUrl: baseUrl, force: force, loadBundleManifest: true }); }; router.get('/.manifest/summary', function (req) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var manifest, sites; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4, manifestFromCacheOrS3()]; case 1: manifest = _a.sent(); sites = manifest.sites.reduce(function (acc, next) { var _a; var name = next.name, version = next.version, size = next.size; return tslib_1.__assign({}, acc, (_a = {}, _a[name] = { version: version, size: size }, _a)); }, {}); return [2, { status: 200, headers: { 'Cache-Control': "s-maxage=10, stale-while-revalidate" }, data: { sites: sites }, }]; } }); }); }); router.get('/.manifest', function (req) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var manifest; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4, manifestFromCacheOrS3()]; case 1: manifest = _a.sent(); return [2, { status: 200, headers: { 'Cache-Control': "s-maxage=10, stale-while-revalidate" }, data: manifest.toObject(), }]; } }); }); }); router.post('/.manifest', function (req) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var manifest; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4, manifestFromCacheOrS3({ force: true })]; case 1: manifest = _a.sent(); common_1.log.info("Manifest cache reset."); return [2, { status: 200, data: manifest.toObject(), }]; } }); }); }); router.get('*', function (req) { return tslib_1.__awaiter(_this, void 0, void 0, function () { var manifest, status_1, message, hostname, site, status_2, message, url, route, headers, entry, status_3, url_2, message, location, status_4; return tslib_1.__generator(this, function (_a) { switch (_a.label) { case 0: return [4, manifestFromCacheOrS3()]; case 1: manifest = _a.sent(); if (!manifest.ok) { status_1 = manifest.status; message = 'Manifest could not be loaded.'; return [2, { status: status_1, data: { status: status_1, message: message } }]; } hostname = (req.headers.host || '').split(':')[0]; site = manifest.site.byHost(hostname); if (!site) { status_2 = 404; message = "A site definition for the domain '" + hostname + "' does not exist in the manifest."; return [2, { status: status_2, data: { status: status_2, message: message } }]; } url = url_1.parse(req.url || '', false); route = site.route(url.pathname); headers = { 'Cache-Control': "s-maxage=10, stale-while-revalidate" }; if (!route) return [3, 3]; return [4, route.entry()]; case 2: entry = _a.sent(); if (entry.ok) { return [2, { headers: headers, data: entry.html }]; } else { status_3 = entry.status, url_2 = entry.url; message = "Failed to get entry HTML from CDN."; return [2, { status: status_3, data: { status: status_3, message: message, url: url_2 } }]; } _a.label = 3; case 3: location = site.redirectUrl(url.pathname); if (location) { status_4 = 307; return [2, { status: status_4, headers: headers, data: location }]; } return [2, undefined]; } }); }); }); return router; } exports.init = init;