Skip to content
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

chore(gatsyb-source-contentful): move locale filter #28553

Merged
merged 3 commits into from
Dec 10, 2020
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
20 changes: 0 additions & 20 deletions packages/gatsby-source-contentful/src/__tests__/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,26 +181,6 @@ it(`calls contentful.getContentTypes with custom plugin option page limit`, asyn
})
})

it(`panics when localeFilter reduces locale list to 0`, async () => {
await fetchData({
pluginConfig: createPluginConfig({
accessToken: `6f35edf0db39085e9b9c19bd92943e4519c77e72c852d961968665f1324bfc94`,
spaceId: `rocybtov1ozk`,
pageLimit: 50,
localeFilter: () => false,
}),
reporter,
})

expect(reporter.panic).toBeCalledWith(
expect.objectContaining({
context: {
sourceMessage: `Please check if your localeFilter is configured properly. Locales 'en-us' were found but were filtered down to none.`,
},
})
)
})

describe(`Displays troubleshooting tips and detailed plugin options on contentful client error`, () => {
it(`Generic fallback error`, async () => {
mockClient.getLocales.mockImplementation(() => {
Expand Down
40 changes: 40 additions & 0 deletions packages/gatsby-source-contentful/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -702,4 +702,44 @@ describe(`gatsby-node`, () => {
expect(homeNode.content.references___NODE).toMatchSnapshot()
})
})

it(`panics when localeFilter reduces locale list to 0`, async () => {
cache.get.mockClear()
cache.set.mockClear()
fetch.mockImplementationOnce(startersBlogFixture.initialSync)
const locales = [`en-US`, `nl`]

const mockPanicReporter = {
...reporter,
panic: jest.fn(),
}

await gatsbyNode.sourceNodes(
{
actions,
store,
getNodes,
getNode,
reporter: mockPanicReporter,
createNodeId,
cache,
getCache,
schema,
},
{
...pluginOptions,
localeFilter: () => false,
}
)

expect(mockPanicReporter.panic).toBeCalledWith(
expect.objectContaining({
context: {
sourceMessage: `Please check if your localeFilter is configured properly. Locales '${locales.join(
`,`
)}' were found but were filtered down to none.`,
},
})
)
})
})
19 changes: 3 additions & 16 deletions packages/gatsby-source-contentful/src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,10 @@ module.exports = async function contentfulFetch({
try {
reporter.verbose(`Fetching default locale`)
space = await client.getSpace()
let contentfulLocales = await client
.getLocales()
.then(response => response.items)
defaultLocale = _.find(contentfulLocales, { default: true }).code
locales = contentfulLocales.filter(pluginConfig.get(`localeFilter`))
if (locales.length === 0) {
reporter.panic({
id: CODES.LocalesMissing,
context: {
sourceMessage: `Please check if your localeFilter is configured properly. Locales '${contentfulLocales
.map(item => item.code)
.join(`,`)}' were found but were filtered down to none.`,
},
})
}
locales = await client.getLocales().then(response => response.items)
defaultLocale = _.find(locales, { default: true }).code
reporter.verbose(
`Default locale is: ${defaultLocale}. There are ${contentfulLocales.length} locales in total.`
`Default locale is: ${defaultLocale}. There are ${locales.length} locales in total.`
)
} catch (e) {
let details
Expand Down
17 changes: 17 additions & 0 deletions packages/gatsby-source-contentful/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const fs = require(`fs-extra`)
const { createClient } = require(`contentful`)
const v8 = require(`v8`)
const fetch = require(`node-fetch`)
const { CODES } = require(`./report`)

const normalize = require(`./normalize`)
const fetchData = require(`./fetch`)
Expand Down Expand Up @@ -293,6 +294,22 @@ exports.sourceNodes = async (
}
}

const allLocales = locales
locales = locales.filter(pluginConfig.get(`localeFilter`))
reporter.verbose(
`All locales: ${allLocales}, default: ${defaultLocale}, after plugin.options.localeFilter: ${locales}`
)
if (locales.length === 0) {
reporter.panic({
id: CODES.LocalesMissing,
context: {
sourceMessage: `Please check if your localeFilter is configured properly. Locales '${allLocales
.map(item => item.code)
.join(`,`)}' were found but were filtered down to none.`,
},
})
}

createTypes(`
interface ContentfulEntry @nodeInterface {
contentful_id: String!
Expand Down