Skip to content

Commit

Permalink
Disable Search Autocomplete based off env var presence
Browse files Browse the repository at this point in the history
Autocomplete uses a denylist to stop certain terms from serving
suggestions. The process to update the denylist is very manual and
developer heavy.

It might be difficult to update the denylist over the holiday period. In
that situation the decision might be taken to temporarily turn off
autocomplete entirely.

An environment variable is used to determine whether search with autocomplete
has been disabled. The value of this variable will be applied
globally across all applications that use site search.

See: alphagov/govuk-helm-charts#2832

Co-authored-by: Leena Gupte <[email protected]>
  • Loading branch information
kevindew and leenagupte committed Dec 10, 2024
1 parent a0e6ada commit 42c69fb
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ GEM
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
chartkick (5.1.2)
climate_control (1.2.0)
coderay (1.1.3)
concurrent-ruby (1.3.4)
connection_pool (2.4.1)
Expand Down Expand Up @@ -614,6 +615,7 @@ PLATFORMS

DEPENDENCIES
capybara
climate_control
dartsass-rails
faker
gds-api-adapters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@
label: "Site-wide",
}
) do %>
<%= render "govuk_publishing_components/components/search_with_autocomplete", {
<% search_options = {
name: "keywords",
inline_label: false,
label_size: "m",
Expand All @@ -326,9 +326,15 @@
size: "large",
margin_bottom: 0,
disable_corrections: true,
source_url: [Plek.new.website_root, "/api/search/autocomplete.json"].join,
source_key: "suggestions"
} %>
<% if ENV["GOVUK_DISABLE_SEARCH_AUTOCOMPLETE"] %>
<%= render "govuk_publishing_components/components/search", search_options %>
<% else %>
<%= render "govuk_publishing_components/components/search_with_autocomplete", search_options.merge({
source_url: [Plek.new.website_root, "/api/search/autocomplete.json"].join,
source_key: "suggestions",
}) %>
<% end %>
<% end %>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions govuk_publishing_components.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Gem::Specification.new do |s|
s.add_dependency "sprockets-rails"

s.add_development_dependency "capybara"
s.add_development_dependency "climate_control"
s.add_development_dependency "dartsass-rails"
s.add_development_dependency "faker"
s.add_development_dependency "gds-api-adapters"
Expand Down
20 changes: 15 additions & 5 deletions spec/components/layout_super_navigation_header_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,6 @@ def component_name
assert_select ".js-module-initialised[data-module=\"super-navigation-mega-menu\"]", false
end

it "includes the search_with_autocomplete component with correct URL" do
render_component({})
assert_select ".gem-c-search-with-autocomplete[data-source-url='http://www.dev.gov.uk/api/search/autocomplete.json']"
end

it "adds GA4 tracking" do
render_component({})

Expand All @@ -180,4 +175,19 @@ def component_name
assert_select 'a[data-ga4-link=\'{"event_name":"navigation","type":"header menu bar","index_section":2,"index_link":6,"index_section_count":3,"index_total":6,"section":"Government activity"}\']'
assert_select "form[data-module='ga4-search-tracker']"
end

describe "search autocomplete" do
it "includes the search_with_autocomplete component by default" do
render_component({})
assert_select ".gem-c-search-with-autocomplete[data-source-url='http://www.dev.gov.uk/api/search/autocomplete.json']"
end

it "allows the GOVUK_DISABLE_SEARCH_AUTOCOMPLETE env var presence to fallback to search without autocomplete" do
ClimateControl.modify GOVUK_DISABLE_SEARCH_AUTOCOMPLETE: "1" do
render_component({})
assert_select ".gem-c-search-autocomplete", false
assert_select ".gem-c-search"
end
end
end
end
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require "rspec/rails"
require "capybara/rails"
require "govuk_test"
require "climate_control"

GovukTest.configure
Selenium::WebDriver::Options.chrome(loggingPrefs: { browser: "ALL" })
Expand Down

0 comments on commit 42c69fb

Please sign in to comment.