Skip to content

WIP feat: server config doc #7092

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/document/main-doc/docs/en/configure/app/usage.mdx
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ Modern.js does not support configuring the same configuration item in both `pack

**Runtime configuration** can be configured in the `src/modern.runtime.(ts|js|mjs)` file.

**Server Runtime configuration** can be configured in the `modern.server-runtime.config.(ts|js|mjs)` file in the root path.
**Server Runtime configuration** can be configured in the `server/modern.server.(ts|js|mjs)` file.

## Compile Configuration

Original file line number Diff line number Diff line change
@@ -23,5 +23,6 @@
"label": "server-monitor",
"collapsed": true
},
"custom-server",
"web-server"
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
---
sidebar_position: 16
---

# Custom Server

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.

## Custom Server Capabilities

Create the `server/modern.server.ts` file in the project directory, where you can configure **middleware**, **rendering middleware**, and **server plugins** to extend the Server.

The execution order of middleware is: Middleware => PluginMiddleware => RenderMiddleware => PluginRenderMiddleware.

### Basic Configuration

```ts title="server/modern.server.ts"
import { defineServerConfig } from '@modern-js/server-runtime';

export default defineServerConfig({
middlewares: [],
renderMiddlewares: [],
plugins: [],
});
```


### Type Definition

`defineServerConfig` type definition is as follows:

```ts
import type { MiddlewareHandler } from 'hono';

type MiddlewareOrder = 'pre' | 'post' | 'default';
type MiddlewareObj = {
name: string;
path?: string;
method?: 'options' | 'get' | 'post' | 'put' | 'delete' | 'patch' | 'all';
handler: MiddlewareHandler | MiddlewareHandler[];
before?: Array<MiddlewareObj['name']>;
order?: MiddlewareOrder;
};
type ServerConfig = {
middlewares?: MiddlewareObj[];
renderMiddlewares?: MiddlewareObj[];
plugins?: (ServerPlugin | ServerPluginLegacy)[];
}
```


### Middleware

Middleware supports executing custom logic before and after the **request handling** and **page routing** processes in Modern.js services.

:::note
In the BFF scenario, BFF routing will only go through Middleware when the runtime framework is Hono.
:::

#### Using Posture

```ts title="server/modern.server.ts"
import { defineServerConfig, type MiddlewareHandler } from '@modern-js/server-runtime';
import { getMonitors } from '@modern-js/runtime';

export const handler: MiddlewareHandler = async (c, next) => {
const monitors = getMonitors();
const start = Date.now();

await next();

const end = Date.now();
// Report Duration
monitors.timing('request_timing', end - start);
};

export default defineServerConfig({
middlewares: [
{
name: 'request-timing',
handler,
},
],
});
```

:::warning
You must execute the `next` function to proceed with the subsequent Middleware.
:::


### RenderMiddleware

Modern.js supports adding rendering middleware to the Server, allowing custom logic to be executed before and after handling page routes.

#### Using Posture

```ts title="server/modern.server.ts"
import { defineServerConfig, type MiddlewareHandler } from '@modern-js/server-runtime';

// Inject render performance metrics
const renderTiming: MiddlewareHandler = async (c, next) => {
const start = Date.now();

await next();

const end = Date.now();
c.res.headers.set('server-timing', `render; dur=${end - start}`);
};

// Modify the Response Body
const modifyResBody: MiddlewareHandler = async (c, next) => {
await next();

const { res } = c;
const text = await res.text();
const newText = text.replace('<body>', '<body> <h3>bytedance</h3>');

c.res = c.body(newText, {
status: res.status,
headers: res.headers,
});
};

export default defineServerConfig({
renderMiddlewares: [
{
name: 'render-timing',
handler: renderTiming,
},
{
name: 'modify-res-body',
handler: modifyResBody,
},
],
});
```


### Plugin

Modern.js supports adding the aforementioned middleware and rendering middleware for the Server in custom plugins.

#### Using Posture


```ts title="server/plugins/server.ts"
import type { ServerPluginLegacy } from '@modern-js/server-runtime';

export default (): ServerPluginLegacy => ({
name: 'serverPlugin',
setup(api) {
return {
prepare(serverConfig) {
const { middlewares, renderMiddlewares } = api.useAppContext();

// Inject server-side data for page dataLoader consumption
middlewares?.push({
name: 'server-plugin-middleware',
handler: async (c, next) => {
c.set('message', 'hi modern.js');
await next();
// ...
},
});

// redirect
renderMiddlewares?.push({
name: 'server-plugin-render-middleware',
handler: async (c, next) => {
const user = getUser(c.req);
if (!user) {
return c.redirect('/login');
}

await next();
},
});
return serverConfig;
},
};
},
});
```


```ts title="server/modern.server.ts"
import { defineServerConfig } from '@modern-js/server-runtime';
import serverPlugin from './plugins/serverPlugin';

export default defineServerConfig({
plugins: [serverPlugin()],
});
```


```ts title="src/routes/page.data.ts"
import { useHonoContext } from '@modern-js/server-runtime';
import { defer } from '@modern-js/runtime/router';

export default () => {
const ctx = useHonoContext();
// Consuming Data Injected by the Server-Side
const message = ctx.get('message');

// ...
};

```
Loading