Skip to content

Conversation

@sylingd
Copy link

@sylingd sylingd commented Dec 27, 2025

Summary

支持部署至边缘计算环境(包括腾讯云 EdgeOne、阿里云 ESA、Cloudflare Workers)(顺便修复了#7758

Support deploy application to edge computing environments (including Tencent EdgeOne, Alibaba Cloud ESA, and Cloudflare Workers).

Related Links

Checklist

  • I have added changeset via pnpm run change.
  • I have updated the documentation.
  • I have added tests to cover my changes.

详细说明

支持了哪些平台

为什么没有在原有的 workerSSR 基础上做

有试图完善这个功能。但发现存在几个问题:

  • SSR功能强依赖async_hooks等nodejs专有库,并且相关API还进一步包装后对用户暴露(例如useHonoContext),没有办法移除。
  • 不同厂商 Runtime API 支持性不一样,例如stream功能,腾讯云EO的边缘函数和阿里云ESA边缘函数有一些不同。
  • bff等功能也有大量基于文件目录的逻辑,改造成本极高。
  • 该功能设计之初似乎没有预留对于不同平台的适配入口?

后重新考虑方案,决定基于node版本实现,原因如下:

  • 云函数环境广泛支持一些基础Node API,例如阿里云ESA、CF、腾讯云EO Node Functions 都直接支持async_hooks、node:stream等API,改造成本相对较低,对已有功能影响更小。
  • 方便支持更多原本基于node环境实现的功能。

第三方平台环境特点

运行环境

  • 腾讯云EO有标准Node环境,支持所有Node内置库,但不能在自己的代码中直接监听端口。
  • 阿里云ESA是worker环境,没有绝大部分Node基础库,但对process、async_hooks等少部分基础库做了兼容。
  • CF疑似高度封装的Node环境,对绝大部分Node API都做了兼容。

入口文件

  • 阿里云ESA和CF是单一入口文件形式,需要在入口文件中导出fetch函数。
  • 腾讯云EO自带一套路由系统(虽然实际研究发现底层做的很难评)。至少需要两个 entry 文件。

同时,三者都有一个共同特点:会对产物进行二次打包,生成单一文件供运行。因此,会破坏原有文件结构,即使CF对fs相关API进行了兼容,也没有办法直接使用。

没有支持腾讯云EO的Edge Functions的原因是其缺失太多Node API,比如async_hooks库。实际上我最早开始尝试支持的也是它,理论上我可以改出一个我自己可以用的版本,但工作量很大且会对原有功能造成 break change,不具备通用性,所以最终放弃。

理论上腾讯云SCF和阿里云FC更容易支持,因为它们都是未经二次打包的、完整的node运行时环境,但好像用的人太少没必要搞?

主要改动说明

依赖方式的核心改动

  • app-tools 不改变原有产物结构。将这三个平台的产物生成入口+deps.js的方式,供其特有的打包工具二次打包。其中,deps.js会扫描所有node产物,并生成一个包含文件结构+内容的结构。在后续工具中,可以通过该结构读取依赖信息。
  • server/core 新增了 edge-function 的适配器,包含原有 node 插件的实现。
  • plugin-bff server/bff-core 改动了 bff 插件,以适配上文提到的“deps.js”形式的依赖结构。

Hono API暴露

  • server/core 将 bindings 通过 loaderContext 暴露给用户。原因是其中包含了一些平台特有信息,如腾讯云EO的节点信息(其中包含了GeoIP地理信息、服务器节点等)、CF Workers的环境信息(用户需要通过它调用内置服务,如D1等)。

其他

  • prod-server app-tools 增加了相关入口
  • server/create-request 优先使用全局的fetch API
  • app-tools 增加了对各平台的入口及编译兼容配置
  • app-tools 复制 cjs 和 mjs 文件,避免被 rslib 编译一遍
  • toolkit/utils 在生成的 esm 产物中保留 import 语句,避免编译后出现 createRequire(此功能在阿里云ESA中不可用)
  • toolkit/plugin server/core 在 package.json 中增加 esm 指向,以支持在编译时对相关包进行 tree shaking(否则会因为 cjs 没法 tree shaking 引入过多包,可能出现部分包不兼容、体积过大等问题)

其他

为什么没有使用ndepe复制依赖,而是单独打包了一份modern-server

  1. 各平台对产物进行二次打包时,使用的esbuild会优先读取package.json中的esm相关字段,ndepe复制依赖的过程中可能出现缺失。
  2. 使用rspack预构建性能更好,可以更准确控制构建过程(例如tree shaking细节等),生成体积更小、性能更好的产物。

RSC 相关

暂未对 edge 环境实现 RSC 相关插件。主要原因:该功能似乎看起来还没做完。文档中也没有相关说明,无法测试。可以在后续该功能正式上线后再进行适配。

已经测试内容

在上述三个平台及node中对以下功能进行了简单测试:

  • ✅SSR
  • ✅CSR
  • ✅BFF
  • ✅Data Loader
  • ✅SSR+Stream

未测试:vercel、netlify(从改动上分析不会对原有功能造成影响,因没有账号故未做实际测试)

@changeset-bot
Copy link

changeset-bot bot commented Dec 27, 2025

🦋 Changeset detected

Latest commit: 78d9577

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 111 packages
Name Type
@modern-js/runtime Minor
@modern-js/create-request Minor
@modern-js/app-tools Minor
@modern-js/prod-server Minor
@modern-js/bff-core Minor
@modern-js/plugin-bff Minor
@modern-js/plugin Minor
@modern-js/utils Minor
@modern-js/server-core Minor
@modern-js/runtime-utils Minor
@modern-js/plugin-styled-components Minor
@modern-js/plugin-i18n Minor
@integration-test/alias-set Patch
app-document Patch
async-entry-test Patch
tmp Patch
bff-api-app Patch
bff-client-app Patch
bff-indep-client-app Patch
bff-hono Patch
integration-clean-dist-path Patch
integration-compatibility Patch
integration-custom-dist-path Patch
custom-file-system-entry Patch
integration-custom-template Patch
deploy Patch
deploy-server Patch
dev-server Patch
integration-disable-html Patch
app-custom-entries Patch
app-custom-routes-runtime Patch
app-custom Patch
app-entry Patch
app-route Patch
app-entry-server Patch
i18n-app Patch
i18n-app-ssr Patch
i18n-custom-i18n-wrapper Patch
i18n-mf-app-provider Patch
i18n-mf-component-provider Patch
i18n-mf-consumer Patch
i18n-routes Patch
i18n-routes-ssr Patch
@integration-test/image-component Patch
main-entry-name Patch
nonce Patch
pure-esm-project Patch
routes-match Patch
routes Patch
app-rsbuild-hooks Patch
rsc-csr-app Patch
rsc-csr-routes Patch
rsc-ssr-app Patch
rsc-ssr-routes Patch
runtime-custom-plugin Patch
runtime-custom-config-plugin Patch
select-mul-entry-test Patch
select-one-entry-test Patch
server-config Patch
server-json-script Patch
server-monitors Patch
server-prod Patch
server-routes Patch
@source-code-build/app Patch
ssg-fixtures-mega-list-routes Patch
ssg-fixtures-nested-routes Patch
ssg-fixtures-simple Patch
ssg-fixtures-web-server Patch
ssr-base-async-entry-test Patch
ssr-base-json-test Patch
ssr-base-test Patch
ssr-base-fallback-test Patch
init Patch
ssr-base-loadable Patch
ssr-partial-test Patch
ssr-script-loading Patch
ssr-streaming-inline-test Patch
ssr-streaming-test Patch
styled-components-stream Patch
styled-components-string Patch
integration-tailwindcss-v2 Patch
integration-tailwindcss-v3 Patch
integration-tailwindcss-v4 Patch
tmp-dir Patch
write-to-dist Patch
@modern-js/bundle-diff-benchmark Minor
@modern-js/plugin-ssg Minor
@modern-js/image Minor
@modern-js/plugin-polyfill Minor
entries-app-builder Patch
@modern-js/builder Minor
@modern-js/plugin-data-loader Minor
@modern-js/render Minor
@modern-js/server Minor
@modern-js/server-utils Minor
@modern-js/i18n-utils Minor
@modern-js/babel-compiler Minor
@scripts/release-node Patch
@modern-js/server-runtime Minor
@modern-js/create Minor
@modern-js/main-doc Minor
@modern-js/tsconfig Minor
@modern-js/babel-preset Minor
@modern-js/flight-server-transform-plugin Minor
@modern-js/babel-plugin-module-resolver Minor
@modern-js/bff-runtime Minor
@modern-js/sandpack-react Minor
@modern-js/types Minor
@modern-js/rslib Minor
@scripts/prebundle Patch
@scripts/rstest-config Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@netlify
Copy link

netlify bot commented Dec 27, 2025

Deploy Preview for modernjs-v3 ready!

Name Link
🔨 Latest commit 78d9577
🔍 Latest deploy log https://app.netlify.com/projects/modernjs-v3/deploys/6951005731c1bf00087aaa6f
😎 Deploy Preview https://deploy-preview-8097--modernjs-v3.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@sylingd sylingd changed the title feat: deploy on edge computing feat: deploy on edge environment Dec 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant