-
Notifications
You must be signed in to change notification settings - Fork 10.3k
feat(gatsby-source-contentful): Increase Contentful sync by up to 10x #30422
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a0b3f63
perf(contentful): increase default pageLimit to 1000 and implement ba…
axe312ger 0922325
style: use String.includes instead of indexOf
axe312ger 85ed360
docs: add comment about page-limit backoff logic
axe312ger 4fc1237
fix(contentful): merge with master and improve network error detection
axe312ger 8f8651f
revert some accidential changes to the tests
axe312ger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
140 changes: 140 additions & 0 deletions
140
packages/gatsby-source-contentful/src/__tests__/fetch-backoff.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| /** | ||
| * @jest-environment node | ||
| */ | ||
|
|
||
| import nock from "nock" | ||
| import fetchData from "../fetch" | ||
| import { createPluginConfig } from "../plugin-options" | ||
|
|
||
| const host = `localhost` | ||
| const options = { | ||
| spaceId: `12345`, | ||
| accessToken: `67890`, | ||
| host, | ||
| contentfulClientConfig: { | ||
| retryLimit: 2, | ||
| }, | ||
| } | ||
|
|
||
| const baseURI = `https://${host}` | ||
|
|
||
| const start = jest.fn() | ||
| const end = jest.fn() | ||
| const mockActivity = { | ||
| start, | ||
| end, | ||
| tick: jest.fn(), | ||
| done: end, | ||
| } | ||
|
|
||
| const reporter = { | ||
| info: jest.fn(), | ||
| verbose: jest.fn(), | ||
| warn: jest.fn(), | ||
| panic: jest.fn(e => { | ||
| throw e | ||
| }), | ||
| activityTimer: jest.fn(() => mockActivity), | ||
| createProgress: jest.fn(() => mockActivity), | ||
| } | ||
|
|
||
| const pluginConfig = createPluginConfig(options) | ||
|
|
||
| describe(`fetch-backoff`, () => { | ||
| afterEach(() => { | ||
| nock.cleanAll() | ||
| reporter.verbose.mockClear() | ||
| reporter.panic.mockClear() | ||
| reporter.warn.mockClear() | ||
| }) | ||
|
|
||
| test(`backoffs page limit when limit is reached`, async () => { | ||
| jest.setTimeout(30000) | ||
| const scope = nock(baseURI) | ||
| // Space | ||
| .get(`/spaces/${options.spaceId}/`) | ||
| .reply(200, { items: [] }) | ||
| // Locales | ||
| .get(`/spaces/${options.spaceId}/environments/master/locales`) | ||
| .reply(200, { items: [{ code: `en`, default: true }] }) | ||
| // Sync with 1000 (to much) | ||
| .get( | ||
| `/spaces/${options.spaceId}/environments/master/sync?initial=true&limit=1000` | ||
| ) | ||
| .times(1) | ||
| .reply(400, { | ||
| sys: { type: `Error`, id: `BadRequest` }, | ||
| message: `Response size too big. Maximum allowed response size: 512000B.`, | ||
| requestId: `12345`, | ||
| }) | ||
| // Sync with 666 (still to much) | ||
| .get( | ||
| `/spaces/${options.spaceId}/environments/master/sync?initial=true&limit=666` | ||
| ) | ||
| .times(1) | ||
| .reply(400, { | ||
| sys: { type: `Error`, id: `BadRequest` }, | ||
| message: `Response size too big. Maximum allowed response size: 512000B.`, | ||
| requestId: `12345`, | ||
| }) | ||
| // Sync with 444 | ||
| .get( | ||
| `/spaces/${options.spaceId}/environments/master/sync?initial=true&limit=444` | ||
| ) | ||
| .reply(200, { items: [] }) | ||
| // Content types | ||
| .get( | ||
| `/spaces/${options.spaceId}/environments/master/content_types?skip=0&limit=1000&order=sys.createdAt` | ||
| ) | ||
| .reply(200, { items: [] }) | ||
|
|
||
| await fetchData({ pluginConfig, reporter }) | ||
|
|
||
| expect(reporter.panic).not.toBeCalled() | ||
| expect(reporter.warn.mock.calls).toMatchInlineSnapshot(` | ||
| Array [ | ||
| Array [ | ||
| "The sync with Contentful failed using pageLimit 1000 as the reponse size limit of the API is exceeded. | ||
|
|
||
| Retrying sync with pageLimit of 666", | ||
| ], | ||
| Array [ | ||
| "The sync with Contentful failed using pageLimit 666 as the reponse size limit of the API is exceeded. | ||
|
|
||
| Retrying sync with pageLimit of 444", | ||
| ], | ||
| Array [ | ||
| "We recommend you to set your pageLimit in gatsby-config.js to 444 to avoid failed synchronizations.", | ||
| ], | ||
| ] | ||
| `) | ||
| expect(scope.isDone()).toBeTruthy() | ||
| }) | ||
|
|
||
| test(`does not backoff page limit when limit is not reached`, async () => { | ||
| jest.setTimeout(30000) | ||
| const scope = nock(baseURI) | ||
| // Space | ||
| .get(`/spaces/${options.spaceId}/`) | ||
| .reply(200, { items: [] }) | ||
| // Locales | ||
| .get(`/spaces/${options.spaceId}/environments/master/locales`) | ||
| .reply(200, { items: [{ code: `en`, default: true }] }) | ||
| // Sync with 1000 (no limit exceeded) | ||
| .get( | ||
| `/spaces/${options.spaceId}/environments/master/sync?initial=true&limit=1000` | ||
| ) | ||
| .reply(200, { items: [] }) | ||
| // Content types | ||
| .get( | ||
| `/spaces/${options.spaceId}/environments/master/content_types?skip=0&limit=1000&order=sys.createdAt` | ||
| ) | ||
| .reply(200, { items: [] }) | ||
|
|
||
| await fetchData({ pluginConfig, reporter }) | ||
|
|
||
| expect(reporter.panic).not.toBeCalled() | ||
| expect(reporter.warn).not.toBeCalled() | ||
| expect(scope.isDone()).toBeTruthy() | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.