RenderContext
RenderContext is a core class in the Esmx framework, responsible for managing the complete lifecycle of Server-Side Rendering (SSR). It provides a comprehensive API to handle rendering contexts, resource management, state synchronization, and other key tasks:
- Rendering Control: Manages the Server-Side Rendering flow, supporting multi-entry rendering, conditional rendering, and other scenarios
- Resource Management: Intelligently collects and injects JS, CSS, and other static resources, optimizing loading performance
- State Synchronization: Handles server-side state serialization, ensuring proper client-side hydration
- Routing Control: Supports advanced features like server-side redirects and status code setting
Type Definitions
ServerRenderHandle
Type definition for Server-Side Rendering handler function.
The Server-Side Rendering handler function is an async or sync function that receives a RenderContext instance as a parameter, used to handle Server-Side Rendering logic.
RenderFiles
Type definition for resource file list collected during rendering process.
- js: JavaScript file list
- css: Stylesheet file list
- modulepreload: ESM module list that needs preloading
- resources: Other resource file list (images, fonts, etc.)
ImportmapMode
Defines the generation mode for importmap.
inline: Embeds importmap content directly into HTML, suitable for:- Reducing HTTP request count
- Small importmap content
- High requirements for first-screen loading performance
js: Generates importmap content as an independent JS file, suitable for:- Large importmap content
- Utilizing browser caching mechanisms
- Multiple pages sharing the same importmap
Rendering context class, responsible for resource management and HTML generation in the Server-Side Rendering (SSR) process.
Instance Options
Defines configuration options for rendering context.
base
- Type:
string - Default:
''
Base path for static resources.
- All static resources (JS, CSS, images, etc.) will be loaded based on this path
- Supports runtime dynamic configuration without rebuilding
- Commonly used in multi-language sites, micro-frontend applications, and other scenarios
entryName
- Type:
string - Default:
'default'
Server-Side Rendering entry function name. Used to specify the entry function for Server-Side Rendering, utilized when a module exports multiple rendering functions.
params
- Type:
Record<string, any> - Default:
{}
Rendering parameters. Can pass parameters of any type to the rendering function, commonly used to pass request information (URL, query parameters, etc.).
importmapMode
- Type:
'inline' | 'js' - Default:
'inline'
Import map generation mode:
inline: Embeds importmap content directly into HTMLjs: Generates importmap content as independent JS file
Instance Properties
esmx
- Type:
Esmx - Read-only:
true
Reference to the Esmx instance. Used to access framework core functionality and configuration information.
redirect
- Type:
string | null - Default:
null
Redirect address. When set, the server can perform HTTP redirects based on this value, commonly used in login verification, permission control, and other scenarios.
status
- Type:
number | null - Default:
null
HTTP response status code. Can set any valid HTTP status code, commonly used in error handling, redirects, and other scenarios.
html
- Type:
string - Default:
''
HTML content. Used to set and get the final generated HTML content, automatically handling base path placeholders when setting.
base
- Type:
string - Read-only:
true - Default:
''
Base path for static resources. All static resources (JS, CSS, images, etc.) will be loaded based on this path, supporting runtime dynamic configuration.
entryName
- Type:
string - Read-only:
true - Default:
'default'
Server-Side Rendering entry function name. Used to select which rendering function to use from entry.server.ts.
params
- Type:
Record<string, any> - Read-only:
true - Default:
{}
Rendering parameters. Can pass and access parameters during the Server-Side Rendering process, commonly used to pass request information, page configuration, etc.
importMetaSet
- Type:
Set<ImportMeta>
Module dependency collection set. Automatically tracks and records module dependencies during component rendering, only collecting resources actually used in the current page rendering.
files
- Type:
RenderFiles
Resource file list:
- js: JavaScript file list
- css: Stylesheet file list
- modulepreload: ESM module list that needs preloading
- resources: Other resource file list (images, fonts, etc.)
importmapMode
- Type:
'inline' | 'js' - Default:
'inline'
Import map generation mode:
inline: Embeds importmap content directly into HTMLjs: Generates importmap content as an independent JS file
Instance Methods
serialize()
- Parameters:
input: any- Data to serializeoptions?: serialize.SerializeJSOptions- Serialization options
- Returns:
string
Serializes a JavaScript object to string. Used to serialize state data during Server-Side Rendering, ensuring data can be safely embedded into HTML.
state()
- Parameters:
varName: string- Variable namedata: Record<string, any>- State data
- Returns:
string
Serializes state data and injects it into HTML. Uses a safe serialization method to handle data, supporting complex data structures.
commit()
- Returns:
Promise<void>
Commits dependency collection and updates the resource list. Collects all used modules from importMetaSet, resolving specific resources for each module based on manifest files.
preload()
- Returns:
string
Generates resource preload tags. Used to preload CSS and JavaScript resources, supports priority configuration, and automatically handles base paths.
css()
- Returns:
string
Generates CSS stylesheet tags. Injects collected CSS files, ensuring stylesheets load in the correct order.
importmap()
- Returns:
string
Generates import map tags. Generates inline or external import maps based on the importmapMode configuration.
moduleEntry()
- Returns:
string
Generates client entry module tags. Injects the client entry module, must be executed after importmap.
modulePreload()
- Returns:
string
Generates module preload tags. Preloads collected ESM modules, optimizing first-screen loading performance.