My index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="google-signin-client_id" content= "my_client_id.apps.googleusercontent.com">
<meta name="google-signin-scope" content="profile email">
<script src="https://apis.google.com/js/client:platform.js?onload=start" async defer></script>
<script>
function start() {
console.log('script running')
gapi.load('auth2', function() {
auth2 = gapi.auth2.init({
client_id: 'my_client_id.apps.googleusercontent.com',
scope: 'profile email'
});
});
}
</script>
<title>React App</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
In the start()
function I print to the console to see when it's running.
When I load the page, every so often start()
will load after the react components.
Login.js
componentDidMount() {
console.log(gapi.auth2.getAuthInstance())
}
In the debugger you can see that the script is loading after the component:
If I refresh the page a few times, it works fine. But sometimes it works, sometimes it doesn't.
Why?