Skip to content

[Bug]: Can't call useContext out of server scope #8782

Description

@xiongz2z

Version

envinfo@7.21.0
Ok to proceed? (y) y


  System:
    OS: macOS 26.5.2
    CPU: (16) arm64 Apple M4 Max
    Memory: 48.68 GB / 128.00 GB
    Shell: 5.9 - /bin/zsh
  Browsers:
    Chrome: 150.0.7871.182
    Safari: 26.5.2
  npmPackages:
    @modern-js/app-tools: 3.7.0 => 3.7.0 
    @modern-js/plugin-bff: ^3.7.0 => 3.7.0 
    @modern-js/runtime: 3.7.0 => 3.7.0 
    @modern-js/server-runtime: ^3.7.0 => 3.7.0 
    @modern-js/tsconfig: 3.7.0 => 3.7.0

Details

问题描述

按照官网所示步骤,启用BFF,执行dev命令,请求BFF接口,报错:message: "Can't call useContext out of server scope"

期望

程序能够正常运行,返回值正确

BFF函数

// FilePath: myapp/api/lambda/hello.ts
import { useHonoContext } from "@modern-js/server-runtime";

export const get = async () => {
  const c = useHonoContext();
  console.info(`access url: ${c.req.url}`);
  return "hello world";
};

依赖

  "name": "myapp",
  "version": "0.1.0",
  "scripts": {
    "dev": "modern dev",
    "build": "modern build",
    "serve": "modern serve",
    "lint": "biome check"
  },
  "engines": {
    "node": ">=20"
  },
  "dependencies": {
    "@modern-js/plugin-bff": "^3.7.0",
    "@modern-js/runtime": "3.7.0",
    "@modern-js/server-runtime": "^3.7.0",
    "react": "^19.2.3",
    "react-dom": "^19.2.0"
  },
  "devDependencies": {
    "@biomejs/biome": "1.9.4",
    "@modern-js/app-tools": "3.7.0",
    "@modern-js/tsconfig": "3.7.0",
    "@types/node": "^20",
    "@types/react": "^19.1.8",
    "@types/react-dom": "^19.1.6",
    "typescript": "~5.7.3"
  }
}

配置项

import { defineConfig, appTools } from "@modern-js/app-tools";
import { bffPlugin } from "@modern-js/plugin-bff";

export default defineConfig({
  plugins: [appTools(), bffPlugin()],
});

Reproduce link

Reproduce Steps

  1. bff中的hello接口使用useHonoContext();
  2. 执行pnpm run dev;
  3. 请求bff中的hello接口;
  4. 报错:Can't call useContext out of server scope

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions