Google Maps API loading and provider lifecycle
APIProvider is the application boundary for the Google Maps JavaScript API. It accepts the API key and first-load settings, starts one shared API load, and gives React children a context with API state and library access.
Provider role
The provider keeps Google Maps setup outside individual map components. It renders its children while it loads the API, then updates them when the shared API state changes. Most consumers should use the public hooks rather than read the provider context directly.
Initial configuration
The first APIProvider render defines the API key and load settings such as version, language, region, referrer policy, and requested libraries. It also accepts usage channels, usage attribution control, App Check token fetching, and load callbacks. These settings describe the API session, so later provider instances must use the same values.
References: src/…/api-provider.tsx• L47-116 src/…/api-provider.tsx• L225-246
Runtime load ownership
The API loader uses module-level state because the Maps JavaScript API can load only once in a browser runtime. The first load records the serialized settings, starts the API loader, and requests the core and maps libraries plus any libraries named by the provider. Later providers observe the same load instead of starting a second one.
The provider warns when a later instance supplies different settings. The new settings do not replace the settings used by the existing API session.
References: src/…/api-provider.tsx• L118-136 src/…/api-provider.tsx• L277-363
Loading lifecycle
The provider reports a small state machine to React consumers. The normal path starts at NOT_LOADED, moves to LOADING, and ends at LOADED; a load error ends at FAILED.
The following diagram shows how provider configuration becomes shared API access.
flowchart LR
A[APIProvider settings] --> B[Shared loader state]
B --> C[Set Google Maps loader options]
C --> D[Load core and maps libraries]
D -->|success| E[LOADED context]
D -->|error| F[FAILED context and onError]
E --> G[React consumers]
Loading states
APILoadingStatus is the shared vocabulary for API readiness. useApiLoadingStatus() returns the current value, while useApiIsLoaded() returns true only for LOADED; consumers can use the child page API loading status and library hooks for the hook-level behavior.
| State | Meaning in the provider lifecycle |
|---|---|
NOT_LOADED | No provider-owned API load has started. |
LOADING | The API and its required initial libraries are being requested. |
LOADED | The required initial libraries are ready for consumers. |
FAILED | Loading or initial library import failed. |
AUTH_FAILURE | A separate authorization-failure state in the shared status model; map rendering treats it as an auth error. |
References: src/…/api-loading-status.ts• L1-9 src/…/api-provider.tsx• L212-223 src/…/use-api-loading-status.ts• L1-7 src/…/use-api-is-loaded.ts• L1-10 src/…/index.tsx• L217-225
Load completion
The provider marks the API as loaded only after the required initial library imports resolve. It then calls onLoad, if supplied, and makes the loaded libraries available through context. If the API already installed google.maps.importLibrary, the provider adopts that loader and waits for the same required libraries before reporting LOADED.
When the API is ready, an optional fetchAppCheckToken function is attached to Google Maps settings. This lets later API requests obtain App Check tokens without changing the provider lifecycle.
References: src/…/api-provider.tsx• L324-340 src/…/api-provider.tsx• L352-367 src/…/api-provider.tsx• L385-393
Failure handling
Any error from the initial API or library load changes the provider state to FAILED. The provider calls onError when present. Without that callback, it writes the failure to the console. Consumers should use the loading status to avoid creating Google Maps objects before readiness.
References: src/…/api-provider.tsx• L368-379 src/…/use-api-loading-status.ts• L5-7
Consumer context
The provider supplies one context value to its descendants. The value combines API state and library import access with registries for active 2D and 3D map instances, so map components and hooks can share the same API session.
React context
APIProviderContext is the internal connection between the provider and library consumers. The provider publishes status, loaded libraries, and an import function together with named map and 3D map instance access. Applications normally reach these capabilities through public hooks and components rather than through the context object.
References: src/…/api-provider.tsx• L21-45 src/…/api-provider.tsx• L432-467
Library access
The provider exposes a cached import function for optional Google Maps libraries. A request imports a library once, stores the resolved library in context, and returns the cached value for later requests. The child page API loading status and library hooks explains how useMapsLibrary() turns this access into a React-friendly loading pattern.
References: src/…/api-provider.tsx• L248-262 src/…/api-provider.tsx• L395-399 src/…/use-maps-library.ts• L8-28
Map instance access
The same context tracks map instances by name. A default name supports the common single-map case, while explicit names let an application keep several map instances available to hooks and related components. The provider also keeps a separate registry for 3D map elements.
References: src/…/api-provider.tsx• L139-190 src/…/api-provider.tsx• L21-35
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the one-load rule aligned with the serialized option check and with behavior for multiple React roots.
- Keep
APILoadingStatusvalues aligned with provider transitions and with consumers that render authorization failures. - Keep the initial library set aligned with the point at which
LOADEDis published andonLoadruns. - Keep the context contract aligned with the loading hooks, library hook, and map instance access paths.
- Keep externally installed
google.maps.importLibrarybehavior aligned with option setup and readiness reporting.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A later provider uses different API settings | The new settings are ignored and a warning is logged. | Use one provider when possible and set final values on its first render. |
| The API or a required initial library rejects | Status becomes FAILED; onError runs or the error is logged. | Render API-dependent behavior from the loading status. |
| A library is requested before the API is loaded | The library hook does not start the request yet. | Wait for LOADED through the public loading hooks. |
| A provider is missing above a consumer | The loading hook falls back to NOT_LOADED; context-dependent map access cannot resolve a provider. | Place API-dependent components below APIProvider. |
Key files
| File | Why to read it |
|---|---|
| src/…/api-provider.tsx | Defines provider configuration, shared load ownership, context values, and failure callbacks. |
| src/…/api-loading-status.ts | Defines the status values shared by the provider and consumers. |
| src/…/use-api-loading-status.ts | Shows the public status read from provider context. |
| src/…/use-api-is-loaded.ts | Shows the simple readiness check used by API-dependent consumers. |
| src/…/use-maps-library.ts | Shows how optional libraries are requested after API readiness. |
| src/…/index.tsx | Shows how a map responds to the authorization-failure status. |