Skip to content

Commit

Permalink
fix: handle path with empty string properly for linking (#10708)
Browse files Browse the repository at this point in the history
**Motivation**

At the moment, a linking url without a path (with or without an ending slash) cannot match the linking configuration, as it is ignored.

Consider the following linking configuration:
```
{
  prefixes: ['https://myapp.com/'],
  config: {
    screens: {
      MyScreen: '',
    },
  },
};
```
If the received linking url is equal to `https://myapp.com/` it won't be matching `MyScreen` as the empty path is ignore. To be able to match an empty path, we need to change the path validation (see change in `useLinking.native.tsx`).

The `getStateFromPath` function is already able to match an empty string. Tests have been added to clearly expose the expected behaviour. 

**Test plan**

Use a deeplink url without a path in your app and match that deeplink in your linking configuration using an empty path.
  • Loading branch information
ercote authored Aug 7, 2022
1 parent f8feedf commit e8c374e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
42 changes: 42 additions & 0 deletions packages/core/src/__tests__/getStateFromPath.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,48 @@ it('returns matching screen if path is empty', () => {
).toEqual(changePath(state, '/'));
});

it('returns matching screen if path is only slash', () => {
const path = '/';
const config = {
screens: {
Foo: {
screens: {
Foe: 'foe',
Bar: {
screens: {
Qux: '',
Baz: 'baz',
},
},
},
},
},
};

const state = {
routes: [
{
name: 'Foo',
state: {
routes: [
{
name: 'Bar',
state: {
routes: [{ name: 'Qux', path }],
},
},
],
},
},
],
};

expect(getStateFromPath<object>(path, config)).toEqual(state);
expect(
getStateFromPath<object>(getPathFromState<object>(state, config), config)
).toEqual(changePath(state, '/'));
});

it('returns matching screen with params if path is empty', () => {
const path = '?foo=42';
const config = {
Expand Down
8 changes: 8 additions & 0 deletions packages/native/src/__tests__/extractPathFromURL.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ it('extracts path from URL with protocol and host', () => {
'scheme:///example.com/some/path'
)
).toBe('/some/path');

expect(
extractPathFromURL(['scheme://example.com'], 'scheme://example.com/')
).toBe('/');

expect(
extractPathFromURL(['scheme://example.com'], 'scheme://example.com')
).toBe('');
});

it('extracts path from URL with protocol and host with wildcard', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/native/src/useLinking.native.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export default function useLinking(

const path = extractPathFromURL(prefixesRef.current, url);

return path
return path !== undefined
? getStateFromPathRef.current(path, configRef.current)
: undefined;
},
Expand Down

0 comments on commit e8c374e

Please sign in to comment.