-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrenderServer.ts
More file actions
179 lines (155 loc) · 4.62 KB
/
renderServer.ts
File metadata and controls
179 lines (155 loc) · 4.62 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
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
177
178
179
/**
* Copyright 2016 DanceDeets.
*/
import fs from 'fs';
import http from 'http';
import express, { Request, Response } from 'express';
import bodyParser from 'body-parser';
import React from 'react';
import ReactDOMServer from 'react-dom/server';
// mjml is kept as external in esbuild config since it uses dynamic requires.
// Email rendering will fail if mjml is not installed, but SSR will still work.
let mjml: ((mjmlData: string) => { html: string }) | null = null;
try {
mjml = require('mjml');
} catch (e) {
console.log('mjml not available - email rendering disabled');
}
// Use environment variables or defaults for Docker deployment
const ADDRESS = process.env.RENDER_SERVER_ADDRESS || '0.0.0.0';
const PORT = parseInt(process.env.RENDER_SERVER_PORT || '8090', 10);
const app = express();
const server = new http.Server(app);
app.use(
bodyParser.json({
limit: '10mb', // This is an internal-only server, so we can handle large payloads
})
);
app.get('/', (req: Request, res: Response) => {
res.end('React render server');
});
interface ComponentModule {
default: React.ComponentType<any>;
HelmetRewind?: () => Record<string, { toString: () => string }>;
}
interface SerializedHead {
[key: string]: string;
}
function serializedHead(component: ComponentModule | null): SerializedHead | null {
if (!component || !component.HelmetRewind) {
return null;
}
const head = component.HelmetRewind();
const serialized: SerializedHead = {};
Object.keys(head).forEach(key => {
serialized[key] = head[key].toString();
});
return serialized;
}
// Cache for loaded components to avoid re-reading files
const componentCache: Record<string, ComponentModule> = {};
interface RenderRequest {
path: string;
props: Record<string, any>;
toStaticMarkup?: boolean;
}
interface RenderResponse {
error: { type: string; message: string; stack?: string } | null;
markup: string | null;
head?: SerializedHead | null;
}
app.post('/render', (req: Request, res: Response) => {
const { path: componentPath, props, toStaticMarkup } = req.body as RenderRequest;
fs.readFile(componentPath, { encoding: 'utf8' }, (err, data) => {
if (err) {
const response: RenderResponse = {
error: {
type: err.constructor.name,
message: err.message,
stack: err.stack,
},
markup: null,
};
res.json(response);
return;
}
let component: ComponentModule | null = null;
let Component: React.ComponentType<any> | null = null;
try {
// Evaluate the bundle to get the component module
// The bundle exports { default: ComponentClass, HelmetRewind?: function }
component = eval(data); // eslint-disable-line no-eval
Component = component!.default;
} catch (e) {
console.error('Error evaluating component:', e);
const error = e as Error;
const response: RenderResponse = {
error: {
type: error.constructor.name,
message: error.message,
stack: error.stack,
},
markup: null,
};
res.json(response);
return;
}
try {
// Create the React element with props
const element = React.createElement(Component!, props);
// Render to string or static markup
const markup = toStaticMarkup
? ReactDOMServer.renderToStaticMarkup(element)
: ReactDOMServer.renderToString(element);
// Get Helmet head data if available
const head = serializedHead(component);
const response: RenderResponse = {
error: null,
markup,
head,
};
res.json(response);
} catch (e) {
console.error('Error rendering component:', e);
const error = e as Error;
const response: RenderResponse = {
error: {
type: error.constructor.name,
message: error.message,
stack: error.stack,
},
markup: null,
};
res.json(response);
}
});
});
interface MjmlRequest {
mjml: string;
}
interface MjmlResponse {
error: { message: string } | null;
html: string | null;
}
app.post('/mjml-render', (req: Request, res: Response) => {
if (!mjml) {
const response: MjmlResponse = {
error: { message: 'mjml not available - please install mjml package' },
html: null,
};
res.json(response);
return;
}
const mjmlData = (req.body as MjmlRequest).mjml;
const result = mjml(mjmlData);
const response: MjmlResponse = {
error: null,
html: result.html,
};
res.json(response);
});
server.listen(PORT, ADDRESS, () => {
console.log(
`Node (react, mjml) server listening at http://${ADDRESS}:${PORT}`
);
});