Skip to main content

升级到 Strapi 5:介绍与常见问题

🌐 Upgrading to Strapi 5: Introduction and FAQ

Strapi 的最新主要版本是 Strapi 5。Strapi v4 仍将支持到 2026 年 4 月。

🌐 The latest major version of Strapi is Strapi 5. Strapi v4 is still supported until April 2026.

每当你准备好升级到 Strapi 5 时,本页面将为你提供帮助。它列出了从 Strapi 4 升级到 Strapi 5 的所有可用资源,并解答你可能有的一般问题。

🌐 Whenever you feel ready to upgrade to Strapi 5, the present page will help you. It lists all available resources for upgrading from Strapi 4 to Strapi 5 and answers general questions you might have.

可用资源

🌐 Available resources

以下所有可用资源将帮助你将应用和插件升级到 Strapi 5,从最常见到最具体的用例:

🌐 All of the following available resources will help you upgrade your application and plugins to Strapi 5, from the most common to the most specific use cases:

常见问题

🌐 Frequently asked questions

以下问题及其答案应可帮助你涵盖最常见的用例:

🌐 The following questions and their answers should help you cover the most common use cases:

我该如何处理升级以及最新依赖的安装?
我该如何处理代码中的重大更改并将我的代码适配到 Strapi 5?

Strapi 提供了一个升级工具以简化此过程。该升级工具是一个命令行工具,带有一些处理依赖升级和执行 codemods 的命令。

按照 逐步指南 学习如何在升级到 Strapi 5 的情况下使用此工具。

Strapi 5 文档还提供了一个完整的重大更改数据库专用资源来覆盖特定的使用案例。


我如何处理数据迁移,以确保在 Strapi 5 中应用仍然能够正常工作?

Strapi 5 集成了一系列数据迁移脚本,这些脚本会在应用首次启动 Strapi 5 时运行。

但是,请 务必备份你的数据库 (如果使用 SQL 数据库,默认位置为 .tmp/data.db)在执行任何升级之前,如 逐步指南中所述。


作为 Strapi Cloud 的客户,我如何处理整个 Strapi 5 应用的升级和部署?
  1. 创建备份 并在本地更新你的代码,按照 逐步指南操作。
  2. Cloud CLI 运行 yarn deploynpm run deploy 命令。

Strapi Cloud 将在 Strapi 5 中部署更新的代码,并会自动运行数据迁移脚本。


在迁移过程中,我如何保持遗留的 attributes 封装器?
  • 对于 REST 客户端,在重构代码时添加 Strapi-Response-Format: v4 头。新的响应格式破坏性更改 (new response format breaking change) 显示了在 curlfetch 和 Axios 请求中添加该头的位置。
  • 对于 GraphQL 客户端,启用 v4CompatibilityMode 并按照 GraphQL API 迁移文档 的步骤逐步移除 attributes
  • 当启用该头时,REST 响应仍会同时暴露 id(旧版)和 documentId。GraphQL 从不暴露数字 id,因此即使在关闭兼容模式之前,也请更新你的查询以使用 documentId

一旦每个消费者读取了扁平化格式,移除头部,这样 Strapi 就会默认发出 Strapi 5 响应格式。