Home > widget-integration > RecentSearchService

RecentSearchService class

Service for managing recent searches in localStorage.

Technical Support teams can extend this service for custom storage strategies:

Signature:

export declare class RecentSearchService 

Example 1

window.boostWidgetIntegration.extend('RecentSearchService', (RecentSearchService) => {
  return class CustomRecentSearchService extends RecentSearchService {
    // Use session storage instead of local storage
    protected getStorage() {
      return sessionStorage;
    }
  };
});

Example 2

// Access the service via module
const recentSearchService = window.boostISWModule.recentSearchService;

// Get recent searches
const searches = recentSearchService.getRecentSearches(5);

// Add a recent search
recentSearchService.addRecentSearch('blue shoes');

// Clear all searches
recentSearchService.clearRecentSearches();

Constructors

Constructor

Modifiers

Description

(constructor)(appService)

Constructs a new instance of the RecentSearchService class

Properties

Property

Modifiers

Type

Description

appService

protected

AppService

debugMode

protected

boolean

Enable debug logging

MAX_ITEMS

protected

readonly

(not declared)

Maximum number of recent searches to store

STORAGE_KEY

protected

readonly

(not declared)

LocalStorage key for recent searches

Methods

Method

Modifiers

Description

addRecentSearch(query, extraParam, scope)

Add a search term to recent searches.

**Legacy**: setOnClickRecentSearches(item, extraParam, scope) **New**: addRecentSearch(query, extraParam, scope) with deduplication

Features: - Moves duplicate searches to the top (case-insensitive) - Trims whitespace from query - Limits storage to MAX_ITEMS

clearRecentSearches()

Clear all recent searches.

**Legacy**: Not available **New**: Added for better UX

getRecentSearchCount()

Get the count of recent searches.

getRecentSearches(limit)

Get recent searches from localStorage.

**Legacy**: getOnClickRecentSearches(maxRecentSearch) **New**: getRecentSearches(limit) with proper typing

getRecentSearchTitles(limit)

Get recent search titles as a simple string array.

getStorage()

protected

Get the storage instance to use. Override this method to use a different storage strategy.

handleSuggestionTermClick(event)

Handle click events on suggestion terms and popular searches. Extracts the search term from clicked elements and saves it to recent searches.

**Legacy**: handleRecentSearchForSuggestionTerm(event) **New**: handleSuggestionTermClick(event) with proper typing

hasRecentSearch(query)

Check if a search term exists in recent searches.

log(message)

protected

Log a debug message when debug mode is enabled.

removeRecentSearch(query, extraParam)

Remove a specific search from recent searches.

**Legacy**: Not available **New**: Added for better UX

setDebugMode(enabled)

Enable or disable debug mode for logging.