See discussion below where we decided we should drop the fallback script, especially since the plugin now supports the <picture> element which provides smoother fallback experience, and because WebP is so widely supported.
Old description for "Script handling in WebP Uploads can be improved"
Currently an inline script is printed to the page which conditionally loads a fallback.js script:
|
wp_print_inline_script_tag( |
|
preg_replace( '/\s+/', '', $javascript ), |
|
array( |
|
'id' => 'webpUploadsFallbackWebpImages', |
|
'data-rest-api' => esc_url_raw( trailingslashit( get_rest_url() ) ), |
|
) |
|
); |
Note how it adds a data-rest-api data attribute here. This is then read in fallback.js via:
|
var restApi = document |
|
.getElementById( 'webpUploadsFallbackWebpImages' ) |
|
.getAttribute( 'data-rest-api' ); |
This value is then used to construct the script URL to load:
|
var jsonp = document.createElement( 'script' ); |
|
var restPath = |
|
'wp/v2/media/?_fields=id,media_details&_jsonp=wpPerfLab.webpUploadsFallbackWebpImages&per_page=100&include=' + |
|
pageIds.join( ',' ); |
|
if ( -1 !== restApi.indexOf( '?' ) ) { |
|
restPath = restPath.replace( '?', '&' ); |
|
} |
|
jsonp.src = restApi + restPath; |
|
document.body.appendChild( jsonp ); |
A couple things here:
- Wouldn't it be better to rely on
fetch() than to use JSONP?
- Instead of exporting the URL in an HTML attribute, why not expose it via a global variable in the same inline script that is being printed to begin with?
- The query args for
restPath would be better constructed with URLSearchParams, or if IE11 compat is really desired, fall back to using document.createElement('a').href.
Also, in that inline script it would be slightly safer if this line used wp_json_encode():
|
s.src = '<?php echo esc_url_raw( plugins_url( '/fallback.js', __FILE__ ) ); ?>'; |
Like:
s.src = <?php echo wp_json_encode( esc_url_raw( plugins_url( '/fallback.js', __FILE__ ) ) ); ?>;
Lastly, instead of relying on data attributes on the script tag, the necessary data may make more sense to export into JS via wp_add_inline_script_tag() or else a JSON script. The reason is that the script tag for JS may end up getting concatenated with other scripts, resulting in those attributes getting lost.
See discussion below where we decided we should drop the fallback script, especially since the plugin now supports the
<picture>element which provides smoother fallback experience, and because WebP is so widely supported.Old description for "Script handling in WebP Uploads can be improved"
Currently an inline script is printed to the page which conditionally loads a
fallback.jsscript:performance/modules/images/webp-uploads/hooks.php
Lines 699 to 705 in 11be325
Note how it adds a
data-rest-apidata attribute here. This is then read infallback.jsvia:performance/modules/images/webp-uploads/fallback.js
Lines 91 to 93 in 11be325
This value is then used to construct the
scriptURL to load:performance/modules/images/webp-uploads/fallback.js
Lines 129 to 137 in 11be325
A couple things here:
fetch()than to use JSONP?restPathwould be better constructed withURLSearchParams, or if IE11 compat is really desired, fall back to usingdocument.createElement('a').href.Also, in that inline script it would be slightly safer if this line used
wp_json_encode():performance/modules/images/webp-uploads/hooks.php
Line 683 in 11be325
Like:
Lastly, instead of relying on data attributes on the script tag, the necessary data may make more sense to export into JS via
wp_add_inline_script_tag()or else a JSONscript. The reason is that thescripttag for JS may end up getting concatenated with other scripts, resulting in those attributes getting lost.