flexsearch icon indicating copy to clipboard operation
flexsearch copied to clipboard

Document index + worker in NextJS

Open artsiompeshko opened this issue 8 months ago • 0 comments

👋 First of all, thanks for a wonderful alternative to server-baked full-text searches; this library truly performs well.

I could not integrate the document index with the worker enabled inside the NextJS app.

With the below index builder, I am getting Failed to construct 'Worker': Script at ... cannot be accessed from origin, as it tries to resolve worker via file path.

const FlexSearchModule = await import('flexsearch');
  const DefaultEncoder = /* ...  */

  return new FlexSearchModule.Document({
    tokenize: 'forward',
    encoder: DefaultEncoder,
    priority: 2,
    worker: true,
    document: {
      id: 'documentId',
      store: true,
      index: [/* ... */]
    }
  });

I tried several variants to use a string path in the worker property, as I see it can accept it, but could not find a working one.

It seems like webpack 5 expects path to be wrapper with new URL to be working and it does not support variable passing https://webpack.js.org/guides/web-workers/.

artsiompeshko avatar Aug 15 '25 00:08 artsiompeshko