Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions public/mobile-app/src/app.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ declare global {
interface Window {
_paq?: any[];
NativeBridge?: any;
NativeURLs?: string[];
}
}

Expand Down
2 changes: 1 addition & 1 deletion public/mobile-app/src/lib/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const fetchAndStoreNotifications = async () => {
};

export const getNotificationsFromStore = async (): Promise<AppNotification[]> => {
const notificationsString: string = localStorage.getItem('notifications') || '';
const notificationsString: string = localStorage.getItem('notifications') || '[]';
return JSON.parse(notificationsString);
};

Expand Down
18 changes: 17 additions & 1 deletion public/mobile-app/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import '@gouvfr/dsfr/dist/utility/utility.min.css';
import '../app.css';
import { onMount } from 'svelte';
import { afterNavigate, goto } from '$app/navigation';
import { afterNavigate, beforeNavigate, goto } from '$app/navigation';
import { env } from '$env/dynamic/public';
import Toasts from '$lib/components/Toasts.svelte';
import { initDsfr } from '$lib/dsfr';
import { initMatomo, trackPageView } from '$lib/matomo';
import { emit } from '$lib/nativeEvents';

let { children } = $props();

Expand All @@ -21,6 +22,21 @@
initMatomo();
});

beforeNavigate((navigation) => {
const url = navigation.to?.url;
if (!url) {
return;
}
emit('navigateTo', url);
const path = url.href.replace(url.origin, ''); // Remove the `https://xxxx.yyy:zzzz` part of the current url, keep eg `/#/settings`.
if (window.NativeURLs?.includes(path)) {
console.log(
`Cancel navigation to ${url}, found an entry in the window.NativeURLs, let the mobile app handle it`
);
navigation.cancel();
}
});

afterNavigate(() => {
trackPageView(document.title);
});
Expand Down
45 changes: 45 additions & 0 deletions public/mobile-app/src/routes/layout.svelte.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('+layout.svelte', () => {
afterEach(() => {
vi.resetAllMocks();
delete window.NativeBridge;
delete window.NativeURLs;
});

describe('whitelisting', () => {
Expand Down Expand Up @@ -76,4 +77,48 @@ describe('+layout.svelte', () => {
});
});
});

describe('cancelling navigation to pages promoted in mobile apps', () => {
test('should cancel navigation if the URL is in window.NativeURLs', () => {
// Given
let beforeNavigateCallback: (navigation: any) => void;
vi.spyOn(navigationMethods, 'beforeNavigate').mockImplementation((cb) => {
beforeNavigateCallback = cb;
});
window.NativeURLs = ['/#/settings'];
render(Layout, { children: (() => {}) as any });

const cancel = vi.fn();

// When
beforeNavigateCallback!({
to: { url: new URL('https://localhost:5173/#/settings') },
cancel,
});

// Then
expect(cancel).toHaveBeenCalled();
});

test('should not cancel navigation if the URL is not in window.NativeURLs', () => {
// Given
let beforeNavigateCallback: (navigation: any) => void;
vi.spyOn(navigationMethods, 'beforeNavigate').mockImplementation((cb) => {
beforeNavigateCallback = cb;
});
window.NativeURLs = ['/#/settings'];
render(Layout, { children: (() => {}) as any });

const cancel = vi.fn();

// When
beforeNavigateCallback!({
to: { url: new URL('https://localhost:5173/#/other') },
cancel,
});

// Then
expect(cancel).not.toHaveBeenCalled();
});
});
});
Loading