algolia / gatsby-plugin-algolia

A plugin to push to Algolia based on graphQl queries
https://yarn.pm/gatsby-plugin-algolia
Apache License 2.0
177 stars 45 forks source link

Index settings (searchable attributes, snippeting, etc) are cleared on index update #110

Closed epbarger closed 3 years ago

epbarger commented 3 years ago

Running on 0.15.1, with enablePartialUpdates: false and chunkSize: 1000. When the index process has finished, checking the Algolia UI shows that many settings have reset to default, including "Searchable attributes", "Attributes to snippet", and "Facets". Otherwise, the index updates successfully.

I believe this was working fine in previous versions. It seems unexpected that these settings would be cleared?

The first version I experience this on is 0.14.1 (0.14.0 produced the error noted here)

Plugin config

      resolve: `gatsby-plugin-algolia`,
      options: {
        appId: process.env.ALGOLIA_APP_ID,
        apiKey: process.env.ALGOLIA_API_KEY,
        indexName: 'index-name-here',
        queries,
        chunkSize: 1000,
        enablePartialUpdates: false,
        skipIndexing: !process.env.INDEX_ON_BUILD,
      }
Haroenv commented 3 years ago

could you provide with what the queries are here? this does seem like a bug, but I'm not too sure what the cause is. I guess possibly settings without value gets set to empty object instead of undefined?

epbarger commented 3 years ago

Sure! Queries is pretty simple .

       const queries = [
          {
            query: indexQuery,
            transformer: ({ data }) =>
              splitNodeContent(
                addBreadcrumbsToNodes(data.allMdx.nodes).map(node =>
                  transformNodeForAlgolia(node),
                ),
               ),
            indexName: 'index-name-here',
          },
        ];

I'll mess with the configuration a bit more and see if I can find anything that causes the issue to manifest. So far I've tried adding settings: {} to both the main and query config, to no effect

Haroenv commented 3 years ago

if you can debug around this line what value settings has, it could be useful https://github.com/algolia/gatsby-plugin-algolia/blob/master/gatsby-node.js#L252

epbarger commented 3 years ago

if you can debug around this line what value settings has, it could be useful https://github.com/algolia/gatsby-plugin-algolia/blob/master/gatsby-node.js#L252

Sure, settings is undefined there, so it's not entering the conditional

epbarger commented 3 years ago

I was able to edit gatsby-node a bit and got something working. No idea if this is a safe solution to the problem or not though without tests or knowledge of other configurations.

Hope this is helpful! Probably cleaner ways to fix this, but it's a start.

Also, it looks like when you provide settings it completely replaces the existing settings? I'm not sure I would have expected that from the README. Maybe worth more explanation, or even a way to control whether the settings you provide are merged with the existing settings, or replace them. Something like a mergeSettings option.

Haroenv commented 3 years ago

It overrides the settings, since if you first set a setting with Gatsby, change the code later to remove that setting, I think you would prefer only the new settings to apply. You're right that in hindsight requiring all settings to be set via Gatsby seems more problematic than removed settings still applying

Defaulting to empty settings would mean that we can't differentiate between no settings given and "remove all settings" (although that doesn't ever seem like a good idea).

Could you make a pull request with your suggested changes for making this work properly again with settings not given?

Also a pull request explaining settings more clearly with how you understood them would be very welcome!

epbarger commented 3 years ago

Sure, I'll try to get a PR up in the next few days. Thanks for your help!