-
Notifications
You must be signed in to change notification settings - Fork 627
/
index.js
176 lines (146 loc) 路 3.75 KB
/
index.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const url = require('url');
const qs = require('querystring');
const { createElement } = require('rax');
const renderer = require('rax-server-renderer');
const { Document, Error: _Error} = require('rax-pwa');
module.exports = class Server {
constructor(options = {}) {
const {
pages,
...baseConfig
} = options;
this.pagesConfig = pages;
this.baseConfig = baseConfig;
}
async render(req, res, options) {
const html = await this.renderToHTML(req, res, options);
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.send(html);
}
async renderToHTML(req, res, options = {}) {
const {
page,
} = options;
const baseConfig = this.baseConfig || {};
const pagesConfig = this.pagesConfig || {};
try {
const pageConfig = pagesConfig[page] || {};
return renderToHTML(req, res, {
...baseConfig,
...pageConfig,
...options
});
} catch (err) {
res.statusCode = err.code === 'ENOENT' ? '404' : '500';
this.logError(err);
const errorPageConfig = pagesConfig.error || {};
return renderToHTML(req, res, {
err,
component: _Error,
...baseConfig,
...errorPageConfig,
...options
});
}
}
logError(err) {
console.log(err);
}
};
function getParsedUrl(req, pathname, query) {
if (!pathname || !query) {
const parsedUrl = url.parse(req.url);
pathname = pathname || parsedUrl.pathname;
query = query || qs.parse(parsedUrl.query);
}
return {
pathname,
query
};
}
class PageNotFoundError extends Error {
constructor(page) {
this.message = `Cannot find module for page: ${page}`;
this.code = 'ENOENT';
}
}
async function renderToHTML(req, res, options) {
const {
err,
page,
pathname,
query,
title,
component,
styles = [],
scripts = [],
document,
shell,
renderOpts
} = options;
if (!component) {
throw new PageNotFoundError(page);
}
const parsedUrl = getParsedUrl(req, pathname, query);
const ctx = { req, res, ...parsedUrl, err };
let pageData;
let pageHtml;
if (shell && shell.component) {
const result = await renderShell({
ctx,
shell,
component,
renderOpts
});
pageData = result.data;
pageHtml = result.html;
} else {
pageData = await getInitialProps(component, ctx);
const pageElement = createElement(component, pageData);
pageHtml = renderer.renderToString(pageElement, renderOpts);
}
const documentComponent = document.component || Document;
const pageTitle = title || document.title;
const documentData = await getInitialProps(documentComponent, ctx);
const documentElement = createElement(documentComponent, {
title: pageTitle,
pageHtml,
pageData: JSON.stringify(pageData),
styles,
scripts,
...documentData
});
const html = '<!doctype html>' + renderer.renderToString(documentElement, renderOpts);
return html;
}
async function renderShell(options) {
const {
ctx,
shell,
component,
renderOpts
} = options;
const shellComponent = shell.component;
const data = await getInitialProps(shellComponent, {
...ctx,
Component: component
});
const shellElement = createElement(shellComponent, {
...data,
Component: component
});
const html = renderer.renderToString(shellElement, renderOpts);
return {
data,
html
};
}
async function getInitialProps(Component, ctx) {
if (!Component.getInitialProps) return {};
const props = await Component.getInitialProps(ctx);
if (!props || typeof props !== 'object') {
const message = `"getInitialProps()" should resolve to an object. But found "${props}" instead.`;
throw new Error(message);
}
return props;
}