How to avoid 'Loader must not be called again with different options' error when using nonce in useJsApiLoader? #3023
|
Hello! I'm currently trying to make use of the After setting everything up it seemed to work as expected, but after I refresh the page and get a new nonce value, I end up with the error: I've read that the loader acts as a singleton and essentially no new instances are created are created as long as you pass the same settings, which makes sense to me, but in this case the nonce value has to be different (since it is a nonce). I've seen from this issue that you could potentially reset the loader manually, which might help with getting around this, but since that would require using It feels like I'm missing something, is there a better/recommended way to handle this scenario? Many thanks in advance! Editing to include my example in the comments:
|
Replies: 12 comments 23 replies
|
@chrisdueck please provide minimal reproduction. If page re-renders, there should be completely new google maps instances, and useLoadScript should be called every time page refreshes, as completely new process. Not sure that it is related to page-refresh in your case. |
|
The problem is happening with me too, while trying on my own style, then copy pasting from the documentation and still would display the same error! |
|
Comment out or remove React.StrictMode in index.js file. {/* <React.StrictMode> */} {/* </React.StrictMode> */} |
|
In my case, the problem was happened every time I add or edit something in Reload the server, solve the issue for me so every time you change |
|
Choose your preferred solution below.
In most cases, it's enough to load scripts only once to use Google Maps API. Twice or more would mean something technically wrong in your app. Calling Sample: https://stackblitz.com/edit/cd-rgmapi-pnhl9b?file=App.tsx If you genuinely need to load API scripts multiple times with different https://github.com/googlemaps/js-api-loader/blob/main/src/index.ts#L317 |
|
in my nextjs project I use |
|
nonce itself is "number once" so it should not be changed at runtime in the browser. Nonce is set by the web server on both, header and html, and guaranties that the code running is the code provided by the server. There should not be change of nonce for the document without page reload. If you are doing SSR, nonce should change on navigation, cos it calls server with full page reload. To get new nonce, both in headers and in html you have to call a web sever and get a fresh page. |
|
can I just assign a string value that won't change at all, or it's not recommended ? const { isLoaded, loadError } = useJsApiLoader({
googleMapsApiKey: `${googlePlacesKey}`,
nonce: "map",
libraries: ["places"],
}); |
|
I faced the same problem when trying to set up I18n with the |
|
I found the same problem, this one because I store my API keys in my backend, any workaround to this one? because useJsApiLoader cannot be used inside a conditional |
|
Just in case this helps anyone, after trying various things, what worked for me was clearing the cookies and site data. Seems like some config data was getting persisted that was causing the "Loader must not be called again with different options" error. |
Choose your preferred solution below.
noncevalueuseJsApiLoaderonly once in your appuseJsApiLoader(I don't recommend this)In most cases, it's enough to load scripts only once to use Google Maps API. Twice or more would mean something technically wrong in your app.
Calling
useJsApiLoaderwith the samenoncevalue, loading scripts will be just skipped, and the same API instance will be reused, so you don't worry about security holes related to loading scripts with known nonce value. And ensure callinguseJsApiLoaderonly once in your app is a good choice too.Sample: https://stackblitz.com/edit/cd-rgmapi-pnhl9b?file=App.tsx
I…