Make zola ability to export search index into json instead of js

Hello,

I currently working on the implementation of a search feature for my personal site and discovered the search index feature coming from Zola.

However, it only exports this index into a js file.

I would make the ability to export it into a JSON file that can be loaded with an XHR request; useful when this search index is heavy.

I made a draft implementation in my fork: https://github.com/citorva/zola/commit/57757c0f745174a77f60c66ab90a64e76fb7e140

Why not load the current index with defer on the script node? I don’t see how the XHR helps there?

It is because I would only load the search index when the user would use the search feature

The advantage of doing it would be to be able to load it programmatically more easily. Using defer is indeed sufficient to improve the performance of the first page load, but it still means that the index is loaded even if the user never begins a search, which can be unnecessary.

Loading after clicking on the search bar can still be done with the current setup, as shown here with the following, but it’s quite cumbersome.

function loadSearchNow() {
    var loadSearch = document.createElement('script');
    loadSearch.setAttribute('src', '/search_bundle.min.js');
    document.head.appendChild(loadSearch);
    document.getElementById('userinput').onclick = '';
}
window.onload = function() {
    document.getElementById('userinput').onclick = function() { return loadSearchNow() }
};

And even then you need more code to wait for the JS to be loaded before actually performing the search. Having a .JSON file would allow simply loading it with fetch and use a global promise with with the result.

Also, parsing JSON is faster than parsing JS litterals.

Finally, I haven’t personally had the issue but it seems that the JS files for all languages store the index in window.searchIndex. This seems like it would prevent someone from having the indexes of multiple languages loaded at the same time. Having multiple JSON files instead would make it easy to load them in different places.

I’ll take a stab at making a PR adding a field to the config to generate search_index.<lang>.json

[search]
index_format = "javascript" | "json"