diff --git a/.github/workflows/publish-gpr.yml b/.github/workflows/publish-gpr.yml index ec0d8be..26c6688 100644 --- a/.github/workflows/publish-gpr.yml +++ b/.github/workflows/publish-gpr.yml @@ -5,7 +5,7 @@ on: push: # Sequence of patterns matched against refs/tags tags: - - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10 + - "v*" # Push events to matching v*, i.e. v1.0, v20.15.10 name: Create Release diff --git a/.github/workflows/publish-npm.yml b/.github/workflows/publish-npm.yml index 20395ea..b5ffd6f 100644 --- a/.github/workflows/publish-npm.yml +++ b/.github/workflows/publish-npm.yml @@ -13,7 +13,7 @@ jobs: - uses: actions/setup-node@v1 with: registry-url: "https://registry.npmjs.org" - - run: npm + - run: npm install - run: npm publish env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.npmignore b/.npmignore index f9b3e28..bf1f180 100644 --- a/.npmignore +++ b/.npmignore @@ -2,5 +2,10 @@ src .babelrc .editorconfig .gitattributes +.prettierrc +.prettierignore +.eslintrc +.eslintignore +index.js package-scripts.js example/ diff --git a/README.md b/README.md index 101c53c..b4576a1 100644 --- a/README.md +++ b/README.md @@ -42,8 +42,7 @@ module.exports = { }, }, // Optional filter to limit indexed nodes - filter: (node, getNode) => - node.frontmatter.tags !== 'exempt', + filter: (node, getNode) => node.frontmatter.tags !== "exempt", }, }, ], @@ -174,6 +173,7 @@ search = evt => { ## Optimize handling of data models with nested nodes There are times when you have a data model that has nested nodes. Example resolver configuration in `gatsby-config.js`: + ``` resolvers : { // For any node of BlogPost, list how to resolve the fields' values @@ -188,6 +188,7 @@ resolvers : { } } ``` + The problem with the above resolvers configuration is that it will include all Asset models in the `elasticlunr` index, potentially bloating the `elasticlunr` index and leading to large bundle sizes and slower page load times. @@ -195,6 +196,7 @@ The solution is to make use of the second paramater passed to each field resolve to the [setFieldsOnGraphQLNodeType](https://www.gatsbyjs.org/docs/node-apis/#setFieldsOnGraphQLNodeType) node api method and when called with a data model node id it will return a node with all it's data. The above example of the `BlogPost` model with the nested `featuredImage` property of type `Asset` then becomes: + ``` resolvers : { // For any node of BlogPost, list how to resolve the fields' values @@ -204,20 +206,18 @@ resolvers : { } } ``` -Now you can use the `featuredImage` data of `BlogPost` model without including all `Asset` models in the `elasticlunr` index [(see PR #3 for more details)](https://github.com/gatsby-contrib/gatsby-plugin-elasticlunr-search/pull/3). +Now you can use the `featuredImage` data of `BlogPost` model without including all `Asset` models in the `elasticlunr` index [(see PR #3 for more details)](https://github.com/gatsby-contrib/gatsby-plugin-elasticlunr-search/pull/3). -You can now also resolve the gatsby store with ``getNodesByType`` and ``getNodes`` +You can now also resolve the gatsby store with `getNodesByType` and `getNodes` so the full signature of node resolving is this: + ``` (node, getNode, getNodesByType, getNodes) ``` + Documentation of all node helpers: - [getNode](https://www.gatsbyjs.org/docs/node-api-helpers/#getNode) - [getNodesByType](https://www.gatsbyjs.org/docs/node-api-helpers/#getNodesByType) - [getNodes](https://www.gatsbyjs.org/docs/node-api-helpers/#getNodes) - - - - diff --git a/package.json b/package.json index f9e97c3..29ec7bf 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "@gatsby-contrib/gatsby-plugin-elasticlunr-search", - "version": "2.4.1", + "version": "2.4.2", "description": "Search for gatsby; implemented via elasticlunr.", - "main": "n/a", + "main": "gatsby-node.js", "scripts": { "build": "babel src --out-dir .", "build:watch": "babel src --watch --out-dir .", @@ -55,4 +55,4 @@ "engines": { "node": ">=10.13.0" } -} +} \ No newline at end of file diff --git a/src/gatsby-node.js b/src/gatsby-node.js index 67aba50..25dbcd4 100644 --- a/src/gatsby-node.js +++ b/src/gatsby-node.js @@ -41,7 +41,7 @@ const createOrGetIndex = async ( node, cache, getNode, - getNodesByType, + getNodesByType, getNodes, server, { fields, resolvers } @@ -67,7 +67,12 @@ const createOrGetIndex = async ( ...Object.keys(fieldResolvers).reduce((prev, key) => { return { ...prev, - [key]: fieldResolvers[key](pageNode, getNode, getNodesByType, getNodes), + [key]: fieldResolvers[key]( + pageNode, + getNode, + getNodesByType, + getNodes + ), } }, {}), } @@ -109,7 +114,9 @@ exports.onCreateNode = ({ node, actions, getNode }, { resolvers, filter }) => { return } - if (filter && !filter(node, getNode)) { return } + if (filter && !filter(node, getNode)) { + return + } const { createNode } = actions const searchIndex = getNode(SEARCH_INDEX_ID) || createEmptySearchIndexNode() @@ -129,7 +136,15 @@ exports.setFieldsOnGraphQLNodeType = ( index: { type: SearchIndex, resolve: (node, _opts, _3, server) => - createOrGetIndex(node, cache, getNode, getNodesByType, getNodes, server, pluginOptions), + createOrGetIndex( + node, + cache, + getNode, + getNodesByType, + getNodes, + server, + pluginOptions + ), }, } }