CloudCannon / pagefind

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

Lost search results when navigating back / Eleventy + Nunjucks #748

Open slivos opened 7 hours ago

slivos commented 7 hours ago

Hello there, I noticed one thing that on other sites works well, but somehow on my project (Eleventy + Nunjucks) is not working as expected.

I have 1 search page and different page for blogs. When I start to search on my search page I type some word, results show OK, now I click on some result, it redirects me to the correct URL, e. g. blog post.

But here starts the problem. When I want to go back to search page by clicking on "step back" arrow in browser, my results are not visible anymore, but searched query in input is still there. Which is interesting, only the results missing.

When I start to type again, it works normally like at the beginning.

Has someone similar issue?

Thank you for your response.

By the way, very good job about the whole library.

OS: Windows 10, Browser: Google Chrome, Firefox, latest version of Eleventy 3.0.0, latest Pagefind 1.2.0

slivos commented 7 hours ago

OK I found nice solution on issue #588, thank you.

Now in my init script it looks like this:

window.addEventListener("DOMContentLoaded", (event) => {
  const params = new URLSearchParams(window.location.search);
  const searchUI = new PagefindUI({
    element: "#search",
    showImages: false,
    pageSize: 10,
    translations: {
      placeholder: "Hľadať v článkoch",
      zero_results: "Nenašlo sa pre [SEARCH_TERM]",
    },
    processTerm: function (term) {
      history.replaceState({}, "", `?q=${encodeURIComponent(term)}`);
      return term;
    },
  });

  if (params.has("q")) {
    searchUI.triggerSearch(params.get("q"));
  }
});