Skip to content

Prevent loading the API library until the element is attached #328

@platosha

Description

@platosha

Currently, the google-map element loads the API library just after the element is created. This creates issues if a temporary instance is created, which is not going to be attached.

Consider the following case:

/* First, create a temporary instance, for example,
to extract a prototype for some analysis purpose */
var proto = Object.getPrototypeOf(document.createElement('google-map'));

/* Later, another instance of map is created and used normally */
var mapElement = document.createElement('google-map');
mapElement.apiKey = '...';
document.body.appendChild(mapElement);

Expected outcome: the API library is loaded once only after the element is attached.

Actual outcome: the API library is loaded multiple times.

Solution idea: postpone loading the API library after the element is attached by wrapping the <google-maps-api> tag with <template is="dom-if" if="isAttached">...</template>

Note that debouncing the API url computation, as proposed in GoogleWebComponents/google-apis#77, is not a complete solution for this issue. For the use case above, it will make the second instance to load the API library with the key provided synchronously after the element creation, while the first instance still loads the API without a key.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions