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

fix(connect): defer token creation after iframe open #2812

Merged
merged 2 commits into from
Oct 3, 2024
Merged
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
30 changes: 26 additions & 4 deletions packages/frontend/lib/connectUI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ import type { MaybePromise } from '@nangohq/types';
import type { ConnectUIEvent, ConnectUIEventToken } from './types';

export interface ConnectUIProps {
sessionToken: string;
sessionToken?: string;
baseURL?: string;
onEvent?: (event: ConnectUIEvent) => MaybePromise<void>;
}

export class ConnectUI {
iframe: HTMLIFrameElement | null = null;

private isReady = false;
private listener: ((this: Window, ev: MessageEvent) => any) | null = null;
private sessionToken;
private baseURL;
private onEvent;
iframe: HTMLIFrameElement | null = null;

constructor({ sessionToken, baseURL = 'http://localhost:5173', onEvent }: ConnectUIProps) {
this.sessionToken = sessionToken;
Expand Down Expand Up @@ -59,8 +61,9 @@ export class ConnectUI {

switch (evt.type) {
case 'ready': {
const data: ConnectUIEventToken = { type: 'session_token', sessionToken: this.sessionToken };
this.iframe?.contentWindow?.postMessage(data, '*');
this.isReady = true;
this.sendSessionToken();

break;
}
case 'close': {
Expand All @@ -80,6 +83,16 @@ export class ConnectUI {
window.addEventListener('message', this.listener, false);
}

/**
* Set the session token and send it to the Connect UI iframe
*/
setSessionToken(sessionToken: string) {
this.sessionToken = sessionToken;
if (this.isReady) {
this.sendSessionToken();
}
}

/**
* Close UI and clear state
*/
Expand All @@ -92,4 +105,13 @@ export class ConnectUI {
this.iframe = null;
}
}

private sendSessionToken() {
if (!this.sessionToken) {
return;
}

const data: ConnectUIEventToken = { type: 'session_token', sessionToken: this.sessionToken };
this.iframe?.contentWindow?.postMessage(data, '*');
}
}
18 changes: 11 additions & 7 deletions packages/webapp/src/pages/Connection/List.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,11 @@ export default function ConnectionList() {
};
}, [debouncedSearch]);

const onClickConnectUI = async () => {
const onClickConnectUI = () => {
if (!environmentAndAccount) {
return;
}

const res = await apiConnectSessions(env);
if ('error' in res.json) {
return;
}

const nango = new Nango({
host: environmentAndAccount.host || baseUrl(),
websocketsPath: environmentAndAccount.environment.websockets_path || '',
Expand All @@ -128,14 +123,23 @@ export default function ConnectionList() {

connectUI.current = nango.openConnectUI({
baseURL: globalEnv.connectUrl,
sessionToken: res.json.data.token,
onEvent: (event) => {
if (event.type === 'close') {
// we refresh on close so user can see the diff
void mutate();
}
}
});

// We defer the token creation so the iframe can open and display a loading screen
// instead of blocking the main loop and no visual clue for the end user
setTimeout(async () => {
const res = await apiConnectSessions(env);
if ('error' in res.json) {
return;
}
connectUI.current!.setSessionToken(res.json.data.token);
}, 10);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a pretty short value. no? what happen if the call to connect/sessions takes longer than that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The call is triggered after 10ms.

The setTimeout is just here to free the main event loop, which means the iframe can be created and the URL loading started, the iframe doesn't have a separate sandbox so it's important that it has time to load and display something.
Then the setTimeout triggers and it takes as much time as it needs.
10ms is not a lot but on a good connection waiting for nothing would be detrimental to UX.

};

if (error) {
Expand Down
Loading