(Plugin builder) Blank API key in header makes script not load

I’m loading the Google maps javascript API in the header of my plugin. This API takes a key value, but it can be left blank and will still work (with a warning in the console), which is nice for testing before the user gets an API key.

If I put this in my shared header:

<script src="https://maps.googleapis.com/maps/api/js?v=3&key="></script>

This works fine. However, if I include a key parameter, eg:

<script src="https://maps.googleapis.com/maps/api/js?v=3&key=_*_APIKey_*_"></script>

Then if I leave the APIKey parameter blank in my project, the script doesn’t load. It works fine if I fill in APIKey with a key.

Any ideas how to get around this? I’d like to keep the option of leaving APIKey blank.

I’d raise that as a bug on the plugin builder.

One workaround is for the app developer to put in a space, not great though, and only works if its the last query parameter.

1 Like

Ok, I’ll put a bug report in. A space does work, would be nice if I could set that as a default in the plugin builder

You could also put it in the the element or actions code and pick it up through the context object and define the secret in shared tab. Then you can test for length and append either or.

I did try that at first, but it wasn’t render blocking, and it got me thinking about things like asynchronously loading scripts, and I got worried about every time I’ve put a script file in the header, and it scared me so I buried my head in the sand and forgot about it. I should probably do it though…

Just chuck an onLoad callback in the script reference and have your function triggered by that. As for the each element sitcho, just test for it in the header on init. Down to preference I think, just a good amount of opportunity this way.

1 Like

This topic was automatically closed after 14 days. New replies are no longer allowed.