Open cheng4kn opened 3 days ago
这是因为你直接访问 .html 文件的时候,React Router 无法匹配你的路由,这和「约定式路由」没有直接关系。
我的需求是用 Modern.js 完成 约定式路由 + 模块联邦 + 微应用化 在构建后能够正常工作,以便实现独立开发和独立部署。但目前看起来,我没办法直接部署到静态的CDN,必须使用 modern serve
?或者有其他更好的方式吗?
如果没有解决办法,我考虑迁移到其他技术栈,或者不使用 Modern.js ,只是用 rspack。
感谢回复。🙏
版本信息
System: OS: macOS 15.0.1 CPU: (8) arm64 Apple M2 Memory: 116.98 MB / 16.00 GB Shell: 5.9 - /bin/zsh Browsers: Chrome: 129.0.6668.90 Safari: 18.0.1 npmPackages: @modern-js/app-tools: 2.60.1 => 2.60.1 @modern-js/runtime: 2.60.1 => 2.60.1 @modern-js/tsconfig: 2.60.1 => 2.60.1
问题详情
参考 https://modernjs.dev/guides/topic-detail/module-federation/application.html 这个文档,学习和了解了这些配置的参数和意义,使用 https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/module-federation/app-export 这个项目作为参考,完成 PoC 验证 modern.js 应用的 约定式路由 + 模块联邦 + 微应用化 在构建后能够正常工作,以便实现独立部署。
Q:为什么构建后的资源访问 html 静态文件,显示 404?约定式路由在 CSR 客户端渲染模式中无效?有文档说明吗?
当我访问 http://localhost:3050/html/main/ 时如图:
当我访问 http://localhost:3051/html/main/ 时如图:
复现链接
https://github.com/web-infra-dev/modern-js-examples/tree/main/examples/module-federation/app-export
复现步骤
modern-js-examples/examples/module-federation/app-export/host/modern.config.ts
:modern-js-examples/examples/module-federation/app-export/remote/modern.config.ts
:PS:
module-federation.config.ts
文件没有做任何修改yarn run dev
命令在开发环境下一切正常,应用 host 跟 remote 都能独立开发和独立运行,且模块联邦一切正常构建后的资源如下:
modern-js-examples/examples/module-federation/app-export/host/dist
:modern-js-examples/examples/module-federation/app-export/remote/dist
:安装 http-server
使用
yarn run build
命令分别先后构建 host 和 remote,然后分别部署静态资源