Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server side render encodes some attributes incorrectly #7086

Closed
timothyjensen opened this issue Jun 1, 2018 · 6 comments · Fixed by #7339
Closed

Server side render encodes some attributes incorrectly #7086

timothyjensen opened this issue Jun 1, 2018 · 6 comments · Fixed by #7339
Labels
[Package] Server Side Render /packages/server-side-render [Type] Bug An existing feature does not function as intended

Comments

@timothyjensen
Copy link

I'm running into a situation where my attributes are being returned in an unexpected format. For example, an array of Post IDs is converted to an array that contains one comma separated string.

Expected:

$postIds = [
   1,2,3
];

Observed:

$postIds = [
   '1,2,3'
];

Another example is an array of objects is converted to [object Object],[object Object]. Both cases are problematic because the data are inconsistent between the post editor and front end. Having to account for both formats increases the chances of buggy code.

I have narrowed the problem down to getQueryUrlFromObject, though I can't offer a solution.

getQueryUrlFromObject( obj, prefix ) {
return map( obj, ( paramValue, paramName ) => {
const key = prefix ? prefix + '[' + paramName + ']' : paramName;
return isPlainObject( paramValue ) ? this.getQueryUrlFromObject( paramValue, key ) :
encodeURIComponent( key ) + '=' + encodeURIComponent( paramValue );
} ).join( '&' );
}

Additional context

  • Gutenberg v2.9.2
@chrisvanpatten
Copy link
Contributor

Additional additional context: this is happening when passing attributes with array values to the <ServerSideRender /> component. Just seems like the process of URL-encoding the attributes for the API request might need to be refined.

@brianpiccione
Copy link

Whoa I've been tearing my hair out all day trying to get this to work! Thought it was just me. Would it be possible to change the call to the server change to a POST?

@danielbachhuber danielbachhuber added the [Type] Bug An existing feature does not function as intended label Jun 6, 2018
@danielbachhuber danielbachhuber added this to the WordPress 5.0 milestone Jun 6, 2018
@brianpiccione
Copy link

I've been working through this for a while and found that @miina was registering the attributes in the register_block_type php function see here:

register_block_type( 'core/archives', array(
	'attributes'      => array(
		'showPostCounts'    => array(
			'type'    => 'boolean',
			'default' => false,
		),
		'displayAsDropdown' => array(
			'type'    => 'boolean',
			'default' => false,
		),
		'align'             => array(
			'type'    => 'string',
			'default' => 'none',
		),
	),
	'render_callback' => 'render_block_core_archives',
) );

After adding this in my php file (and also making sure the attributes were in the block's js file) booleans worked fine. I haven't tried an array @timothyjensen but hopefully this helps you make some progress!

@chrisvanpatten
Copy link
Contributor

@brianpiccione I think normal types, like boolean and string, are okay. It only becomes an issue when trying to register array or object type attributes; they aren't always urlencoded correctly.

@brianpiccione
Copy link

Gotcha, thanks Chris. It'd be good to add this requirement (registering the atts in PHP) to the docs. I went down a number of rabbit holes to find this! Let me know if I can help at all

@chrisvanpatten
Copy link
Contributor

One idea for this would be to use a JS equivalent of PHP's http_build_query, which is capable of transforming complex arrays/objects into a query string.

Such as: https://github.com/vladzadvorny/http-build-query

@danielbachhuber danielbachhuber added the [Package] Server Side Render /packages/server-side-render label Jun 15, 2018
danielbachhuber pushed a commit that referenced this issue Jun 19, 2018
* Improve serialising JSON to PHP-compatible query strings (fixes #7086)

* Fix an eslint issue (unused `map` method)

* Update http-build-query to 0.7.0

* ServerSideRender: remove an over-scoped function and a default value for attributes

* ServerSideRender: cleanup unused var (oups, linting..)

* ServerSideRender: tests for `http-build-query`

* allow null default for attributes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Server Side Render /packages/server-side-render [Type] Bug An existing feature does not function as intended
Projects
None yet
4 participants