You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Navigation V6深度链接配置:排除指定URL路径需求

解决React Navigation v6排除指定深度链接路径的问题

要实现仅启用/m和/tag路径的深度链接,同时让Android Intent和React Navigation忽略/app路径,需要分别调整AndroidManifest配置和React Navigation深度链接配置,具体步骤如下:

一、Android端配置调整

修改AndroidManifest.xml中的Intent Filter,仅匹配/m和/tag开头的路径,这样/app路径的URL不会被App捕获:

<!-- 保留原有的example scheme过滤 -->
<intent-filter>
   <action android:name="android.intent.action.VIEW" />
   <category android:name="android.intent.category.DEFAULT" />
   <category android:name="android.intent.category.BROWSABLE" />
   <data android:scheme="example" />
</intent-filter>

<!-- 修改HTTP/HTTPS的Intent Filter,仅匹配/m和/tag路径 -->
<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    
    <!-- 匹配/m开头的路径 -->
    <data android:scheme="https" android:host="www.example.com" android:pathPrefix="/m" />
    <data android:scheme="http" android:host="www.example.com" android:pathPrefix="/m" />
    <data android:scheme="https" android:host="example.com" android:pathPrefix="/m" />
    <data android:scheme="http" android:host="example.com" android:pathPrefix="/m" />
    
    <!-- 匹配/tag开头的路径 -->
    <data android:scheme="https" android:host="www.example.com" android:pathPrefix="/tag" />
    <data android:scheme="http" android:host="www.example.com" android:pathPrefix="/tag" />
    <data android:scheme="https" android:host="example.com" android:pathPrefix="/tag" />
    <data android:scheme="http" android:host="example.com" android:pathPrefix="/tag" />
</intent-filter>

通过android:pathPrefix指定仅拦截以/m或/tag开头的URL,/app路径的请求会直接留在浏览器中,不会触发App的Intent。

二、React Navigation深度链接配置调整

原有配置已经正确映射了/m和/tag路径,为了进一步确保忽略/app路径,可以添加自定义的URL过滤逻辑:

import { Linking } from 'react-native';

const deepLinking = {
    prefixes: [
      'https://www.example.com',
      'http://www.example.com',
      'https://*.example.com',
      'http://example.com',
      'https://example.com',
      'example://'
    ],
    config: {
      screens: {
        HOME: 'home',
        LIVE: 'm/:uuid',
        TAGList: 'tag/:enVal',
        // 不定义任何与/app相关的screen映射
      }
    },
    // 过滤初始URL中的/app路径
    async getInitialURL() {
      const url = await Linking.getInitialURL();
      if (url?.includes('/app')) {
        return null; // 返回null让React Navigation忽略该URL
      }
      return url;
    },
    // 监听链接变化时过滤/app路径
    subscribe(listener) {
      const onReceiveURL = ({ url }) => {
        if (!url.includes('/app')) {
          listener(url);
        }
      };

      const subscription = Linking.addEventListener('url', onReceiveURL);
      return () => subscription.remove();
    }
  };

说明:

  1. getInitialURL:当App通过深度链接启动时,检查URL是否包含/app,如果是则返回null,React Navigation会忽略该链接,不会进行页面跳转。
  2. subscribe:监听App运行过程中收到的深度链接,同样过滤掉包含/app的URL,确保这类链接不会触发页面跳转。

内容的提问来源于stack exchange,提问作者m.akbari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 14:48:15