返回提示词目录第 1304 页

升级前端路由主版本并验证

梳理现有网址与路由用法,逐级迁移组件和导航接口,并检查页面匹配及前进后退行为。

完整提示词

<context>
React Router majors change how routes are matched, not only what things are called. From v5 to v6: `Switch` became `Routes`, `component` and `render` props became an `element` prop, route matching ranks routes instead of taking the first match (so `exact` disappears and route order stops mattering), nested routes render through `<Outlet>` and use relative paths, `useHistory` became `useNavigate`, `Redirect` became `Navigate`, `withRouter` and the `history` object are gone, and regex paths and optional params changed. Code that reaches into `history.listen` or blocks navigation needs a real replacement, and `Prompt` has no one-to-one equivalent. From v6 to v7: the package name collapses to `react-router`, the future flags (`v7_startTransition`, `v7_relativeSplatPath` and the others) describe exactly what changes, so turning them on one at a time in v6 is the safe path; v7 also offers framework mode (the Remix successor), which is a separate, optional move. The usual failure is a route that silently stops matching, which no compiler catches, so the upgrade needs a list of every URL and a check of each.
</context>

<task>
Upgrade React Router from {{current_version}} to {{target_version}}.
{{#project}}
About the project:
{{project}}
{{/project}}

1. Find every route definition, every use of router hooks, `history`, `withRouter`, redirects, navigation blocking, and links with relative paths. Record the package versions.
2. Build a route map: every URL pattern the app serves today and the component it renders, including nested and catch-all routes. This is the checklist for the end.
3. Run build, type check and tests and record the baseline.
4. Get the official migration guide for each major in between. Fetch it if you can; otherwise ask the user to paste it and stop.
5. If the current major supports future flags for the target, turn them on one at a time, fixing what each breaks, before changing the package version.
6. Upgrade one major at a time. Convert routes (`Switch` to `Routes`, `element` props, nested routes with `Outlet`, relative paths), hooks (`useNavigate`, `useParams`, `useLocation`), redirects and the `withRouter` consumers. Use official codemods if they exist and review their output.
7. Replace removed capabilities deliberately: navigation blocking, `history.listen`, regex paths. Say what you chose for each.
8. Leave framework mode or data routers out unless the user asked for them.
9. Run the checks, then walk the route map in the running app: each URL renders the same component, params are read correctly, redirects and 404s behave as before, and back and forward buttons work.
</task>

<constraints>
- Every URL in the route map must still match after the upgrade, or the change must be listed and agreed.
- Do not adopt loaders, actions or framework mode as part of a version upgrade unless asked.
- Do not keep a compatibility package in place permanently without saying so.
{{> guardrails/no-hardcoding-to-pass-tests}}
{{> guardrails/verify-before-done}}
{{> guardrails/scope-discipline}}
</constraints>

<output_format>
## Starting point
Versions, how routes are defined, baseline results.
## Route map
Table: URL pattern, component, status after upgrade (same, changed, removed).
## Changes made
Grouped by pattern (routes, hooks, redirects, blocking, history), with files.
## Verification
Checks before and after; route map walk results.
## Follow-ups
Optional moves such as data routers or framework mode, and anything not verified.
</output_format>

可替换内容

  • {{current_version}}
  • {{target_version}}
  • {{#project}}
  • {{project}}
  • {{/project}}
  • {{> guardrails/no-hardcoding-to-pass-tests}}
  • {{> guardrails/verify-before-done}}
  • {{> guardrails/scope-discipline}}

分类与标签

编程开发、前端路由、依赖升级、接口迁移、路由回归测试

前往 AI 图片创作