Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Invalid/Expired API key crashes the page #137

Closed
iqbalio opened this issue Dec 21, 2023 · 4 comments
Closed

[Bug] Invalid/Expired API key crashes the page #137

iqbalio opened this issue Dec 21, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@iqbalio
Copy link

iqbalio commented Dec 21, 2023

Description

If the API key is expired/wrong. Then the page crashes once we move away from the map page. It throws the following error

Cannot read properties of undefined (reading 'remove')

Steps to Reproduce

https://codesandbox.io/p/sandbox/gracious-rain-slhtvx

  1. Open the above sandbox and click on "Map" link
  2. Map page will open and It will show "Oops! Something went wrong." as the API key is wrong.
  3. Click on Home link to go back to home page
  4. Click on Map again

Now after this If you move again to home page or any other route, It'll crash the page.

Environment

  • Library version: 0.4.1
  • Google maps version: weekly
  • Browser and Version: Chrome 120
  • OS: Windows 11

Logs

No response

@iqbalio iqbalio added the bug Something isn't working label Dec 21, 2023
@usefulthink
Copy link
Collaborator

This seems to be coming from the effect-cleanup for the useMapsEvents hook.
Needs some further investigation.

We should also implement the window.gm_authFailure callback in the API-loader so we can properly react to problems with the API-key.

@usefulthink
Copy link
Collaborator

usefulthink commented Jan 16, 2024

Interesting:

useEffect(() => {
if (!map) return;
if (!handler) return;
const listener = map.addListener(
eventType,
(ev?: google.maps.MapMouseEvent | google.maps.IconMouseEvent) => {
const mapEvent = createMapEvent(eventType, map, ev);
trackDispatchedEvent(mapEvent, cameraStateRef);
handler(mapEvent);
}
);
return () => listener.remove();
}, [map, cameraStateRef, eventType, handler]);

For the error to happen, listener has to be undefined in line 76, which - at least according to the google maps docs can't happen. I verified that in the debugger and indeed there seems to be a situation where the google.maps.Map instance has an empty function instead of addListener which isn't documented anywhere. i'll try to replace that with google.maps.events.addListener to see if that is more reliable.

usefulthink added a commit to usefulthink/react-google-maps that referenced this issue Jan 17, 2024
As discovered in visgl#137, there are apparently situations where the `.addListener()` function that components inherit from the MVCObject class will return undefined in certain edge cases, like when the Map is rendering the fallback-screen for an invalid API-key.

Therefore we will use the hopefully more stable `google.maps.event.addListener` method everywhere.
@usefulthink
Copy link
Collaborator

So the main issue should be adressed with #158. Unfortunately the maps API doesn't provide a reasonable way to react to an invalid API key in any other way. The "official" way to handle this would be the window.gm_authFailure callback, but that would only be useful for informational purpose, since it is called a long time after the "maps api ready" callback.

usefulthink added a commit that referenced this issue Jan 17, 2024
As discovered in #137, there are apparently situations where the `.addListener()` function that components inherit from the MVCObject class will return undefined in certain edge cases, like when the Map is rendering the fallback-screen for an invalid API-key.

Therefore we will use the hopefully more stable `google.maps.event.addListener` method everywhere.

Co-authored-by: Malte Modrow <malte.modrow@gmail.com>
@usefulthink
Copy link
Collaborator

With #158 and #165 this should be taken care of. Will release a new Version later today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants