mint-metrics / mojito-website

Static website for Mojito
https://mojito.mx/
BSD 3-Clause "New" or "Revised" License
6 stars 7 forks source link

Installation Method update #117

Closed kingo55 closed 10 months ago

kingo55 commented 11 months ago

@dapperdrop @allmywant - related to https://github.com/mint-metrics/mojito-js-delivery/pull/111 - When installing the snippet, we often recommend this approach to clients:

<script type="text/javascript" src="//hostname.com/path-to/mojito.js"></script>

Via: https://mojito.mx/docs/js-delivery-hosting-snippet

Obviously we can't easily fix containers already implemented on clients' sites with this method, but going forwards, should we consider updating the snippet installation? I.e. Check before loading it into the page.

For SPAs this might prevent it from loading the JS file twice, right?

allmywant commented 11 months ago

@kingo55 To do this, we need the following snippet:

<script type="text/javascript">
    if (!window.Mojito || !Mojito.testObjects || !Object.keys(Mojito.testObjects).length) {
        var po = document.createElement('script');
        po.type = 'text/javascript';
        po.async = false;

       var stags = document.getElementsByTagName('script');
       var s = document.getElementsByTagName('script')[stags.length -1];
       s.parentNode.insertBefore(po, s);
       po.src = '//hostname.com/path-to/mojito.js';
    }
</script>

It prevents from loading the JS file twice.

kingo55 commented 11 months ago

Thanks @allmywant - I might update these docs to reflect that then.

What does || !Mojito.testObjects || !Object.keys(Mojito.testObjects).length do in the check here and in the one in the library?

allmywant commented 11 months ago

@kingo55 || !Mojito.testObjects || !Object.keys(Mojito.testObjects).length it checks if Mojito has test objects loaded. For example: If we call Mojito.addTest({ test object }) in the container, then the check returns false. In other word, it checks if the container is empty (without test objects loaded).

kingo55 commented 11 months ago

Cheers @allmywant - that makes sense.