@trifrost/core
Version:
Blazingly fast, runtime-agnostic server framework for modern edge and node environments
27 lines (26 loc) • 1.11 kB
JavaScript
import { MimeTypes } from '../../../types';
import { StyleEngine } from './Engine';
import { setActiveStyleEngine } from './use';
import { isDevMode } from '../../../utils/Generic';
export function mount(router, path, module) {
/* We cache root content in mem*/
let content;
/* Set mount path on module */
module.setMountPath(path);
router.get(path, ctx => {
if (!content) {
const style_engine = setActiveStyleEngine(new StyleEngine());
/* We wrap in setMountPath(path) and setMountPath(null) to tell the module we're rendering our mounted parts */
module.setMountPath(null);
module.root();
module.setMountPath(path);
content = style_engine.flush({ mode: 'file' });
setActiveStyleEngine(null);
}
/* Set css mime */
ctx.setType(MimeTypes.CSS);
return ctx.text(content,
/* If not in dev mode add cache control */
!isDevMode(ctx.env) ? { status: 200, cacheControl: { type: 'public', maxage: 86400, immutable: true } } : { status: 200 });
});
}