]> git.ardis.lu - ardislu.dev/commitdiff
Always sync search field value regardless of path
authorArdis Lu <ardisglu@gmail.com>
Sat, 4 Mar 2023 07:10:05 +0000 (23:10 -0800)
committerArdis Lu <ardisglu@gmail.com>
Sat, 4 Mar 2023 07:10:05 +0000 (23:10 -0800)
Otherwise 'back' button to home will not clear search as expected

script.js

index f2c59fa6733d6d1daaca1736a02c59b38de4a993..828cdda44417fb82666a2b8a5f498224129cae3a 100644 (file)
--- a/script.js
+++ b/script.js
@@ -219,6 +219,12 @@ async function showPage(path) {
     document.querySelector('main').replaceWith(globalThis.components.get('article-skeleton'));
   }
 
+  // Sync the search field value with the query param. A de-sync can happen if the user indirectly sets
+  // the search query param (e.g. direct links, browser navigation)
+  const form = document.querySelector('form');
+  const query = new URLSearchParams(location.search).get('q');
+  form.elements['query'].value = query;
+
   // Load all post metadata if it hasn't been loaded yet
   if (globalThis.metadata.size === 0) {
     globalThis.metadata = await fetchPostMetadata();
@@ -237,10 +243,6 @@ 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)) {
This page took 0.037398 seconds and 4 git commands to generate.