Unstable Middleware

用于拓展 Modern.js 内置的 Web Server。 未来 UnstableMiddleware 将替代 Middleware

使用

server/index.ts
import { UnstableMiddleware } from '@modern-js/runtime/server';

export const unstableMiddleware: UnstableMiddleware[] = [];

类型

UnstableMiddleware

type UnstableMiddleware<
  V extends Record<string, unknown> = Record<string, unknown>,
> = (
  c: UnstableMiddlewareContext<V>,
  next: UnstableNext,
) => Promise<void | Response>;

UnstableMiddlewareContext

type Body = ReadableStream | ArrayBuffer | string | null;

type UnstableMiddlewareContext<
  V extends Record<string, unknown> = Record<string, unknown>,
> = {
  request: Request;
  response: Response;
  get: Get<V>;
  set: Set<V>;
  header: (name: string, value: string, options?: { append?: boolean }) => void;
  status: (code: number) => void;
  redirect: (location: string, status?: number) => Response;
  body: (data: Body, init?: ResponseInit) => Response;
  html: (
    data: string | Promise<string>,
    init?: ResponseInit,
  ) => Response | Promise<Response>;
};

UnstableNext

type UnstableNext = () => Promise<void>;

用例

web server 耗时打点

import { UnstableMiddleware } from '@modern-js/runtime/server';

const time: UnstableMiddleware = async (c, next) => {
  const start = Date.now();

  await next();

  const end = Date.now();

  console.log(`${end - start}`);
};

export const unstableMiddleware: UnstableMiddleware[] = [time];

注入服务端数据,供页面 dataLoader 消费

shared/index.ts
export type Vars = {
  message: string;
};
server/index.ts
import {
  UnstableMiddleware,
  UnstableMiddlewareContext,
} from '@modern-js/runtime/server';
import type { Vars } from '../shared/index';

const setPayload: UnstableMiddleware<Vars> = async (
  c: UnstableMiddlewareContext<Vars>,
  next,
) => {
  c.set('message', 'facker');

  await next();
};

export const unstableMiddleware: UnstableMiddleware<Vars>[] = [setPayload];
src/routes/page.data.ts
import type { Payload } from '../../shared/index';
import { LoaderFunctionArgs } from '@modern-js/runtime/router';

export const loader = async ({ context }: LoaderFunctionArgs<Vars>) => {
  const message = context?.get('message');

  // ...
};

重定向

import { UnstableMiddleware } from '@modern-js/runtime/server';

const auth: UnstableMiddleware = async (c, next) => {
  const user = getUser(c.request);

  if (!user) {
    return c.redirect('/login');
  }

  await next();
};

export const unstableMiddleware: UnstableMiddleware[] = [auth];

修改响应体

import { UnstableMiddleware } from '@modern-js/runtime/server';

const modifier: UnstableMiddleware = async (c, next) => {
  await next();

  const { response } = c;

  const text = await response.text();

  const newText = text.replace('<html>', `<html lang="${language}">`);

  const newheaders = response.headers;
  newheaders.set('x-custom-value', 'modern');

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

export const unstableMiddleware: UnstableMiddleware[] = [modifier];