Repository navigation
Conversation
api.js, gsi/client and the Analytics API discovery docs were only requested from onInitialClientRender, i.e. after the JS bundle had downloaded and React had hydrated, so the login check and the first API calls waited on the whole app first. Add an inline loader to the document head (onRenderBody) that starts fetching api.js and gsi/client immediately and loads the discovery docs as soon as gapi is ready, exposing the promises on window.__googleScripts. onInitialClientRender picks them up (falling back to loading the scripts itself) and no longer waits for the sign-in script before fetching the discovery docs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On every page load the login button and the tools that need the Analytics APIs stay in a loading state for a noticeable time before the signed-in state is known.
Cause
api.js,gsi/clientand the Analytics Data/Admin API discovery docs are only requested fromonInitialClientRender, i.e. after the JS bundle has downloaded, parsed and React has hydrated. On top of that, the discovery docs are only fetched after both scripts have loaded, so the slower sign-in script also delays them. All of this is sequential.Fix
gatsby-ssr.tsx: add anonRenderBodythat injects a small inline loader into<head>. It starts loadingapi.jsandgsi/clientimmediately, loads the discovery docs as soon asgapi.clientis ready, and exposes the promises onwindow.__googleScripts.gatsby/onInitialClientRender.js: use the preloaded promises when present (falling back to loading the scripts itself if not), fetch the discovery docs without waiting for the sign-in script, and split the code into small helpers (loadGapi,loadGis,restoreToken). The token client setup, stored-token restore and error handling behave as before.How tested
yarn check-types: no new errors (the 3 existing errors onmainare unrelated).gatsby buildsucceeds and the generated HTML contains the loader script in<head>.api.jsandgsi/clientnow start loading about 150ms after navigation instead of after hydration.