-
-
Notifications
You must be signed in to change notification settings - Fork 629
Expand file tree
/
Copy pathserverRenderReactComponent.ts
More file actions
127 lines (110 loc) · 4.17 KB
/
Copy pathserverRenderReactComponent.ts
File metadata and controls
127 lines (110 loc) · 4.17 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
import ReactDOMServer from 'react-dom/server';
import type { ReactElement } from 'react';
import ComponentRegistry from './ComponentRegistry';
import createReactOutput from './createReactOutput';
import {isServerRenderHash, isPromise} from
'./isServerRenderResult';
import buildConsoleReplay from './buildConsoleReplay';
import handleError from './handleError';
import type { RenderParams, RenderResult, RenderingError } from './types/index';
export default function serverRenderReactComponent(options: RenderParams): null | string | Promise<RenderResult> {
const { name, domNodeId, trace, props, railsContext, returnPromise, throwJsErrors } = options;
let renderResult: null | string | Promise<string> = null;
let hasErrors = false;
let renderingError: null | RenderingError = null;
try {
const componentObj = ComponentRegistry.get(name);
if (componentObj.isRenderer) {
throw new Error(`\
Detected a renderer while server rendering component '${name}'. \
See https://github.com/shakacode/react_on_rails#renderer-functions`);
}
const reactElementOrRouterResult = createReactOutput({
componentObj,
domNodeId,
trace,
props,
railsContext,
});
if (isServerRenderHash(reactElementOrRouterResult)) {
// We let the client side handle any redirect
// Set hasErrors in case we want to throw a Rails exception
hasErrors = !!(reactElementOrRouterResult as {routeError: Error}).routeError;
if (hasErrors) {
console.error(
`React Router ERROR: ${JSON.stringify((reactElementOrRouterResult as {routeError: Error}).routeError)}`,
);
}
if ((reactElementOrRouterResult as {redirectLocation: {pathname: string; search: string}}).redirectLocation) {
if (trace) {
const { redirectLocation } = (reactElementOrRouterResult as {redirectLocation: {pathname: string; search: string}});
const redirectPath = redirectLocation.pathname + redirectLocation.search;
console.log(`\
ROUTER REDIRECT: ${name} to dom node with id: ${domNodeId}, redirect to ${redirectPath}`,
);
}
// For redirects on server rendering, we can't stop Rails from returning the same result.
// Possibly, someday, we could have the rails server redirect.
} else {
renderResult = (reactElementOrRouterResult as { renderedHtml: string }).renderedHtml;
}
} else if (isPromise(reactElementOrRouterResult)) {
if (!returnPromise) {
console.error(`Your render function returned a Promise, which is only supported by a node renderer, not ExceJS.`)
}
renderResult = reactElementOrRouterResult;
} else {
try {
renderResult = ReactDOMServer.renderToString(reactElementOrRouterResult as ReactElement);
} catch (error) {
console.error(
`Invalid call to renderToString. Possibly you have a renderFunction, a function that already
calls renderToString, that takes one parameter. You need to add an extra unused
parameter to identify this function as a renderFunction and not a simple React
Function Component.`);
throw error;
}
}
} catch (e) {
if (throwJsErrors) {
throw e;
}
hasErrors = true;
renderResult = handleError({
e,
name,
serverSide: true,
});
renderingError = e;
}
const consoleReplayScript = buildConsoleReplay();
const addRenderingErrors = (resultObject: RenderResult, renderError: RenderingError) => {
resultObject.renderingError = { // eslint-disable-line no-param-reassign
message: renderError.message,
stack: renderError.stack,
};
}
if(returnPromise) {
const resolveRenderResult = async () => {
const promiseResult = {
html: await renderResult,
consoleReplayScript,
hasErrors,
};
if (renderingError !== null) {
addRenderingErrors(promiseResult, renderingError);
}
return promiseResult;
};
return resolveRenderResult();
}
const result = {
html: renderResult,
consoleReplayScript,
hasErrors,
} as RenderResult;
if (renderingError) {
addRenderingErrors(result, renderingError);
}
return JSON.stringify(result);
}