CloudCannon / pagefind

Static low-bandwidth search at scale
https://pagefind.app
MIT License
3.22k stars 97 forks source link

`processResult()` is run each time a filter is checked or unchecked #589

Open nhoizey opened 3 months ago

nhoizey commented 3 months ago

I wanted to enrich my results titles with processResult(), and thought this would be enough:

processResult: function (result) {
  // Add the page type to the title
  result.meta.title = `${result.filters['00. Page type']} ▸ ${result.meta.title}`;

  return result;
}

Unfortunately, with this code, the page type (value of the 00. Page type filter) is added in front of current title each type I check or uncheck a filter.

Live demo:

Is it a feature, and I should check/replace in my processResult() function, or is it a bug?

bglw commented 3 months ago

I don't know which I would classify this as — but it is something Pagefind should indeed handle better in either case, since this function is meant to offer mutation.

Internally this can pose an issue as well, ex: https://github.com/CloudCannon/pagefind/blob/fe11bee730dd15d2f35dc5cd34db8cc15e3fe592/pagefind_web_js/lib/coupled_search.ts#L286-L289

For now, I would implement a similar operation, e.g.:

processResult: function (result) {
  if (!result.raw_title) {
    result.raw_title = result.meta.title;
  }
  // Add the page type to the title
  result.meta.title = `${result.filters['00. Page type']} ▸ ${result.raw_title}`;

  return result;
}
bglw commented 3 months ago

Marked as a bug, with a future goal of Pagefind always providing the original unmodified values to the processResult hook.

nhoizey commented 3 months ago

Thanks!