gatsby-plugin-lunr icon indicating copy to clipboard operation
gatsby-plugin-lunr copied to clipboard

Fetch index only on certain pages

Open rtrvrtg opened this issue 5 years ago • 8 comments

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?

rtrvrtg avatar Jan 02 '20 14:01 rtrvrtg

Thank you for contribution! Looks cool!

deemaagog avatar Jan 07 '20 21:01 deemaagog

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 includeOptions like { include: [], exclude: [] }. Right now I've got it set to never load the Lunr search index at all

I'm ok with this

deemaagog avatar Jan 07 '20 21:01 deemaagog

I'll merge and bump new version as soon as possible

deemaagog avatar Jan 07 '20 21:01 deemaagog

@deemaagog Thanks Dmitry, I'll see about getting that sample into the README so users know what to do.

rtrvrtg avatar Jan 08 '20 06:01 rtrvrtg

Added that extra part to the README.

rtrvrtg avatar Jan 08 '20 23:01 rtrvrtg

@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 avatar Jan 12 '20 21:01 deemaagog

@deemaagog Hi Dmitry, good call. I'll take another look at it.

rtrvrtg avatar Jan 15 '20 04:01 rtrvrtg

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 ?

qcho avatar Jul 02 '20 23:07 qcho