forked from nullstack/nullstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprerender.js
More file actions
74 lines (70 loc) · 2.19 KB
/
prerender.js
File metadata and controls
74 lines (70 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import environment from './environment';
import project from './project';
import Router from './router';
import generator from './generator';
import {generateParams} from './params';
import render from './render';
import settings from './settings';
import worker from './worker';
import printError from './printError';
import {generateContext} from './client';
import generateTree from '../shared/generateTree';
import Routable from '../plugins/routable';
import Bindable from '../plugins/bindable';
import Datable from '../plugins/datable';
import Parameterizable from '../plugins/parameterizable';
export async function prerender(request, response) {
const context = {};
context.page = {image: '/image-1200x630.png', status: 200};
context.project = project;
context.environment = environment;
context.settings = settings;
context.params = generateParams(request.originalUrl);
context.router = new Router(request, response);
const online = context.router.url !== `/offline-${environment.key}`;
context.worker = {...worker, online, responsive: online};
const scope = {};
scope.instances = {};
scope.segments = context.params;
scope.request = request;
scope.response = response;
scope.head = '';
scope.body = '';
scope.context = context;
scope.generateContext = generateContext(context);
scope.plugins = [
new Parameterizable({scope}),
new Routable({scope}),
new Datable({scope}),
new Bindable({scope})
]
try {
const tree = await generateTree(generator.starter(), scope);
scope.body = render(tree, scope);
if(!online) {
context.page.status = 200;
}
} catch(error) {
printError(error);
context.page.status = 500;
} finally {
if(context.page.status !== 200) {
for(const key in scope.routes) {
delete scope.routes[key];
}
for(const key in scope.instances) {
delete scope.instances[key];
}
scope.head = '';
scope.plugins = [
new Parameterizable({scope}),
new Routable({scope}),
new Datable({scope}),
new Bindable({scope})
]
const tree = await generateTree(generator.starter(), scope);
scope.body = render(tree, scope);
}
}
return scope;
}