webfont-loader icon indicating copy to clipboard operation
webfont-loader copied to clipboard

Provide a soft-fail when the response is not 200

Open erikyo opened this issue 3 years ago • 0 comments

I am using this awesome module to dynamically enqueue fonts but, since it is possible that it will fail to download some font families or weights, I would like if WPTT/webfont-loader could handle the error message and display it. Currently the server error message is queued as a style (and this creates some layout glitches, at least with my template since it's an html message) and it would probably be better to display that error at the top of body (eg. like as when React makes a compilation error)

This is the code in which the request is made: https://github.com/WPTT/webfont-loader/blob/19fa29ba8d82bed018fb8c6f949799739c61356c/wptt-webfont-loader.php#L262-L272

I think it would be enough to add something like below to solve (just after "is_wp_error").

// The response is invalid, report the error message at the top of body and avoid to enqueue the error response as style.
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
  return "body::before {
	  content: 'WPTT Error: {$response['response']['message']}';
	  display: block;
	  font-family: monospace;
	  padding: 1rem;
	  background-color: black;
	  color:white;
	  font-weight: bold;
  }";
}

An error message for testing -> https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@700;600;500&family=Merriweather:wght@500;700&family=Material+Icons&display=swap

erikyo avatar Feb 07 '23 15:02 erikyo