From d45eb1c9455915e1fa32aeb29da5d7b7679a9453 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Tue, 11 Jun 2024 02:52:31 -0500 Subject: [PATCH] Always use the latest form data in submit event --- plugins/speculation-rules/search-form.js | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/plugins/speculation-rules/search-form.js b/plugins/speculation-rules/search-form.js index 9a0440b3de..8e373a4965 100644 --- a/plugins/speculation-rules/search-form.js +++ b/plugins/speculation-rules/search-form.js @@ -51,7 +51,7 @@ const { actions } = store( 'speculationRules', { if ( ! formData.get( 's' ) ) { context.speculativeLoadUrl = null; } else { - const url = new URL( form.action || location.href ); + const url = new URL( form.action ); url.search = new URLSearchParams( formData ).toString(); context.speculativeLoadUrl = url.href; } @@ -63,11 +63,12 @@ const { actions } = store( 'speculationRules', { } }, handleFormSubmit: ( event ) => { - const context = getContext(); - if ( context.speculativeLoadUrl ) { - event.preventDefault(); - location.href = context.speculativeLoadUrl; - } + event.preventDefault(); + const form = event.target; + const formData = new FormData( form ); + const url = new URL( form.action ); + url.search = new URLSearchParams( formData ).toString(); + location.href = url.href; }, }, } );