Skip to content

Commit

Permalink
feat(gatsby-source-contentful): add locale filter (#12939)
Browse files Browse the repository at this point in the history
* Added locale filter when creating nodes

* Updated test locale and snapshot

* Updated from PR comments, moved filtering function earlier in the flow

* New line between function

* add note where to get locale codes
  • Loading branch information
stoltzrobin authored and pieh committed Apr 16, 2019
1 parent ec2d07d commit 6421c5b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/gatsby-source-contentful/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ Downloads and caches `ContentfulAsset`'s to the local filesystem. Allows you to

You can pass in any other options available in the [contentful.js SDK](https://github.com/contentful/contentful.js#configuration).

**`localeFilter`** [function][optional] [default: `() => true`]

Possibility to limit how many locales/nodes are created in graphQL. This can limit the memory usage by reducing the amout of nodes created. Useful if you have a large space in contentful and only want to get the data from one selected locale.

For example, to filter locales on only germany `localeFilter: locale => locale.code === 'de-DE'`

List of locales and their codes can be found in Contentful app -> Settings -> Locales

## Notes on Contentful Content Models

There are currently some things to keep in mind when building your content models at Contentful.
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-source-contentful/src/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = async ({ spaceId, syncToken, ...options }) => {
console.log(`Fetching default locale`)
locales = await client.getLocales().then(response => response.items)
defaultLocale = _.find(locales, { default: true }).code
locales = locales.filter(options.localeFilter || (() => true))
console.log(`default locale is : ${defaultLocale}`)
} catch (e) {
console.log(
Expand Down

0 comments on commit 6421c5b

Please sign in to comment.