pagefind icon indicating copy to clipboard operation
pagefind copied to clipboard

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

Open nhoizey opened this issue 2 years ago • 3 comments

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:

  • go to https://nicolas-hoizey.photo/search/?q=lamine
  • the first result title is Photo ▸ Inoummar agadir's Lamine
  • check/uncheck multiple times any filter
  • the title becomes Photo ▸ Inoummar agadir's Lamine
    • then Photo ▸ Photo ▸ Inoummar agadir's Lamine
    • then Photo ▸ Photo ▸ Photo ▸ Inoummar agadir's Lamine
    • etc.

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

nhoizey avatar Apr 03 '24 17:04 nhoizey

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 avatar Apr 04 '24 05:04 bglw

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

bglw avatar Apr 04 '24 05:04 bglw

Thanks!

nhoizey avatar Apr 04 '24 08:04 nhoizey