Modern.js encapsulates most server-side capabilities required by projects, typically eliminating the need for server-side development. However, in certain scenarios such as user authentication, request preprocessing, or adding page skeletons, custom server-side logic may still be necessary.
Modern.js provides two types of APIs to extend the Web Server: Middleware and Lifecycle Hooks.
Middleware and Hooks only take effect when users request page routes, and BFF routes won't pass through these APIs.
Developers can execute the pnpm run new
command in the project root directory to enable the "Custom Web Server" feature:
After executing the command, register the @modern-js/plugin-server
plugin in modern.config.ts
:
Once enabled, a server/index.ts
file will be automatically created in the project directory where custom logic can be implemented.
Modern.js supports adding rendering middleware to the Web Server, allowing custom logic execution before and after processing page routes.
For detailed API and more usage, see UnstableMiddleware.
We recommend using UnstableMiddleware instead of Hooks.
Modern.js provides Hooks to control specific logic in the Web Server. All page requests will pass through Hooks.
Currently, two types of Hooks are available: AfterMatch
and AfterRender
. Developers can implement them in server/index.ts
as follows:
Best practices when using Hooks:
For detailed API and more usage, see Hook.