import path from 'path' ; import { SERVER_RENDER_FUNCTION_NAME } from '@modern-js/utils' ; import mime from 'mime-types' ; import { ModernServerContext } from '../context' ; import { RenderResult , ServerHookRunner } from '../../type' ; import cache from './cache' ; import { SSRServerContext } from './type' ; export const render = async ( ctx , renderOptions , runner ) : Promise < RenderResult > => { const { bundle , distDir , template , entryName , staticGenerate } = renderOptions ; const bundleJS = path . join ( distDir , bundle ) ; const context = { request : { params : ctx . params , pathname : ctx . path , query : ctx . query as Record < string , string > , headers : ctx . headers , cookie : ctx . headers . cookie , } , redirection : { } , template , entryName , distDir , staticGenerate , logger : ctx . logger , metrics : ctx . metrics , } ; runner . extendSSRContext ( context ) ; const serverRender = require ( bundleJS ) [ SERVER_RENDER_FUNCTION_NAME ] ; const html = await cache ( serverRender , ctx ) ( context ) ; const { url , status = 302 } = context . redirection ; if ( url ) { return { content : url , contentType : '' , statusCode : status , redirect : true , } ; } return { content : html , contentType : mime . contentType ( 'html' ) as string , } ; } ;