INN / doubleclick-for-wp

WordPress plugin for serving Google Ad Manager ads
https://wordpress.org/plugins/doubleclick-for-wp/
GNU General Public License v2.0
25 stars 11 forks source link

The not-lazy-load ad loader needs to wait until jQuery.dfp is available #95

Open benlk opened 5 years ago

benlk commented 5 years ago

Problem

This line of code:

https://github.com/INN/doubleclick-for-wp/blob/bf4fcb5423ff359af955948ba253a8e2211185a3/inc/class-doubleclick.php#L210-L212

Can potentially be output upon the page before the script enqueues that load the js:

https://github.com/INN/doubleclick-for-wp/blob/bf4fcb5423ff359af955948ba253a8e2211185a3/inc/class-doubleclick.php#L180-L181

On one site in recent memory, the normal code output on the page was thus:

<script type='text/javascript' src='https://example.org/wp-content/plugins/doubleclick-for-wp/js/vendor/jquery.dfp.js/jquery.dfp.min.js?ver=0.3.0'></script>
<script type='text/javascript'>
    /* <![CDATA[ */
        var dfw = { /* snip */};
    /* ]]> */
</script>
<script type='text/javascript' src='https://example.org/wp-content/plugins/doubleclick-for-wp/js/jquery.dfw.js?ver=0.3.0'></script>
<script type="text/javascript">
    jQuery('.dfw-unit:not(.dfw-lazy-load)').dfp( window.dfw );
</script>

but when that site loaded the https://wordpress.org/plugins/autoptimize/ plugin, the call to jQuery().dfp(); came before the concatenated scripts were loaded on the page.

Solution