mirror of
https://github.com/zhigang1992/react-navigation.git
synced 2026-04-29 12:55:21 +08:00
feat: support wildcard string prefixes (#8942)
Prefixes should be more flexible for situations like wild card subdomain. On android and IOS we can define wild cards by * but react-navigation does not work, In this PR I added support for RegExp Prefixes.
For Example
```js
{
prefixes: [
/^[^.s]+.example.com/g
],
}
```
I tested this work well.
Closes #8941
Co-authored-by: Satyajit Sahoo <satyajit.happy@gmail.com>
This commit is contained in:
committed by
GitHub
parent
80ff5a9c54
commit
23ab350492
@@ -38,6 +38,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@react-navigation/core": "^5.12.5",
|
||||
"escape-string-regexp": "^4.0.0",
|
||||
"nanoid": "^3.1.12"
|
||||
},
|
||||
"devDependencies": {
|
||||
@@ -70,4 +71,4 @@
|
||||
]
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,6 +27,12 @@ export type LinkingOptions = {
|
||||
* The prefixes are stripped from the URL before parsing them.
|
||||
* Usually they are the `scheme` + `host` (e.g. `myapp://chat?user=jane`)
|
||||
* Only applicable on Android and iOS.
|
||||
*
|
||||
* @example
|
||||
* prefixes: [
|
||||
* "https://example.com", // Exact
|
||||
* "https://*.example.com" // Match with any subdomain
|
||||
* ]
|
||||
*/
|
||||
prefixes: string[];
|
||||
/**
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
NavigationContainerRef,
|
||||
} from '@react-navigation/core';
|
||||
import type { LinkingOptions } from './types';
|
||||
import escapeStringRegexp from 'escape-string-regexp';
|
||||
|
||||
let isUsingLinking = false;
|
||||
|
||||
@@ -58,8 +59,16 @@ export default function useLinking(
|
||||
|
||||
const extractPathFromURL = React.useCallback((url: string) => {
|
||||
for (const prefix of prefixesRef.current) {
|
||||
if (url.startsWith(prefix)) {
|
||||
return url.replace(prefix, '');
|
||||
const protocol = prefix.match(/^[^:]+:\/\//)?.[0] ?? '';
|
||||
const host = prefix.replace(protocol, '');
|
||||
const prefixRegex = new RegExp(
|
||||
`^${escapeStringRegexp(protocol)}${host
|
||||
.split('.')
|
||||
.map((it) => (it === '*' ? '[^/]+' : escapeStringRegexp(it)))
|
||||
.join('\\.')}`
|
||||
);
|
||||
if (prefixRegex.test(url)) {
|
||||
return url.replace(prefixRegex, '');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user