Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Merge pull request #149 from dpvitt/extra-page-fields-for-filtering
Browse files Browse the repository at this point in the history
Add `extraPageFields` to `AllPagesQuery` for better filtering
  • Loading branch information
birkir authored Mar 4, 2020
2 parents 49dbf64 + 66ac7ba commit 08609fc
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 2 deletions.
29 changes: 29 additions & 0 deletions packages/gatsby-source-prismic-graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ yarn add gatsby-source-prismic-graphql
component: require.resolve('./src/templates/article.js'),
sortBy: 'date_ASC', // optional, default: meta_lastPublicationDate_ASC; useful for pagination
}],
extraPageFields: 'article_type', // optional, extends pages query to pass extra fields
sharpKeys: [
/image|photo|picture/, // (default)
'profilepic',
Expand Down Expand Up @@ -141,6 +142,34 @@ Given 3 articles with UIDs of `why-i-like-music`, `why-i-like-sports` and `why-i
- `/blog/why-i-like-sports`
- `/blog/why-i-like-food`

### Generating pages from page fields

Sometimes the meta provided by default doesn't contain enough context to be able to filter pages effectively. By passing `extraPageFields` to the plugin options, we can extend what we can filter on.

```js
{
extraPageFields: 'music_genre',
pages: [{
type: 'Article',
match: '/techno/:uid',
filter: data => data.node.music_genre === 'techno',
path: '/blogposts',
component: require.resolve('./src/templates/article.js'),
}, {
type: 'Article',
match: '/acoustic/:uid',
filter: data => data.node.music_genre === 'acoustic',
path: '/blogposts',
component: require.resolve('./src/templates/article.js'),
}]
}
```

Given 2 articles with the `music_genre` field set, we'll get the following slugs:

/techno/darude
/acoustic/mik-parsons

### Support for Multiple Languages

Prismic allows you to create your content in multiple languages. This library supports that too. When setting up your configuration options in `gatsby-config.js`, there are three _optional_ properties you should be aware of: `options.defaultLang`, `options.langs`, and `options.pages[i].langs`. In the following example, all are in use:
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-source-prismic-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"bugs": {
"url": "https://github.com/birkir/gatsby-source-prismic-graphql/issues"
},
"version": "3.4.0",
"version": "3.5.0",
"main": "index.js",
"files": [
"*.js",
Expand Down
6 changes: 5 additions & 1 deletion packages/gatsby-source-prismic-graphql/src/gatsby-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,11 @@ function createDocumentPages(
const getDocumentsQuery = ({
documentType,
sortType,
extraPageFields,
}: {
documentType: string;
sortType: string;
extraPageFields: string;
}): string => `
query AllPagesQuery ($after: String, $lang: String, $sortBy: ${sortType}) {
prismic {
Expand All @@ -163,6 +165,7 @@ const getDocumentsQuery = ({
edges {
cursor
node {
${extraPageFields}
_meta {
id
lang
Expand Down Expand Up @@ -199,7 +202,8 @@ exports.createPages = async ({ graphql, actions: { createPage } }: any, options:
// Prepare and execute query
const documentType: string = `all${page.type}s`;
const sortType: string = `PRISMIC_Sort${page.type}y`;
const query: string = getDocumentsQuery({ documentType, sortType });
const extraPageFields = options.extraPageFields || '';
const query: string = getDocumentsQuery({ documentType, sortType, extraPageFields });
const { data, errors } = await graphql(query, {
after: endCursor,
lang: lang || null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ export interface PluginOptions {
pages?: Page[];
omitPrismicScript?: boolean;
sharpKeys: RegExp[] | string[];
extraPageFields: string;
}

0 comments on commit 08609fc

Please sign in to comment.