Jetpack: Gutenberg: error when in the editor

Created on 31 Oct 2018  路  1Comment  路  Source: Automattic/jetpack

The following error has started to appear at times, when viewing the editor under Posts > Add New:

| <b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /srv/users/user9c07aa14/apps/user9c07aa14/public/wp-admin/includes/template.php:2053) in <b>/srv/users/user9c07aa14/apps/user9c07aa14/public/wp-content/plugins/jetpack-dev/class.jetpack.php</b> on line <b>5849</b><br />

It would appear that we first noticed the error here:
https://github.com/Automattic/jetpack/pull/10327#pullrequestreview-166924254

Gutenberg [Pri] High [Type] Bug

Most helpful comment

It looks like you stumbled upon something that has existed since https://github.com/Automattic/jetpack/pull/4012 (cc: @dereksmart ) in some ways.

What's happening is state will fire setcookies which must be run before any output has been sent to the browser by PHP.

The react page's get_initial_state function (which is new, but the functionality has existed since 4012) fires state, so it can sometimes set cookies.

For both the React page and Gutenberg, we are calling this function too late.

For the React page, we're calling it on page_admin_scripts (#) which is firing in (after a jump) via admin_print_scripts-$hook (#).

That hook fires after output has started in Core (#).

For the Gutenberg use case, similar. We are requesting the initial_state function (#) from within the enqueue_block_editor_assets hook (#), which fires in Core when loading the block editor, after the admin_header has been dropped.

tl;dr: We need to not call Jetpack::state within that initial state function if we're going to use it where we're using it (edit: or check for headers_sent within state before before attempting to setcookie)

>All comments

It looks like you stumbled upon something that has existed since https://github.com/Automattic/jetpack/pull/4012 (cc: @dereksmart ) in some ways.

What's happening is state will fire setcookies which must be run before any output has been sent to the browser by PHP.

The react page's get_initial_state function (which is new, but the functionality has existed since 4012) fires state, so it can sometimes set cookies.

For both the React page and Gutenberg, we are calling this function too late.

For the React page, we're calling it on page_admin_scripts (#) which is firing in (after a jump) via admin_print_scripts-$hook (#).

That hook fires after output has started in Core (#).

For the Gutenberg use case, similar. We are requesting the initial_state function (#) from within the enqueue_block_editor_assets hook (#), which fires in Core when loading the block editor, after the admin_header has been dropped.

tl;dr: We need to not call Jetpack::state within that initial state function if we're going to use it where we're using it (edit: or check for headers_sent within state before before attempting to setcookie)

Was this page helpful?
0 / 5 - 0 ratings