Cancel Google One Tap on login unmount#26
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
There was a problem hiding this comment.
Pull request overview
This PR updates the frontend Login page’s Google One Tap integration to ensure the One Tap prompt is properly cancelled on unmount, including when the Google script is already present before the page mounts.
Changes:
- Extracted a reusable
cancelGoogleOneTapcleanup function. - Returned the cleanup function from the
useEffecteven when the Google script is already present (previously returned early without cleanup).
| if (existingScript) { | ||
| initializeGoogleOneTap(); | ||
| return; | ||
| return cancelGoogleOneTap; | ||
| } |
There was a problem hiding this comment.
existingScript only indicates the tag exists, not that window.google.accounts.id is ready. If the script is still loading, initializeGoogleOneTap() returns early and One Tap will never be initialized because no load handler is attached in this branch. Consider checking for window.google?.accounts?.id and, if missing, attaching a one-time load listener to the existing script (or polling) before calling initializeGoogleOneTap().
There was a problem hiding this comment.
Addressed in 6d1a4a5 by checking whether google.accounts.id is ready before initialization and attaching a one-time load listener when the existing script is still loading.
| @@ -105,11 +111,7 @@ export default function Login() { | |||
| script.onload = initializeGoogleOneTap; | |||
| document.head.appendChild(script); | |||
|
|
|||
| return () => { | |||
| const googleClient = (window as Window & { google?: GoogleOneTapClient }) | |||
| .google; | |||
| googleClient?.accounts?.id?.cancel(); | |||
| }; | |||
| return cancelGoogleOneTap; | |||
There was a problem hiding this comment.
The cleanup only calls google.accounts.id.cancel(). If the component unmounts before the script finishes loading, script.onload can still fire later and run initializeGoogleOneTap() (with stale closures), potentially prompting One Tap when the Login page is no longer mounted. Consider clearing the onload handler (or using addEventListener + removeEventListener) and/or guarding initializeGoogleOneTap() with an isMounted/cancelled flag set by the cleanup.
There was a problem hiding this comment.
Addressed in 6d1a4a5 by using a cancelled guard and removing the load listener during cleanup, so a late script load cannot initialize One Tap after the login page unmounts.
Summary
Verification