Skip to content

Commit 2cc90df

Browse files
authored
feat(gatsby-source-contentful): update docs and improve errors (#30538)
* docs(contentful): add known limitations to the readme * fix(contentful): tell users of environments that access tokens need to be able to access master env fixes #26909
1 parent 418c3bc commit 2cc90df

File tree

3 files changed

+77
-11
lines changed

3 files changed

+77
-11
lines changed

packages/gatsby-source-contentful/README.md

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ npm install gatsby-source-contentful
1717

1818
First, you need a way to pass environment variables to the build process, so secrets and other secured data aren't committed to source control. We recommend using [`dotenv`][dotenv] which will then expose environment variables. [Read more about `dotenv` and using environment variables here][envvars]. Then we can _use_ these environment variables and configure our plugin.
1919

20+
## Restrictions and limitations
21+
22+
This plugin has several limitations, please be aware of these:
23+
24+
1. At the moment, fields that do not have at least one populated instance will not be created in the GraphQL schema. This can break your site when field values get removed. You may workaround with an extra content entry with all fields filled out.
25+
26+
2. When using reference fields, be aware that this source plugin will automatically create the reverse reference. You do not need to create references on both content types.
27+
28+
3. When working with environments, your access token has to have access to your desired enviornment and the `master` environment.
29+
30+
4. Using the preview functionallity might result in broken content over time, as syncing data on preview is not officially supported by Contentful. Make sure to regulary clean your cache when using Contentfuls preview API.
31+
2032
### Using Delivery API
2133

2234
```javascript
@@ -120,14 +132,6 @@ Additional config which will get passed to [Contentfuls JS SDK](https://github.c
120132

121133
Use this with caution, you might override values this plugin does set for you to connect to Contentful.
122134

123-
## Notes on Contentful Content Models
124-
125-
There are currently some things to keep in mind when building your content models at Contentful.
126-
127-
1. At the moment, fields that do not have at least one populated instance will not be created in the GraphQL schema.
128-
129-
2. When using reference fields, be aware that this source plugin will automatically create the reverse reference. You do not need to create references on both content types.
130-
131135
## How to query for nodes
132136

133137
Two standard node types are available from Contentful: `Asset` and `ContentType`.

packages/gatsby-source-contentful/src/__tests__/fetch.js

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,13 @@ describe(`Displays troubleshooting tips and detailed plugin options on contentfu
258258
err.responseData = { status: 404 }
259259
throw err
260260
})
261+
const masterOptions = { ...options, environment: `master` }
262+
const masterConfig = createPluginConfig(masterOptions)
261263

262-
await fetchData({ pluginConfig, reporter })
264+
await fetchData({
265+
pluginConfig: masterConfig,
266+
reporter,
267+
})
263268

264269
expect(reporter.panic).toBeCalledWith(
265270
expect.objectContaining({
@@ -283,7 +288,7 @@ describe(`Displays troubleshooting tips and detailed plugin options on contentfu
283288

284289
expect(formatPluginOptionsForCLI).toBeCalledWith(
285290
expect.objectContaining({
286-
...options,
291+
...masterOptions,
287292
}),
288293
{
289294
host: `Check if setting is correct`,
@@ -292,6 +297,46 @@ describe(`Displays troubleshooting tips and detailed plugin options on contentfu
292297
)
293298
})
294299

300+
it(`API 404 response handling with environment set`, async () => {
301+
mockClient.getLocales.mockImplementation(() => {
302+
const err = new Error(`error`)
303+
err.responseData = { status: 404 }
304+
throw err
305+
})
306+
307+
await fetchData({ pluginConfig, reporter })
308+
309+
expect(reporter.panic).toBeCalledWith(
310+
expect.objectContaining({
311+
context: {
312+
sourceMessage: expect.stringContaining(
313+
`Unable to access your space. Check if environment is correct and your accessToken has access to the env and the master environments.`
314+
),
315+
},
316+
})
317+
)
318+
319+
expect(reporter.panic).toBeCalledWith(
320+
expect.objectContaining({
321+
context: {
322+
sourceMessage: expect.stringContaining(
323+
`formatPluginOptionsForCLIMock`
324+
),
325+
},
326+
})
327+
)
328+
329+
expect(formatPluginOptionsForCLI).toBeCalledWith(
330+
expect.objectContaining({
331+
...options,
332+
}),
333+
{
334+
accessToken: `Check if setting is correct`,
335+
environment: `Check if setting is correct`,
336+
}
337+
)
338+
})
339+
295340
it(`API authorization error handling`, async () => {
296341
mockClient.getLocales.mockImplementation(() => {
297342
const err = new Error(`error`)

packages/gatsby-source-contentful/src/fetch.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,24 @@ module.exports = async function contentfulFetch({
160160
},
161161
})
162162
} else if (e.responseData) {
163-
if (e.responseData.status === 404) {
163+
if (
164+
e.responseData.status === 404 &&
165+
contentfulClientOptions.environment &&
166+
contentfulClientOptions.environment !== `master`
167+
) {
168+
// environments need to have access to master
169+
details = `Unable to access your space. Check if ${chalk.yellow(
170+
`environment`
171+
)} is correct and your ${chalk.yellow(
172+
`accessToken`
173+
)} has access to the ${chalk.yellow(
174+
contentfulClientOptions.environment
175+
)} and the ${chalk.yellow(`master`)} environments.`
176+
errors = {
177+
accessToken: `Check if setting is correct`,
178+
environment: `Check if setting is correct`,
179+
}
180+
} else if (e.responseData.status === 404) {
164181
// host and space used to generate url
165182
details = `Endpoint not found. Check if ${chalk.yellow(
166183
`host`

0 commit comments

Comments
 (0)