Change record status: 
Introduced in branch: 
7.x-2.x
Introduced in version: 
7.x-2.2
Description: 

All callbacks are now are wrapped, by default, with output buffer control.

During the work on the related issue (intentionally creating errors, including debugging via printing output), it became apparent that sometimes a callback may either intentionally or unintentionally print content to the browser instead of returning its content from the callback.

Unfortunately, because this content gets printed after js_deliver_json() encoded and gzipped the JSON data, browsers will fail in decoding the returned gzipped content because it was a mixture of both gzipped data and raw printed data from prior executed code.

Most callbacks shouldn't be affected by this, however if you notice that your callbacks are no longer working properly, you can easily mitigate this change by adding the following property to your module's hook_js_info() callback definition:

$callback['my_callback'] = array(
  // Disable output buffer capture.
  'capture' => FALSE,
);

Optionally, you can leave this enabled and instead invoke the following newly created alter hook to add it to your callback's return result:

/**
 * Implements hook_js_captured_content_alter().
 */
function MY_MODULE_js_captured_content_alter(&$result, $captured) {
  // Pass the captured output to the JSON array right before delivery.
  if (js_delivery_callback() === 'js_deliver_json') {
    $result['captured'] = filter_xss_admin($captured);
  }
}

Note: This alter hook is only invoked by loaded modules. If this alter hook is located in a module other than your own, you will need to add that module to the list of your callback's dependencies.

Impacts: 
Module developers