@@ -3,11 +3,10 @@ import type { ReactElement } from 'react';
33
44import ComponentRegistry from './ComponentRegistry' ;
55import createReactOutput from './createReactOutput' ;
6- import { isServerRenderHash , isPromise } from
7- './isServerRenderResult' ;
6+ import { isPromise , isServerRenderHash } from './isServerRenderResult' ;
87import buildConsoleReplay from './buildConsoleReplay' ;
98import handleError from './handleError' ;
10- import type { RenderParams , RenderResult , RenderingError } from './types/index ' ;
9+ import type { RenderParams , RenderResult , RenderingError , ServerRenderResult } from './types' ;
1110
1211/* eslint-disable @typescript-eslint/no-explicit-any */
1312
@@ -35,37 +34,37 @@ See https://github.com/shakacode/react_on_rails#renderer-functions`);
3534 } ) ;
3635
3736 const processServerRenderHash = ( ) => {
38- // We let the client side handle any redirect
39- // Set hasErrors in case we want to throw a Rails exception
40- hasErrors = ! ! ( reactRenderingResult as { routeError : Error } ) . routeError ;
41-
42- if ( hasErrors ) {
43- console . error (
44- `React Router ERROR: ${ JSON . stringify ( ( reactRenderingResult as { routeError : Error } ) . routeError ) } ` ,
45- ) ;
46- }
37+ // We let the client side handle any redirect
38+ // Set hasErrors in case we want to throw a Rails exception
39+ const { redirectLocation, routeError } = reactRenderingResult as ServerRenderResult ;
40+ hasErrors = ! ! routeError ;
41+
42+ if ( hasErrors ) {
43+ console . error (
44+ `React Router ERROR: ${ JSON . stringify ( routeError ) } ` ,
45+ ) ;
46+ }
4747
48- if ( ( reactRenderingResult as { redirectLocation : { pathname : string ; search : string } } ) . redirectLocation ) {
49- if ( trace ) {
50- const { redirectLocation } = ( reactRenderingResult as { redirectLocation : { pathname : string ; search : string } } ) ;
51- const redirectPath = redirectLocation . pathname + redirectLocation . search ;
52- console . log ( `\
48+ if ( redirectLocation ) {
49+ if ( trace ) {
50+ const redirectPath = redirectLocation . pathname + redirectLocation . search ;
51+ console . log ( `\
5352 ROUTER REDIRECT: ${ name } to dom node with id: ${ domNodeId } , redirect to ${ redirectPath } ` ,
54- ) ;
55- }
56- // For redirects on server rendering, we can't stop Rails from returning the same result.
57- // Possibly, someday, we could have the rails server redirect.
58- return '' ;
53+ ) ;
5954 }
60- return ( reactRenderingResult as { renderedHtml : string } ) . renderedHtml ;
55+ // For redirects on server rendering, we can't stop Rails from returning the same result.
56+ // Possibly, someday, we could have the rails server redirect.
57+ return '' ;
58+ }
59+ return ( reactRenderingResult as ServerRenderResult ) . renderedHtml as string ;
6160 } ;
6261
6362 const processPromise = ( ) => {
6463 if ( ! renderingReturnsPromises ) {
65- console . error ( 'Your render function returned a Promise, which is only supported by a node renderer, not ExecJS.' )
64+ console . error ( 'Your render function returned a Promise, which is only supported by a node renderer, not ExecJS.' ) ;
6665 }
6766 return reactRenderingResult ;
68- }
67+ } ;
6968
7069 const processReactElement = ( ) => {
7170 try {
@@ -105,11 +104,11 @@ as a renderFunction and not a simple React Function Component.`);
105104 message : renderError . message ,
106105 stack : renderError . stack ,
107106 } ;
108- }
107+ } ;
109108
110- if ( renderingReturnsPromises ) {
109+ if ( renderingReturnsPromises ) {
111110 const resolveRenderResult = async ( ) => {
112- let promiseResult ;
111+ let promiseResult : RenderResult ;
113112
114113 try {
115114 promiseResult = {
@@ -129,7 +128,7 @@ as a renderFunction and not a simple React Function Component.`);
129128 } ) ,
130129 consoleReplayScript,
131130 hasErrors : true ,
132- }
131+ } ;
133132 renderingError = e ;
134133 }
135134
@@ -143,11 +142,11 @@ as a renderFunction and not a simple React Function Component.`);
143142 return resolveRenderResult ( ) ;
144143 }
145144
146- const result = {
147- html : renderResult ,
145+ const result : RenderResult = {
146+ html : await renderResult ,
148147 consoleReplayScript,
149148 hasErrors,
150- } as RenderResult ;
149+ } ;
151150
152151 if ( renderingError ) {
153152 addRenderingErrors ( result , renderingError ) ;
0 commit comments