fix(google-api): wait for the Google Identity script and reject when loading fails (#18179)

* fix(google-api): wait for the Google Identity script and reject when loading fails

* fix(google-api): track each library load and surface failures to callers

* fix(google-api): retry explicitly and only flag real load failures
This commit is contained in:
MANI authored and GitHub committed 2026-11-06 12:09:57 -06:00
1 parent e1c1694828
commit fda859b9c2
5 files changed
+146 -34

No files matched your search

+1
View File
@@ -769,6 +769,7 @@
"enterStreamKeyGeneric": "Enter your live stream key or RTMP URL here.",
"error": "Live Streaming failed. Please try again.",
"errorAPI": "An error occurred while accessing your YouTube broadcasts. Please try logging in again.",
"errorGoogleApiLoad": "Google sign-in could not be loaded. Check that it is not blocked by your browser or network and try again, or enter your live stream key below.",
"errorLiveStreamNotEnabled": "Live Streaming is not enabled on {{email}}. Please enable live streaming or log into an account with live streaming enabled.",
"errorPopupBlocked": "The Google sign-in popup was blocked. Allow popups for this site and try again, or enter your live stream key below.",
"expandedOff": "The live streaming has stopped",
+4 -2
View File
@@ -30,11 +30,13 @@ export function getCalendarEntries(
* Loads Google API. It does not prompt the user to sign in, that has to be
* started explicitly by the user with {@link signIn}.
*
* @param {boolean} retryFailedLoads - Whether to try a failed library load
* again right away, for a retry requested by the user.
* @returns {Function}
*/
export function loadGoogleAPI() {
export function loadGoogleAPI(retryFailedLoads = false) {
return (dispatch: IStore['dispatch'], getState: IStore['getState']) =>
googleApi.get()
googleApi.get(retryFailedLoads)
.then(() => {
const {
enableCalendarIntegration,
+107 -27
View File
@@ -13,12 +13,37 @@ const GOOGLE_API_CLIENT_LIBRARY_URL = 'https://apis.google.com/js/api.js';
const GOOGLE_GIS_LIBRARY_URL = 'https://accounts.google.com/gsi/client';
/**
* A promise for dynamically loading the Google API Client Library.
* How long to wait for a library to load before giving up, in milliseconds.
*
* @private
* @type {Promise}
* @type {number}
*/
let googleClientLoadPromise;
const LOAD_TIMEOUT = 30000;
/**
* How long a failed library load is reused before it can be tried again, in
* milliseconds. Avoids a new request for every call while a library is blocked.
*
* @private
* @type {number}
*/
const LOAD_RETRY_DELAY = 5000;
/**
* The promises for dynamically loading the Google libraries, by library URL.
*
* @private
* @type {Map<string, Promise>}
*/
const libraryLoads = new Map();
/**
* The URLs of the libraries whose load failed and can be tried again.
*
* @private
* @type {Set<string>}
*/
const failedLibraryLoads = new Set();
/**
* A singleton for loading and interacting with the Google API.
@@ -28,12 +53,19 @@ const googleApi = {
* Obtains Google API Client Library, loading the library dynamically if
* needed.
*
* @param {boolean} retryFailedLoads - Whether to try a failed library load
* again right away, for a retry requested by the user.
* @returns {Promise}
*/
get() {
get(retryFailedLoads = false) {
if (retryFailedLoads) {
failedLibraryLoads.forEach(url => this._clearFailedLoad(url));
}
const globalGoogleApi = this._getGoogleApiClient();
if (!globalGoogleApi) {
// Both the client library and Google Identity Services are needed.
if (!globalGoogleApi?.client || !this._getGoogleGISApiClient()?.accounts) {
return this.load();
}
@@ -139,14 +171,23 @@ const googleApi = {
_loadScriptTag(src) {
return new Promise((resolve, reject) => {
const scriptTag = document.createElement('script');
let timeout = null;
scriptTag.async = true;
scriptTag.addEventListener('error', () => {
const onFailure = message => {
clearTimeout(timeout);
scriptTag.remove();
reject();
reject(new Error(`${message} ${src}`));
};
timeout = setTimeout(() => onFailure('Timed out loading'), LOAD_TIMEOUT);
scriptTag.async = true;
scriptTag.addEventListener('error', () => onFailure('Failed to load'));
scriptTag.addEventListener('load', () => {
clearTimeout(timeout);
resolve();
});
scriptTag.addEventListener('load', resolve);
scriptTag.type = 'text/javascript';
scriptTag.src = src;
@@ -156,31 +197,70 @@ const googleApi = {
},
/**
* Generates a script tag and downloads the Google API Client Library.
* Generates script tags and downloads the Google API Client Library and
* the Google Identity Services Library. Rejects if either fails to load.
* Each library is tracked on its own, so a retry only requests the one
* that failed, and not one that is still loading. A script that timed out
* can still run later, so a tag is only added if the library is missing.
*
* @returns {Promise}
*/
load() {
if (googleClientLoadPromise) {
return googleClientLoadPromise;
return Promise.all([
this._loadLibrary(GOOGLE_API_CLIENT_LIBRARY_URL, () =>
(this._getGoogleApiClient() ? Promise.resolve() : this._loadScriptTag(GOOGLE_API_CLIENT_LIBRARY_URL))
.then(() => new Promise((resolve, reject) =>
this._getGoogleApiClient().load('client', {
callback: resolve,
onerror: () => reject(new Error('Failed to load the Google API client')),
ontimeout: () => reject(new Error('Timed out loading the Google API client')),
timeout: LOAD_TIMEOUT
})))),
this._loadLibrary(GOOGLE_GIS_LIBRARY_URL, () => {
if (this._getGoogleGISApiClient()?.accounts) {
return Promise.resolve();
}
return this._loadScriptTag(GOOGLE_GIS_LIBRARY_URL);
})
])
.then(() => this._getGoogleApiClient());
},
/**
* Loads a library once. A failure is reused for a while, so a blocked
* library doesn't cause a new request for every call.
*
* @param {string} url - The URL of the library.
* @param {Function} load - Starts loading the library.
* @private
* @returns {Promise}
*/
_loadLibrary(url, load) {
if (!libraryLoads.has(url)) {
libraryLoads.set(url, load().catch(error => {
logger.error('Error loading a Google library', error);
failedLibraryLoads.add(url);
setTimeout(() => this._clearFailedLoad(url), LOAD_RETRY_DELAY);
throw error;
}));
}
googleClientLoadPromise = this._loadScriptTag(GOOGLE_API_CLIENT_LIBRARY_URL)
.catch(() => {
googleClientLoadPromise = null;
})
.then(() => new Promise((resolve, reject) =>
this._getGoogleApiClient().load('client', {
callback: resolve,
onerror: reject
})))
.then(this._loadScriptTag(GOOGLE_GIS_LIBRARY_URL))
.catch(() => {
googleClientLoadPromise = null;
})
.then(() => this._getGoogleApiClient());
return libraryLoads.get(url);
},
return googleClientLoadPromise;
/**
* Forgets a failed library load, so the next call tries it again.
*
* @param {string} url - The URL of the library.
* @private
* @returns {void}
*/
_clearFailedLoad(url) {
if (failedLibraryLoads.delete(url)) {
libraryLoads.delete(url);
}
},
/**
@@ -28,6 +28,11 @@ import AbstractStartLiveStreamDialog, {
import StreamKeyForm from './StreamKeyForm';
import StreamKeyPicker from './StreamKeyPicker';
/**
* The error type used when the Google API libraries could not be loaded.
*/
const GOOGLE_API_LOAD_FAILED = 'googleApiLoadFailed';
interface IProps extends AbstractProps {
/**
@@ -60,6 +65,7 @@ class StartLiveStreamDialog
this._onInitializeGoogleApi = this._onInitializeGoogleApi.bind(this);
this._onGoogleSignIn = this._onGoogleSignIn.bind(this);
this._onRequestGoogleSignIn = this._onRequestGoogleSignIn.bind(this);
this._onRetryGoogleApi = this._onRetryGoogleApi.bind(this);
this._onYouTubeBroadcastIDSelected
= this._onYouTubeBroadcastIDSelected.bind(this);
}
@@ -118,12 +124,28 @@ class StartLiveStreamDialog
* If the user is already logged in, then a request for available YouTube
* broadcasts is also made.
*
* @param {boolean} retryFailedLoads - Whether to try a failed library load
* again right away.
* @private
* @returns {void}
*/
_onInitializeGoogleApi() {
this.props.dispatch(loadGoogleAPI())
.catch((response: any) => this._parseErrorFromResponse(response));
_onInitializeGoogleApi(retryFailedLoads = false) {
// Back to the spinner while retrying after a failed load.
this._setStateIfMounted({ errorType: undefined });
// The Google API stays unusable after this fails, whatever the reason.
this.props.dispatch(loadGoogleAPI(retryFailedLoads))
.catch(() => this._setStateIfMounted({ errorType: GOOGLE_API_LOAD_FAILED }));
}
/**
* Tries again to load the Google web client application after it failed.
*
* @private
* @returns {void}
*/
_onRetryGoogleApi() {
this._onInitializeGoogleApi(true);
}
/**
@@ -333,7 +355,8 @@ class StartLiveStreamDialog
if (this.state.errorType !== undefined) {
googleContent = (
<GoogleSignInButton
onClick = { this._onRequestGoogleSignIn } />
onClick = { this.state.errorType === GOOGLE_API_LOAD_FAILED
? this._onRetryGoogleApi : this._onRequestGoogleSignIn } />
);
helpText = this._getGoogleErrorMessageToDisplay();
}
@@ -360,6 +383,9 @@ class StartLiveStreamDialog
let text;
switch (this.state.errorType) {
case GOOGLE_API_LOAD_FAILED:
text = this.props.t('liveStreaming.errorGoogleApiLoad');
break;
case 'liveStreamingNotEnabled':
text = this.props.t(
'liveStreaming.errorLiveStreamNotEnabled',
@@ -163,7 +163,10 @@ class CalendarTab extends Component<IProps, IState> {
* @returns {void}
*/
_attemptSignIn(type: string) {
this.props.dispatch(signIn(type));
this.props.dispatch(signIn(type))
.catch(() => {
// The error is already logged by signIn.
});
}
/**