Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 80 additions & 52 deletions fetch.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gatsby-source-contentstack",
"version": "5.3.2",
"version": "5.3.3",
"description": "Gatsby source plugin for building websites using Contentstack as a data source",
"scripts": {
"prepublish": "npm run build",
Expand Down
34 changes: 27 additions & 7 deletions src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ const getData = async (url, options) => {
});
};

const fetchCsData = async (url, config, query) => {
const fetchCsData = async (url, config, query, SyncRetryCount = 0) => {
query = query || {};
query.include_count = true;
query.environment = config.environment;
Expand Down Expand Up @@ -296,12 +296,32 @@ const getSyncData = async (
* To make final sync call and concatenate the result if found any during on fetch request.
*/
const aggregatedSyncToken = syncToken.filter(item => item !== undefined);
for (const token of aggregatedSyncToken) {
const syncResponse = await fetchCsData(
url,
config,
(query = { sync_token: token })
);
for (const token of aggregatedSyncToken) {

let syncResponse;
try {

syncResponse = await fetchCsData(
url,
config,
(query = { sync_token: token }),
0 // Reset SyncRetryCount for each call
);
} catch (error) {
if (SyncRetryCount < config.httpRetries) {
const timeToWait = 2 ** SyncRetryCount * 100;
//Retry attempt ${retries + 1} after sync token error. Waiting for ${timeToWait} ms...
await waitFor(timeToWait);
return syncResponse = await fetchCsData(
url,
config,
(query = { sync_token: token }),
SyncRetryCount + 1
);
} else {
throw new Error(`Failed to fetch sync data after ${config.httpRetries} retry attempts due to invalid sync token.`);
}
}
aggregatedResponse.data = aggregatedResponse.data?.concat(
...syncResponse.items
);
Expand Down
Loading