逐步升级到 Strapi 5 的指南
🌐 Step-by-step guide to upgrade to Strapi 5
Strapi 的最新主要版本是 Strapi 5。
🌐 The latest major version of Strapi is Strapi 5.
本页面旨在用作将 Strapi v4 应用升级到 Strapi 5 的分步说明。
🌐 The present page is meant to be used as step-by-step instructions for upgrading your Strapi v4 application to Strapi 5.
你的 Strapi v4 应用已经在最新的 v4 小版本和修补版本上运行。如果没有,请使用 minor 命令运行 升级工具 以升级到最新版本:npx @strapi/upgrade minor。
🌐 Your Strapi v4 application is already running on the latest v4 minor and patch version. If it's not, run the upgrade tool with the minor command to reach it: npx @strapi/upgrade minor.
步骤 1:准备升级
🌐 Step 1: Get ready to upgrade
在进入升级过程之前,请采取以下预防措施:
🌐 Before getting into the upgrade process itself, take the following precautions:
- 备份你的数据库:
- 如果你使用 SQLite 且采用默认配置(Strapi 提供的默认数据库),你的数据库文件名为
data.db,并位于 Strapi 应用根目录下的.tmp/文件夹中。 - 如果你使用的是其他类型的数据库,请参阅其官方文档(见 PostgreSQL docs 和 MySQL docs)。
- 如果你的项目托管在 Strapi Cloud 上,你可以手动创建备份。
- 如果你使用 SQLite 且采用默认配置(Strapi 提供的默认数据库),你的数据库文件名为
- 备份你的代码:
- 如果你的代码已使用 git 进行版本控制,请创建一个新的专用分支来运行迁移。
- 如果你的代码没有使用 git 进行版本控制,请创建你正在使用的 Strapi v4 代码的备份,并将其存放在安全的地方。
- 确保你使用的插件与 Strapi 5 兼容。
为此,请列出你正在使用的插件,然后通过阅读它们在 Marketplace 网站上的专用文档来检查每个插件的兼容性。
步骤 2:运行自动迁移
🌐 Step 2: Run automated migrations
Strapi 提供了一个工具来自动化升级到 Strapi 5 的某些部分:升级工具。
🌐 Strapi provides a tool to automate some parts of the upgrade to Strapi 5: the upgrade tool.
- 运行升级工具。
npx @strapi/upgrade major
该命令将执行 Strapi 5 依赖的更新和安装,并运行 codemods 来处理 Strapi 5 带来的一些重大更改。
codemods 将处理以下更改:
| Codemod name and GitHub code link | Description |
|---|---|
| comment-out-lifecycle-files | Comment out lifecycles files in favor of Document Service Middlewares |
| dependency-remove-strapi-plugin-i18n | Remove the i18n plugin dependency as i18n is now integrated into the core of Strapi |
| dependency-upgrade-react-and-react-dom | Upgrade the react and react-dom dependencies |
| dependency-upgrade-react-router-dom | Upgrade the react-router-dom dependency |
| dependency-upgrade-styled-components | Upgrade the styled-components dependency |
| deprecate-helper-plugin | Partly handle migrations from @strapi/helper-plugin |
| entity-service-document-service | Partly handle the migration from the Entity Service API to the new Document Service API |
| s3-keys-wrapped-in-credentials | Wrap the accessKeyId and secretAccessKey properties inside a credentials object for users using the aws-s3 provider |
| sqlite3-to-better-sqlite3 | Update the sqlite dependency to better-sqlite3 |
| strapi-public-interface | Transform @strapi/strapi imports to use the new public interface |
| use-uid-for-config-namespace | Replace string dot format for config get/set/has with uid format for 'plugin' and 'api' namespace where possible |
| utils-public-interface | Update utils to use the new public interface |
如果你开发 Strapi 插件,其他代码修改工具会处理 helper-plugin 弃用的一些方面。有关更多信息,请参见相关的重大更改。
🌐 If you develop Strapi plugins, other codemods handle some aspects of the helper-plugin deprecation. See the related breaking change for more information.
- 查看升级工具所做的更改,以检查是否需要手动完成某些代码更新:
查找由 codemods 自动添加到你的代码中的 __TODO__。其中一些可能是在从 Entity Service API 迁移到 Strapi 5 引入的新 Document Service API 时添加的。
步骤 3:检查并处理手动升级
🌐 Step 3: Check and handle manual upgrades
以下主要更改可能会影响你的 Strapi 应用并要求你执行一些手动操作。
🌐 The following main changes might affect your Strapi application and require you to do some manual actions.
对于每个日志项,请阅读指示的重大更改条目并检查升级工具运行后是否仍需要某些手动操作:
🌐 For each of them, read the indicated breaking change entry and check if some manual actions are still required after the upgrade tool has run:
- 数据库迁移:
- 不支持 MySQL v5 👉 查看 重大变更
- 仅支持 better-sqlite3 👉 参见 breaking change
- 只支持 mysql2 👉 参见 重大更改
- 生命周期钩子的触发方式有所不同 👉 请参见 重大更改
- 配置:
- 管理面板自定义:
- 助手 插件已被移除 👉 参见 迁移参考
👉 最后,查看剩余的 breaking changes 数据库,以了解你可能关心的任何边缘情况。
步骤 4:迁移API消费端
🌐 Step 4: Migrate the API consuming side
Strapi 5 已更新 REST 和 GraphQL API。
🌐 Strapi 5 has updated both the REST and GraphQL APIs.
请按照以下步骤操作,并利用向后兼容性标头和引导式迁移资源,逐步将你的代码更新为 Strapi 5。
🌐 Follow the steps below and leverage retro-compatibility headers and guided migration resources to gradually update your code for Strapi 5.
迁移 REST API 调用
🌐 Migrate REST API calls
- 通过在 HTTP 客户端、SDK 和中间件的 REST 调用中添加
Strapi-Response-Format: v4,在所有仍然期望使用attributes的地方启用兼容性头(具体示例请参见重大更改条目)。 - 在启用头部的同时,审计现有的有效负载。捕获有代表性的响应(包括已填充的关系、组件和媒体),以便在过渡期间验证旧版使用者仍能正常工作。
- 通过以下方式更新和测试每个客户端:
- 移除
data.attributes访问权限,
- 移除