-
Notifications
You must be signed in to change notification settings - Fork 64
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
Loader not downloading the google map script post deleting window.google object on component unmount #358
Comments
First, I would not recommend this pattern of deleting and reloading as it will result in double billing. If you want this behavior, you can try using the private loader.reset() method. duplicate of #100 |
Can you elaborate on this use case and why it is necessary? |
In my application, the location field is used on a page that is not going to be used frequently. Once the user leaves that page, we don't want the google script and the object to persist as it most likely not going to be used as long as the user is on the application. This is why we are doing it as a part of the cleanup function.
|
@jpoehnelt Is there a better way that you think of where we can achieve the expected outcome. I just tried and see that i can get the api to load. It's just that i have to call |
Double billing would only be in cases where you create a google.maps.Map instance combined with reloading the entire API. That might not be the case here.
Why does this matter? Would probably want this order of steps: // cleanup
delete window.google;
loader.deleteScript();
loader.reset();
// later
loader.load(); |
The idea to delete the script and the google object was because of the reason that we wanted to unload the effect that we are making use of once the component is unmounted, as most likely it is not going to be used again. There were no other strong reasons. But giving some more thought to it, i think that it's better to conditionally create a new instance of loader only the window.google object doesn't exist. So we will go ahead with that approach. In future, when #100 is implemented, we will update our code. I'll close this, thanks for the prompt response @jpoehnelt . |
The Loader class acts as a singleton and will do this for you so long as you pass the same settings (key, libraries, etc). |
I have a user case for region dynamic change. |
I have used the js-api-loader to develop a location search field which would give a list of suggestions (
using the autocomplete service
) when the user begins to type in the search box. This functionality is something that is not going to be used often. So, we delete the script and thewindow.google
object on component unmount. However, we notice that on revisiting the page where this search field is located, the Loader does not make a call to refetch the google map script.Environment details
Specify the API at the beginning of the title
places
OS type and version -
All Platforms, Windows, Mac and Linux. Desktop and Mobile as well
Library version and other environment information
"@googlemaps/js-api-loader": "~1.12.1"
"react": "~17.0.2",
"next": "~10.1.3",
Since we use NextJS with server side rendering enabled, the
window
object is available only in functions that run on the client side. Code sample belowCode example
The above is not the complete piece of code. It is just given for a rough estimation of how we are loading and deleting the google map js script on mounting and unmounting.
Please let me know if you need any more information
The text was updated successfully, but these errors were encountered: