Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
Only print wp.apiFetch.createPreloadingMiddleware when needed (#8647)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aljullu authored Mar 8, 2023
1 parent d930e77 commit bbb29ea
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/Assets/AssetDataRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,15 +368,18 @@ public function enqueue_asset_data() {
$this->initialize_core_data();
$this->execute_lazy_data();

$data = rawurlencode( wp_json_encode( $this->data ) );
$preloaded_api_requests = rawurlencode( wp_json_encode( $this->preloaded_api_requests ) );
$data = rawurlencode( wp_json_encode( $this->data ) );
$wc_settings_script = "var wcSettings = wcSettings || JSON.parse( decodeURIComponent( '" . esc_js( $data ) . "' ) );";
$preloaded_api_requests_script = '';

if ( count( $this->preloaded_api_requests ) > 0 ) {
$preloaded_api_requests = rawurlencode( wp_json_encode( $this->preloaded_api_requests ) );
$preloaded_api_requests_script = "wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( JSON.parse( decodeURIComponent( '" . esc_js( $preloaded_api_requests ) . "' ) ) ) );";
}

wp_add_inline_script(
$this->handle,
"
var wcSettings = wcSettings || JSON.parse( decodeURIComponent( '" . esc_js( $data ) . "' ) );
wp.apiFetch.use( wp.apiFetch.createPreloadingMiddleware( JSON.parse( decodeURIComponent( '" . esc_js( $preloaded_api_requests ) . "' ) ) ) )
",
$wc_settings_script . $preloaded_api_requests_script,
'before'
);
}
Expand Down

0 comments on commit bbb29ea

Please sign in to comment.