先说结论
阿里云 ESA 函数与 Pages 采用尾斜杠路由:
- 当
about.html或about/index.html存在时,访问/about会 301 到/about/; - 站内链接直接写
/about/,可以少一次跳转; - 目录入口用
index.html; - 静态站点用
404Page策略,单页应用用singlePageApplication。
推荐文件结构
/
├─ index.html → /
├─ about.html → /about/(自动 301)
├─ about/index.html → /about/(目录式,二选一)
├─ products/index.html → /products/
├─ 404.html
└─ assets/esa.jsonc 配置
放在项目根目录;存在时优先级高于控制台配置。
{
"name": "my-site",
"assets": {
"directory": "./public",
"notFoundStrategy": "404Page"
}
}assets.directory:静态资源目录,里面必须有index.html;notFoundStrategy: "404Page":多页静态站,未匹配路径返回404.html和 404;notFoundStrategy: "singlePageApplication":单页应用,未匹配的导航回到/index.html并返回 200。
函数与静态资源的关系
- 命中静态资源时直接返回,不会执行函数;
- 未命中时按
notFoundStrategy回退; - 配置了函数和
notFoundStrategy时,浏览器地址栏的导航请求不会触发函数脚本。
常见坑
- 站内链接写
/about而不是/about/:每次多一次 301。 - 静态多页站误用
singlePageApplication:所有未命中地址都返回首页,SEO 会重复。 assets.directory指向源码目录而不是构建产物目录:部署后没有index.html。- 只有
about.html却链接到/about:能跳转,但建议直接写/about/。 - 文件名大小写不一致。
检查清单
- [ ] 静态目录里有
index.html - [ ] 每个页面链接都带结尾
/ - [ ] 多页站点使用
404Page并提供404.html - [ ] 只有 SPA 使用
singlePageApplication - [ ]
esa.jsonc的assets.directory指向构建产物 - [ ] canonical / sitemap 与尾斜杠地址一致
可直接复制的提示词
你是一名前端工程师,要为【品牌名 / 业务】生成一个可直接部署到阿里云 ESA 函数与 Pages 的静态多页网站。
硬性规范:
1. 输出 index.html;每个页面使用目录入口,例如 about/index.html、products/index.html、blog/post/index.html;资源放在 assets/。
2. 所有站内链接写带结尾斜杠的根相对路径:/、/about/、/products/、/blog/post/;禁止写 about.html,禁止写不带斜杠的 /about,禁止使用 ../。
3. canonical、og:url、sitemap.xml、JSON-LD 使用同样的带斜杠地址。
4. 同时输出 esa.jsonc,包含 assets.directory(指向静态产物目录)和 notFoundStrategy: "404Page";只有单页应用才使用 singlePageApplication。
5. 文件名只用小写字母、数字和连字符。
6. 不依赖服务端路由、Node 或数据库;只用 HTML、CSS 和原生 JavaScript。
7. 提供 404.html。
8. 输出完整文件清单和每个文件的完整内容,不要省略。
9. 页面正文使用【中文 / 英文】。