Skip to content

fix(h5): trigger onPageNotFound when navigating to unregistered routes#5967

Open
StrivingRabbit wants to merge 2 commits intouni-app-vue3-devfrom
copilot/handle-404-route-redirects
Open

fix(h5): trigger onPageNotFound when navigating to unregistered routes#5967
StrivingRabbit wants to merge 2 commits intouni-app-vue3-devfrom
copilot/handle-404-route-redirects

Conversation

@StrivingRabbit
Copy link
Copy Markdown
Collaborator

In H5, onPageNotFound was only fired during app launch when the initial URL had no matching route. Navigating to unregistered pages via navigateTo/redirectTo/reLaunch/switchTab silently failed — only the API fail callback fired, onPageNotFound was never invoked.

Changes

  • packages/uni-h5/src/service/api/route/utils.ts: After a navigation resolves without a NavigationFailure, check if router.currentRoute.value.matched.length === 0. If so, invoke the onPageNotFound app lifecycle hook (via invokeHook) and reject the promise.
    • openType is set to the navigation type (navigateTo, redirectTo, etc.) for accurate context in the handler
    • Consistent with the existing onPageNotFound invocation in setupApp() (same shape: notFound, openType, path, query, scene)

Usage

// App.vue
export default {
  onPageNotFound({ path, openType }) {
    // Now fires for both direct URL access AND programmatic navigation
    uni.redirectTo({ url: '/pages/404/index' })
  }
}
Original prompt

This section details on the original issue you should resolve

<issue_title>h5下如何将没有注册在pages.json的路由统一拦截到404</issue_title>
<issue_description>### 问题分类

uni-app

问题描述

现状:
根据文档uni.navigateTo只会跳转到已注册的页面,没有注册的页面会触发fail回调。
期望:
我希望能有什么办法可以统一处理这些错误,然后我在onPageNotFound的钩子里重定向到对应的404页面
需求原因:
因为这个项目之前是一个纯小程序的项目,后面需要适配h5,但是目前只适配了部分页面和功能,没有的页面和功能希望能引导用户去小程序操作

附加信息

No response</issue_description>

<agent_instructions>分析问题并给出建议</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@chouchouji 可以试试拦截器 [https://uniapp.dcloud.net.cn/api/interceptor.html#addinterceptor](https://uniapp.dcloud.net.cn/api/interceptor.html#addinterceptor) @chouchouji 有需要你可以考虑对navigateTo包装一层

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits March 5, 2026 07:48
Co-authored-by: hbcui1984 <177226+hbcui1984@users.noreply.github.com>
@github-actions
Copy link
Copy Markdown

Size report

Path Size
packages/size-check/dist/size-check.es.js 48.11 KB (+0.11% 🔺)
packages/size-check/dist/style.css 2.82 KB (0%)
packages/uni-app/dist/uni-app.es.js 2.64 KB (0%)
packages/uni-h5-vue/dist/vue.runtime.esm.js 76.48 KB (0%)
packages/uni-mp-vue/dist/vue.runtime.esm.js 43.74 KB (0%)
packages/uni-mp-alipay/dist/uni.api.esm.js 15.04 KB (0%)
packages/uni-mp-alipay/dist/uni.mp.esm.js 8.19 KB (0%)
packages/uni-mp-baidu/dist/uni.api.esm.js 11.33 KB (0%)
packages/uni-mp-baidu/dist/uni.mp.esm.js 9.68 KB (0%)
packages/uni-mp-qq/dist/uni.api.esm.js 10.65 KB (0%)
packages/uni-mp-qq/dist/uni.mp.esm.js 7.6 KB (0%)
packages/uni-mp-toutiao/dist/uni.api.esm.js 10.59 KB (0%)
packages/uni-mp-toutiao/dist/uni.mp.esm.js 9.42 KB (0%)
packages/uni-mp-weixin/dist/uni.api.esm.js 11.65 KB (0%)
packages/uni-mp-weixin/dist/uni.mp.esm.js 8.16 KB (0%)
packages/uni-quickapp-webview/dist/uni.api.esm.js 10.6 KB (0%)
packages/uni-quickapp-webview/dist/uni.mp.esm.js 8.16 KB (0%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants