gatsby-plugin-lunr
gatsby-plugin-lunr copied to clipboard
Fetch index only on certain pages
Handles the case described in #21.
This is done by defining support for additional config, includeOptions, which lets you include and exclude specific paths from loading the Lunr search index. If this property isn't present, or an empty object is used as config, it'll use the existing behaviour of loading on the first page load, using onClientEntry.
If includeOptions is specified, it expects the keys include and/or exclude, which contains an array of strings that will be converted to RegExps for matching. These will be used in the onPreRouteUpdate hook, using the path we're about to navigate to and matching against the include and exclude paths.
I'm a little unsure about what to do if someone uses an includeOptions like { include: [], exclude: [] }. Right now I've got it set to never load the Lunr search index at all, but it might make more sense to bump it back to onClientEntry in this case. Interested in your thoughts about this.
Also, we'll want to add a recommendation for some sort of watcher to wait for the Lunr search index to be loaded on the relevant pages before we try using it, something Promise-based probably. Should we include a sample in the code that can optionally be used?
Thank you for contribution! Looks cool!
Also, we'll want to add a recommendation for some sort of watcher to wait for the Lunr search index to be loaded on the relevant pages before we try using it, something Promise-based probably. Should we include a sample in the code that can optionally be used?
For sure, feel free to add a sample in readme.
I'm a little unsure about what to do if someone uses an
includeOptionslike{ include: [], exclude: [] }. Right now I've got it set to never load the Lunr search index at all
I'm ok with this
I'll merge and bump new version as soon as possible
@deemaagog Thanks Dmitry, I'll see about getting that sample into the README so users know what to do.
Added that extra part to the README.
@rtrvrtg Hi Geoffrey! I'm not sure about your last commit. I guess we already have promise window.LUNR.__loaded.then(lunr => {}) for the same purpose. Why do we need this? Could you please check if window.LUNR.__loaded promise works for both scenarios? (with include options and without)
@deemaagog Hi Dmitry, good call. I'll take another look at it.
Isn't a better approach to fetch the index+store with a static query like other search-plugins do instead of loading it magically in window ?