From 50795b8a53701ded86f29005ed208b9bbe3eefb2 Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Fri, 3 Mar 2023 23:10:05 -0800 Subject: [PATCH] Always sync search field value regardless of path Otherwise 'back' button to home will not clear search as expected --- script.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/script.js b/script.js index f2c59fa..828cdda 100644 --- 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)) { -- 2.47.3