From b4a66602b6790b27f49738c67b69142bdcc21bcd Mon Sep 17 00:00:00 2001 From: Joel Chen Date: Thu, 7 Jan 2021 17:13:36 -0800 Subject: [PATCH] [fix] handle ssr off when loading subapp (#1780) --- packages/xarc-subapp/src/node/load-v2.ts | 35 ++++++++++++++++-------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/packages/xarc-subapp/src/node/load-v2.ts b/packages/xarc-subapp/src/node/load-v2.ts index b1da4df3c..25448033c 100644 --- a/packages/xarc-subapp/src/node/load-v2.ts +++ b/packages/xarc-subapp/src/node/load-v2.ts @@ -39,7 +39,7 @@ export function loadSubApp(setupContext: any, { props: setupProps }) { // ensure subapp's data prep are executed and embedded as initial state in index.html // console.log("subapp load", name, "useReactRouter", subApp.useReactRouter); - const { name } = props; + const { name, ssr } = props; const subapp = getContainer().get(name); @@ -52,27 +52,38 @@ export function loadSubApp(setupContext: any, { props: setupProps }) { // get framework library const frameworkLib = subapp._frameworkFactory!(); const startTime = Date.now(); - const { props: ssrProps, content: ssrContent } = await frameworkLib.handleSSR!({ - context, - subapp, - props: {}, - request, - path: request.path - }); + + let ssrResult; + + if (ssr) { + ssrResult = await frameworkLib.handleSSR!({ + context, + subapp, + props: {}, + request, + path: request.path + }); + } else { + // TODO: still need to prepare props? like when subapp uses staticProps feature. + ssrResult = { + content: `` + }; + } + const now = Date.now(); let initialStateData = ""; let initialStateScript = ""; - if (!_.isEmpty(ssrProps)) { + if (!_.isEmpty(ssrResult.props)) { const dataId = `${name}-initial-state-${Date.now()}-${++INITIAL_STATE_TAG_ID}`; initialStateData = ` -${JSON.stringify(ssrProps)} +${JSON.stringify(ssrResult.props)} `; initialStateScript = `${xarc}.dyn("${dataId}")`; } outputSpot.add( ` -
${ssrContent}
${initialStateData} +
${ssrResult.content}
${initialStateData} ${xarc}.startSubAppOnLoad(${JSON.stringify(props)}, {getInitialState:function(){return ${initialStateScript}}}); @@ -83,6 +94,8 @@ ${JSON.stringify(ssrProps)} }; asyncProcess(); + + return undefined; } }; }