@platform/react.ssr
Version:
A lightweight SSR (server-side-rendering) system for react apps bundled with ParcelJS and hosted on S3.
63 lines (62 loc) • 2 kB
JavaScript
import { fs } from './common';
const CACHE = { 'Cache-Control': `s-maxage=5, stale-while-revalidate` };
export function init(args) {
const { router, getManifest } = args;
router.get('/.manifest/summary', async (req) => {
const manifest = await getManifest();
const sites = manifest.sites.reduce((acc, site) => {
const info = toSiteInfo({ site });
return Object.assign({}, acc, { [site.name]: info });
}, {});
return {
status: 200,
headers: CACHE,
data: { sites },
};
});
router.get('/.manifest/summary/:site', async (req) => {
const manifest = await getManifest();
const site = manifest.site.byName(req.params.site);
if (site) {
return {
status: 200,
headers: CACHE,
data: toSiteInfo({ site, name: true, files: true }),
};
}
else {
const status = 404;
const message = `Site named '${req.params.site || 'UNNAMED'}' not found in manifest.`;
return {
status,
data: { status, message },
};
}
});
router.get('/.manifest', async (req) => {
const manifest = await getManifest({ force: true });
return {
status: 200,
headers: CACHE,
data: manifest.toObject(),
};
});
}
function toSiteInfo(args) {
const { site } = args;
const { name, version, size } = site;
const domain = site.domain.join(', ');
let info = { version, size, domain };
if (args.name) {
info = Object.assign({ name }, info);
}
if (args.files) {
const files = site.files.map(file => {
const { path, bytes } = file;
const size = fs.size.toString(bytes);
return { path, size, bytes };
});
info = Object.assign({}, info, { files });
}
return info;
}