Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
slivos opened this issue Dec 1, 2024 · 1 comment
Open

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

slivos opened this issue Dec 1, 2024 · 1 comment

Comments

@slivos
Copy link

slivos commented Dec 1, 2024

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 slivos changed the title Lost search results when navigating back - Eleventy + Nunjucks Lost search results when navigating back / Eleventy + Nunjucks Dec 1, 2024
@slivos
Copy link
Author

slivos commented Dec 1, 2024

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"));
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant