logo
  • Guide
  • Config
  • Plugin
  • API
  • Examples
  • Community
  • Modern.js 2.x Docs
  • English
    • 简体中文
    • English
    • Configuration
      dev
      assetPrefix
      beforeStartUrl
      client
      hmr
      host
      https
      lazyCompilation
      liveReload
      port
      progressBar
      setupMiddlewares
      startUrl
      watchFiles
      writeToDisk
      bff
      prefix
      html
      appIcon
      crossorigin
      favicon
      inject
      meta
      mountId
      outputStructure
      scriptLoading
      tags
      templateParameters
      template
      title
      tools
      autoprefixer
      babel
      bundlerChain
      cssExtract
      cssLoader
      devServer
      htmlPlugin
      less
      lightningcssLoader
      minifyCss
      postcss
      rspack
      sass
      styleLoader
      swc
      tsChecker
      source
      aliasStrategy
      alias
      configDir
      decorators
      define
      disableDefaultEntries
      enableAsyncEntry
      entriesDir
      entries
      exclude
      globalVars
      include
      mainEntryName
      preEntry
      transformImport
      resolve
      aliasStrategy
      alias
      conditionNames
      dedupe
      extensions
      server
      baseUrl
      port
      publicRoutes
      routes
      ssrByEntries
      ssr
      output
      assetPrefix
      assetsRetry
      charset
      cleanDistPath
      convertToRem
      copy
      cssModules
      dataUriLimit
      disableCssModuleExtension
      disableInlineRuntimeChunk
      disableSvgr
      disableTsChecker
      distPath
      enableAssetManifest
      enableCssModuleTSDeclaration
      disableInlineRouteManifests
      externals
      filenameHash
      filename
      injectStyles
      inlineScripts
      inlineStyles
      legalComments
      minify
      overrideBrowserslist
      polyfill
      sourceMap
      splitRouteChunks
      ssg
      ssgByEntries
      svgDefaultExport
      tempDir
      plugins
      security
      checkSyntax
      nonce
      sri
      runtime
      Introduce
      plugins
      router
      performance
      buildCache
      bundleAnalyze
      chunkSplit
      dnsPrefetch
      preconnect
      prefetch
      preload
      printFileSize
      profile
      removeConsole
      removeMomentLocale
      experiments
      sourceBuild
      builderPlugins
      📝 Edit this page
      Previous pagessrByEntriesNext pageassetPrefix

      #server.ssr

      • Type: boolean
      • Default: false

      Enalbe SSR configuration.

      #Boolean Type

      When the value type is boolean, it indicates whether to enable SSR deployment mode. The default is false to disable it.

      modern.config.ts
      export default defineConfig({
        server: {
          ssr: true,
        },
      });

      #Object Type

      When the value type is Object, the following properties can be configured:

      NameTypeDefaultDescription
      modestringstringwhich defaults to using renderToString for rendering. Configure stream to enable streaming rendering
      forceCSRbooleanfalsewhich is off by default for forcing CSR rendering. Configure true to force CSR by adding ?csr=true or adding x-modern-ssr-fallback header when accessing the page
      unsafeHeadersstring[][]For safety reasons, Modern.js does not add excessive content to SSR_DATA. Developers can use this configuration to specify the headers that need to be injected
      loaderFailureModeclientRender | errorBoundaryerrorBoundaryThe default configuration is 'errorBoundary', when an error occurs in data loader, it will default to rendering the Error component of the route. When configured as 'clientRender', if a loader throws an error, it switch to client-side rendering,you can use it with Client Loader
      modern.config.ts
      export default defineConfig({
        server: {
          ssr: {
            forceCSR: true,
            mode: 'stream',
            unsafeHeaders: ['User-Agent']
          },
        },
      });

      #Active Fallback

      In a production environment, there are scenarios where it is necessary to actively fallback an SSR project to CSR. Examples include

      1. When the SSR fails, a fallback to the CSR is required to ensure product availability.

      2. When the SSR is working normally, but there are rendering failures during csr, debugging is required.

      3. When the SSR server is under heavy load, it may be necessary to fallback some traffic directly to the CSR to avoid service downtime.

      By configuring server.ssr.forceCSR to true in the project, you can control this behavior through query strings or request headers.

      For example, in a custom Web Server middleware, you can actively fallback when traffic exceeds a certain threshold:

      server/modern.server.ts
      import {
        defineServerConfig,
        type MiddlewareHandler,
      } from '@modern-js/server-runtime';
      
      export const handler: MiddlewareHandler = async (c, next) => {
        if (condition) {
          c.set('forceCSR', '1');
        }
        await next();
      };
      
      export default defineServerConfig({
        middlewares: [
          {
            name: 'request-middleware',
            handler,
          },
        ],
      });