]> git.ardis.lu - ardislu.dev/commitdiff
Properly record searches in browser history
authorArdis Lu <ardisglu@gmail.com>
Tue, 15 Nov 2022 02:24:33 +0000 (18:24 -0800)
committerArdis Lu <ardisglu@gmail.com>
Tue, 15 Nov 2022 02:24:33 +0000 (18:24 -0800)
script.js

index 2f9d837a4d69bf2fd0498ae24df319327d1e2265..4cc2e77891163a59d37893651bfda2005c88224d 100644 (file)
--- a/script.js
+++ b/script.js
@@ -157,12 +157,7 @@ function search(str) {
 }
 globalThis.search = search;
 
-/* On search submit, replace <main> with the search results. */
-document.querySelector('form').addEventListener('submit', async event => {
-  event.preventDefault();
-
-  const data = new FormData(event.target);
-  const query = data.get('query')
+function showSearch(query) {
   if (query === '') {
     document.querySelector('main').replaceWith(globalThis.components.get('home'));
     return;
@@ -174,6 +169,18 @@ document.querySelector('form').addEventListener('submit', async event => {
   // TODO: update the query params to support deeplinks
   // TODO: logic to handle searching while in an article
   document.querySelector('main').replaceWith(filteredHome);
+}
+
+/* On search submit, replace <main> with the search results. */
+document.querySelector('form').addEventListener('submit', async event => {
+  event.preventDefault();
+
+  const data = new FormData(event.target);
+  const query = data.get('query');
+
+  // showPage flow is used for direct link. Manually set history here.
+  history.pushState({}, '', `/search?q=${query}`);
+  showSearch(query);
 });
 
 /* Focus the search input on '/' or Ctrl + k. */
@@ -216,6 +223,13 @@ async function showPage(path) {
 
     document.querySelector('main').replaceWith(globalThis.components.get('home'));
   }
+  else if (path === '/search') {
+    const form = document.querySelector('form');
+    const params = new URLSearchParams(location.search);
+    const query = params.get('q');
+    form.elements['query'].value = query;
+    showSearch(query); // Bypass form submission because it will set the history twice on direct link
+  }
   else if (globalThis.metadata.has(path)) {
     const post = globalThis.metadata.get(path);
 
This page took 0.037341 seconds and 4 git commands to generate.