From 8c815cc83e1270df7fd1da60813bf31cf83c95f3 Mon Sep 17 00:00:00 2001 From: Ferit Date: Sun, 18 Oct 2026 23:20:43 +0300 Subject: [PATCH] Cache query only after validating source and results container --- src/index.ts | 4 ++-- test/test.js | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 5d61c5f..c1818d7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -89,8 +89,6 @@ async function fetchResults( const query = input.value if (checkCurrentQuery && state.currentQuery === query) return - state.currentQuery = query - const src = remoteInput.src if (!src) return @@ -99,6 +97,8 @@ async function fetchResults( ) if (!resultsContainer) return + state.currentQuery = query + const url = new URL(src, window.location.href) const params = new URLSearchParams(url.search) params.append(remoteInput.getAttribute('param') || 'q', query) diff --git a/test/test.js b/test/test.js index bbf0297..24416c3 100644 --- a/test/test.js +++ b/test/test.js @@ -35,6 +35,31 @@ describe('remote-input', function () { results = null }) + for (const missing of ['source', 'results']) { + it(`retries an unchanged query when the missing ${missing} becomes available`, async function () { + if (missing === 'source') remoteInput.removeAttribute('src') + else results.remove() + let loads = 0 + remoteInput.addEventListener('loadstart', () => loads++) + changeValue(input, 'test') + assert.equal(loads, 0) + + if (missing === 'source') remoteInput.src = '/results' + else document.body.append(results) + const success = once(remoteInput, 'remote-input-success') + input.dispatchEvent(new Event('focus')) + await success + assert.equal(loads, 1) + assert.equal( + results.querySelector('ol').getAttribute('data-src'), + '/results?q=test' + ) + + input.dispatchEvent(new Event('focus')) + assert.equal(loads, 1) + }) + } + it('emits network events in order', async function () { const events = [] const track = (event) => events.push(event.type)