contentful.js icon indicating copy to clipboard operation
contentful.js copied to clipboard

sync limit param seems not work

Open Gorden-Wang opened this issue 5 years ago • 4 comments

Expected Behavior

When I use sync API and set the limit param to 10, I expected the response results length will be 10, And it takes a long time to wait for the response.

 const syncRes = await client.sync({
      initial: true,
      limit: 10,
    });

Environment

  • Language Version: v12.15.0
  • Package Manager Version: 6.13.4
  • Operating System: Mac os
  • Package Version: 7.14.3
  • Which API are you using?: Delivery

Gorden-Wang avatar Apr 21 '20 12:04 Gorden-Wang

I finally find the solution with HTTP API

...
this.requestPrefix = `http://${this.options.host}/spaces/${this.options.space}/environments/${this.options.environment}/`;
...
sync(query: ContentSyncParam) {
    const { type, limit, initial, nextPageUrl } = query;
    let requestUrl = '';
    if (initial) {
      // make a request params string
      const query = querystring.stringify({ access_token: this.options.accessToken, type, limit, initial });
      requestUrl = `${this.requestPrefix}/sync?${query}`;
    } else {
      assert(nextPageUrl, 'nextPageUrl must not be empty');
      requestUrl = `${nextPageUrl}&access_token=${this.options.accessToken}` as string;
    }

    return axios.get(requestUrl);
  }

Gorden-Wang avatar Apr 26 '20 03:04 Gorden-Wang

I also have this problem using the latest version of API, Please fix it.

shedinnmarble avatar Feb 12 '21 01:02 shedinnmarble

I am also experiencing this issue.

This does not match the behavior specified in https://www.contentful.com/developers/docs/javascript/tutorials/using-the-sync-api-with-js/

igierard avatar Jan 04 '22 22:01 igierard

After digging into the code more it seems that this is a case where the docs are not complete. The library is, in fact, limiting each request to the limit count. However, it is automatically performing multiple requests to fetch the next page behind the scenes for you. You can prevent this behavior by passing a second config object to the sync command with the paginate key set to false.

client.sync({initial: true,limit:10},{paginate: false})

igierard avatar Jan 04 '22 23:01 igierard

Hey @Gorden-Wang 👋 , as I understand this is now solved. Closing the issue for now - feel free to re-open the issue with any additional details.

marcolink avatar Jun 08 '23 10:06 marcolink