@platform/react.ssr
Version:
A lightweight SSR (server-side-rendering) system for react apps bundled with ParcelJS and hosted on S3.
75 lines (74 loc) • 2.6 kB
JavaScript
import { fs, R } 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(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 name = (req.params.site || '').toString();
const site = manifest.site.byName(name);
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 };
});
const groups = [
{ ext: ['js'], group: 'javascript' },
{ ext: ['html', 'htm'], group: 'html' },
{ ext: ['css'], group: 'css' },
{ ext: ['png', 'jpeg', 'jpg', 'gif', 'ico'], group: 'images' },
];
const findGroup = (path) => {
const ext = fs.extname(path).replace(/^\./, '');
const item = groups.find(item => item.ext.includes(ext));
return item ? item.group : 'asset';
};
info = Object.assign(Object.assign({}, info), { files: R.groupBy(file => findGroup(file.path), files) });
}
return info;
}