Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support wildcard string prefixes #8942

Merged
merged 7 commits into from Oct 20, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
17 changes: 15 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,20 @@ export default function useLinking(

const extractPathFromURL = React.useCallback((url: string) => {
for (const prefix of prefixesRef.current) {
if (url.startsWith(prefix)) {
return url.replace(prefix, '');
/**
satya164 marked this conversation as resolved.
Show resolved Hide resolved
* https is default schema, as mentioned in android documentation
* User can add prefixes like this `example.com`
*/
const protocol = prefix.match(/^[^:]+:\/\//)?.[0] || 'https://';
satya164 marked this conversation as resolved.
Show resolved Hide resolved
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