When I have been testing to make the website searchable with FlexSearch I couldn't find a really good way of integrating the indexing in the build process. The most obvious solution was to run the indexing as a script that is referenced from the build command in package.json, but there's a few issues with that approach: When using typescript we need to run the script with ts-node, which often causes issues due with esm and imports. It would also be nicer to have it integrated within the nextjs framework.
As nextjs supports statically building the sitemap when building this seems like a good place to build the indexes. It looks something like this:
The way FlexSearch works is that it creates a page and a section-index as a json file that is shipped all the way to the client and the search is performed there. The code above creates the indexes as stores it in the public folder and it available at https://ahlstrand.es/search-index.json. It is a fairly large file so I wonder if this will scale when adding a lot of content, but I guess we'll see.