Browse Source

fix: 添加页面不存在兜底 + 配置miniprogramRoot

- app.ts: 新增 usePageNotFound hook, 页面不存在时 reLaunch 到首页
- mini.project.json: 添加 miniprogramRoot 指向 dist 构建目录
alphaH 2 weeks ago
parent
commit
b94f8afc49
2 changed files with 9 additions and 2 deletions
  1. 2 1
      mini.project.json
  2. 7 1
      src/app.ts

+ 2 - 1
mini.project.json

@@ -1,3 +1,4 @@
 {
-  "format": 2
+  "format": 2,
+  "miniprogramRoot": "dist"
 }

+ 7 - 1
src/app.ts

@@ -1,5 +1,5 @@
 import { PropsWithChildren } from 'react';
-import { useLaunch } from '@tarojs/taro';
+import Taro, { useLaunch, usePageNotFound } from '@tarojs/taro';
 
 import '@nutui/nutui-react-taro/dist/style.css';
 import '@nutui/nutui-react-taro/dist/styles/themes/jmapp.css';
@@ -19,6 +19,12 @@ function App({ children }: PropsWithChildren<any>) {
     }
   });
 
+  // 页面不存在兜底:线上环境严格校验页面路径时,可避免白屏/错误页
+  usePageNotFound((res) => {
+    console.error('[App] onPageNotFound — path:', res.path, 'isEntryPage:', res.isEntryPage);
+    Taro.reLaunch({ url: '/pages/index/index' });
+  });
+
   // children 是将要会渲染的页面
   return children;
 }