Skip to content

Commit

Permalink
Prevent an error in <ServerSideRender> by allowing POST request (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kienstra committed Aug 15, 2020
1 parent 48c4fb9 commit 068064a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/server-side-render/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

### Feature
- Add an optional prop `httpMethod`, which can be 'POST' or the default 'GET'. Requires WP 5.5 or later. When 'POST', this sends the attributes in the request body, not in the URL. This can allow a bigger attributes object. [#21068](https://github.com/WordPress/gutenberg/pull/21068)

## 1.7.0 (2020-02-04)

### Bug
Expand Down
6 changes: 6 additions & 0 deletions packages/server-side-render/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ Examples: "my-custom-server-side-rendered".
- Type: `String`
- Required: No

### httpMethod

The HTTP request method to use, either 'GET' or 'POST'. It's 'GET' by default. The 'POST' value will cause an error on WP earlier than 5.5, unless 'rest_endpoints' is filtered in PHP to allow this. If 'POST', this sends the attributes in the request body, not in the URL. This can allow a bigger attributes object.
- Type: `String`
- Required: No

### urlQueryArgs

Query arguments to apply to the request URL.
Expand Down
21 changes: 18 additions & 3 deletions packages/server-side-render/src/server-side-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,27 @@ export class ServerSideRender extends Component {
if ( null !== this.state.response ) {
this.setState( { response: null } );
}
const { block, attributes = null, urlQueryArgs = {} } = props;
const {
block,
attributes = null,
httpMethod = 'GET',
urlQueryArgs = {},
} = props;

// If httpMethod is 'POST', send the attributes in the request body instead of the URL.
// This allows sending a larger attributes object than in a GET request, where the attributes are in the URL.
const isPostRequest = 'POST' === httpMethod;
const urlAttributes = isPostRequest ? null : attributes;
const path = rendererPath( block, urlAttributes, urlQueryArgs );
const data = isPostRequest ? { attributes } : null;

const path = rendererPath( block, attributes, urlQueryArgs );
// Store the latest fetch request so that when we process it, we can
// check if it is the current request, to avoid race conditions on slow networks.
const fetchRequest = ( this.currentFetchRequest = apiFetch( { path } )
const fetchRequest = ( this.currentFetchRequest = apiFetch( {
path,
data,
method: isPostRequest ? 'POST' : 'GET',
} )
.then( ( response ) => {
if (
this.isStillMounted &&
Expand Down

0 comments on commit 068064a

Please sign in to comment.