Skip to content

Commit

Permalink
feat: support wildcard string prefixes (#8942)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
hosseinmd and satya164 committed Oct 20, 2020
1 parent 80ff5a9 commit 23ab350
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/native/package.json
Expand Up @@ -38,6 +38,7 @@
},
"dependencies": {
"@react-navigation/core": "^5.12.5",
"escape-string-regexp": "^4.0.0",
"nanoid": "^3.1.12"
},
"devDependencies": {
Expand Down Expand Up @@ -70,4 +71,4 @@
]
]
}
}
}
6 changes: 6 additions & 0 deletions packages/native/src/types.tsx
Expand Up @@ -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[];
/**
Expand Down
13 changes: 11 additions & 2 deletions packages/native/src/useLinking.native.tsx
Expand Up @@ -6,6 +6,7 @@ import {
NavigationContainerRef,
} from '@react-navigation/core';
import type { LinkingOptions } from './types';
import escapeStringRegexp from 'escape-string-regexp';

let isUsingLinking = false;

Expand Down Expand Up @@ -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, '');
}
}

Expand Down

0 comments on commit 23ab350

Please sign in to comment.