logo
  • Guide
  • Config
  • Plugin
  • API
  • Examples
  • Community
  • Modern.js 2.x Docs
  • English
    • 简体中文
    • English
    • Commands
      File Conventions
      src/
      App.tsx
      entry.ts
      entry.server.tsx
      modern.runtime.ts
      routes/
      *.[server|node].tsx
      api/
      lambda/*.ts
      server/
      modern.server.ts
      shared/
      config/
      html/
      favicon.*
      icon.*
      mock/
      public/
      upload/
      modern.config.ts
      Runtime
      Core
      createRoot
      render
      RuntimeContext
      Router
      router
      SSR
      NoSSR
      renderStreaming
      renderString
      createRequestHandler
      BFF
      useHonoContext
      Utility
      CSS-In-JS API
      Head
      loadable
      📝 Edit this page
      Previous pageentry.tsNext pagemodern.runtime.ts

      #entry.server.tsx

      When the project initiates server.ssr, Modern.js generates a default Server-Side entry. The sample code is as follows:

      entry.server.tsx
      import {
        renderString,
        createRequestHandler,
      } from '@modern-js/runtime/ssr/server';
      
      const handleRequest = async (request, ServerRoot, options) => {
        const body = await renderString(request, <ServerRoot />, options);
      
        return new Response(body, {
          headers: {
            'content-type': 'text/html; charset=utf-8',
          },
        });
      };
      
      export default createRequestHandler(handleRequest);

      #Add Custom Entry Points

      Users need to customize the Server-Side Rendering entry points, they can customize the server entry in src/entry.server.ts or src/{entryName}/entry.server.ts.

      src/entry.server.tsx
      import {
        renderString,
        createRequestHandler,
      } from '@modern-js/runtime/ssr/server';
      import type { HandleRequest } from '@modern-js/runtime/ssr/server';
      
      const handleRequest: HandleRequest = async (request, ServerRoot, options) => {
        // do something before rendering
        const body = await renderString(request, <ServerRoot />, options);
      
        const newBody = body + '<div>Byte-Dance</div>';
      
        return new Response(newBody, {
          headers: {
            'content-type': 'text/html; charset=UTF-8',
            'x-custom-header': 'abc',
          },
        });
      };
      
      export default createRequestHandler(handleRequest);