lunr.js icon indicating copy to clipboard operation
lunr.js copied to clipboard

Search breaks in react-native release build but not in debug build

Open aecorredor opened this issue 6 years ago • 11 comments

I've been going through the issues like crazy to find something similar to what is happening to me. The closest one I've found is #279 and the possible solutions haven't worked. My app breaks when searching in the release build for some terms, but not when it's on debug mode. I've also gone through the react-native issues and have tried to fix the minify/uglify bugs but haven't been able to solve the problem I'm having. I get TypeError: undefined is not an object (evaluating 'p._index') when I call search with 'maine* maine v* v' for example. The weird thing is that in debug mode it works as expected, but in the release build it crashes.

Here's my code for building the index, and my code for searching:

export default {
  createSearchIndex: function (items) {
    return lunr(function() {
      this.ref('id');
      this.field('title');
      this.field('description');

      items
        .forEach(item => {
          // Adds description index field to the outer object so videos are also
          // searchable by description as per requirements.
          const data = item && item.data || {};
          const description = data.description && data.description.toLowerCase() || '';

          const searchableItem = {
            ...item,
            description,
          }

          this.add(searchableItem);
        });
    });
  },
  search: (searchText, searchIndex, videoDocs) => {
    const query = searchText
      .toLowerCase()
      .split(/\s+/)
      // Add asterisk to partially match terms: ALA* -> ALASKA, ALAMO, etc...
      // this query will give a high positive boost to exact matches, and
      // a lower one to partially matched terms.
      .map(term => term && `${term}^100 ${term}*^10` || '')
      .join(' ')
      .trim();

    const searchResults = query ?
      searchIndex.search(query).map(result => videoDocs[result.ref]) :
      [];

    return searchResults;
  }
};

aecorredor avatar Apr 20 '18 21:04 aecorredor

@aecorredor I know next to nothing about React Native, so bear with me, but how are you passing the pre-built search index to search? Are you keeping it around in a global variable, or are you serializing it to some storage and then deserializing it before you search?

hoelzro avatar Apr 22 '18 15:04 hoelzro

@hoelzro I build the index when the app launches and then keep it in my redux store so I can connect different components that might need search. Just an FYI there’s an issue open in RN for minification breaking some libraries; I tried the specified solution but had no luck. I don’t want to have to switch search libraries just for that, because it works on development and just breaks on release. So I guess it has to do with some part of the library being incorrectly minified for production.

aecorredor avatar Apr 22 '18 16:04 aecorredor

@aecorredor From the look of that error message, I'm guessing that's the issue

hoelzro avatar Apr 23 '18 12:04 hoelzro

@aecorredor do you have a link to the issue on RN for magnification problems? Might be worth updating the RN team with Lunr as a reproduction to help them test any fixes they have.

olivernn avatar Apr 24 '18 06:04 olivernn

@oliviernn https://github.com/facebook/react-native/issues/9711

I had to switch to fuse.js for now which I didn’t want to do but had no other choice. I had already spent too much time figuring what part of the build process was breaking it. Hopefully we can figure it out. Cheers.

aecorredor avatar Apr 25 '18 14:04 aecorredor

@aecorredor Thanks for the link. A brief look through it suggests that react native is using uglify for minification, this is what is used by Lunr for generating minified code so it certainly is a strange bug.

olivernn avatar Apr 25 '18 15:04 olivernn

@olivernn no problem. And also, I kind of discarded that it was a bug on my code from the get go because on RN debug builds it works perfectly fine and it never crashes.

aecorredor avatar Apr 25 '18 15:04 aecorredor

Any updates on using lunr + React Native?

yaronlevi avatar May 27 '18 13:05 yaronlevi

@aecorredor @yaronlevi are you still having problems in recent RN versions? I'm on RN 0.55 and not getting a crash in a Release build when I call idx.search('test*');

dave-irvine avatar Jul 05 '18 08:07 dave-irvine

@dave-irvine I ended up having to switch to fuse.js so I wouldn’t be able to tell you. Sorry mate.

aecorredor avatar Jul 05 '18 13:07 aecorredor

I am quite confident that this issue with React Native was fixed by this PR:

https://github.com/olivernn/lunr.js/pull/361

The PR originally targeted a bug in Safari, and React Native on iOS uses the same JS engine as Safari.

lucaong avatar Jan 23 '19 16:01 lucaong