podlove-subscribe-button icon indicating copy to clipboard operation
podlove-subscribe-button copied to clipboard

CDN with local fallback

Open eteubert opened this issue 8 years ago • 6 comments

I want to use the CDN but fall back to a local button if the CDN is not reachable. This is what I came up with:

<script>window.podcastData167f2eb064c10c = {/* ... */};</script>
<script class="podlove-subscribe-button" src="https://cdn.podlove.org/subscribe-button/javascripts/app.js" data-json-data="podcastData167f2eb064c10c" data-language="de" data-size="big auto" data-format="cover" data-style="filled" data-color="#75ad91"> </script>
<script>if (typeof SubscribeButton == 'undefined') {
    document.write(unescape("%3Cscript class=\"podlove-subscribe-button\" src=\"https://publisher.dev/wp-content/plugins/podlove-publisher/lib/modules/subscribe_button/dist/javascripts/app.js\" data-json-data=\"podcastData167f2eb064c10c\" data-language=\"de\" data-size=\"big auto\" data-format=\"cover\" data-style=\"filled\" data-color=\"#75ad91\"> %3E%3C/script%3E"));
}</script>

It kind of works. This is the result:

screen shot 2018-03-09 at 09 42 14

The fallback button is rendered correctly but the failed CDN script still creates the iframe and style nodes which take up vertical space. Ideas how I can make this work?

eteubert avatar Mar 09 '18 08:03 eteubert

Hacky solution / workaround:


if (typeof SubscribeButton == 'undefined') {

    document.write(unescape("%3Cscript class=\"podlove-subscribe-button\" src=\"https://publisher.dev/wp-content/plugins/podlove-publisher/lib/modules/subscribe_button/dist/javascripts/app.js\" data-json-data=\"podcastDataa209f41320cdd9\" data-language=\"de\" data-size=\"big auto\" data-format=\"cover\" data-style=\"filled\" data-color=\"#75ad91\"> %3E%3C/script%3E"));

    window.setTimeout(function() {
        iframes = document.querySelectorAll('.podlove-subscribe-button-iframe')
        for (i = 0; i < iframes.length; ++i) {
            if (!iframes[i].style.width && !iframes[i].style.height) {
                iframes[i].style.display = 'none';
            }
        }
    }, 5000);

}

Explanation: 5 seconds after loading the fallback, I check all button iframes if they have a width and style css set. If not, that means they were not initialized and I can hide them. It's not a great solution but better than showing no button at all when the CDN is not available.

Curious if you have ideas to improve on this.

eteubert avatar Mar 09 '18 09:03 eteubert

This would be a great improvement. I would love to add it to the wp plugin when a working solution is available

cfoellmann avatar Oct 03 '18 18:10 cfoellmann

This approach is already used with success in the Publisher https://github.com/podlove/podlove-publisher/blob/master/lib/modules/subscribe_button/button.php#L111

eteubert avatar Oct 04 '18 07:10 eteubert

perfect! I will add this to the next version of the standalone plugin. Close?

cfoellmann avatar Oct 04 '18 12:10 cfoellmann

Great :) Close when it's committed.

eteubert avatar Oct 04 '18 12:10 eteubert

Would it be possible to give us a complete option to run the app locally? Right now, using the plugin always uses the CDN and this apparently can't be disabled. It would be great for those of us who don't want to run any external code on our site.

Natanji avatar Apr 05 '20 20:04 Natanji