diff --git a/web/lib/algolia.ts b/web/lib/algolia.ts index f2cb07f56..14c039122 100644 --- a/web/lib/algolia.ts +++ b/web/lib/algolia.ts @@ -1,5 +1,19 @@ import algoliasearch, { AlgoliaSearchOptions } from 'algoliasearch/lite' import { algolia } from './config' +import { createInMemoryCache } from '@algolia/cache-in-memory' +import { createFallbackableCache } from '@algolia/cache-common' +import { createBrowserLocalStorageCache } from '@algolia/cache-browser-local-storage' -export const searchClient = (options: AlgoliaSearchOptions | undefined) => - algoliasearch(algolia.applicationId, algolia.searchApiKey, options) +export const searchClient = (options?: AlgoliaSearchOptions) => + algoliasearch(algolia.applicationId, algolia.searchApiKey, { + ...options, + responsesCache: createFallbackableCache({ + caches: [ + createBrowserLocalStorageCache({ + key: `algolia-responses-${algolia.applicationId}`, + timeToLive: 600, // 10 minutes cache + }), + createInMemoryCache(), + ], + }), + }) diff --git a/web/package.json b/web/package.json index 0b62a895a..54c51f370 100644 --- a/web/package.json +++ b/web/package.json @@ -21,6 +21,9 @@ "type-check": "tsc --project tsconfig.json --pretty --noEmit" }, "dependencies": { + "@algolia/cache-browser-local-storage": "^4.22.1", + "@algolia/cache-common": "^4.22.1", + "@algolia/cache-in-memory": "^4.22.1", "@chakra-ui/react": "^2.4.2", "@chakra-ui/skip-nav": "^2.0.12", "@emotion/react": "^11", diff --git a/web/pageComponents/pageTemplates/newsroom/UncontrolledSearchBox.tsx b/web/pageComponents/pageTemplates/newsroom/UncontrolledSearchBox.tsx index 886d7c3b9..7ab72ea30 100644 --- a/web/pageComponents/pageTemplates/newsroom/UncontrolledSearchBox.tsx +++ b/web/pageComponents/pageTemplates/newsroom/UncontrolledSearchBox.tsx @@ -42,7 +42,7 @@ const Input = styled.input` export type SearchBoxProps = UseSearchBoxProps -const DEBOUNCE_TIME = 300 +const DEBOUNCE_TIME = 800 let timerId: any = undefined const queryHook: UseSearchBoxProps['queryHook'] = (query, search) => { diff --git a/web/pageComponents/search/Search.tsx b/web/pageComponents/search/Search.tsx index 5e7974d51..2493b7bdd 100644 --- a/web/pageComponents/search/Search.tsx +++ b/web/pageComponents/search/Search.tsx @@ -22,7 +22,7 @@ const StyledPagination = styled(Pagination)` margin-top: var(--space-xLarge); justify-content: center; ` -const searchClient = client(undefined) +const searchClient = client() const queriedSearchClient: SearchClient = { ...searchClient, search(requests: any) { diff --git a/web/pageComponents/search/SearchBox.tsx b/web/pageComponents/search/SearchBox.tsx index eaf98f6cf..1b1426ce3 100644 --- a/web/pageComponents/search/SearchBox.tsx +++ b/web/pageComponents/search/SearchBox.tsx @@ -3,7 +3,7 @@ import { useSearchBox, UseSearchBoxProps } from 'react-instantsearch' import ControlledSearchBox from './ControlledSearchBox' import { SearchContext } from './SearchContext' -const DEBOUNCE_TIME = 300 +const DEBOUNCE_TIME = 800 export type SearchBoxProps = ComponentProps<'div'> & UseSearchBoxProps diff --git a/web/pnpm-lock.yaml b/web/pnpm-lock.yaml index 25a3b534a..b9d5681e4 100644 --- a/web/pnpm-lock.yaml +++ b/web/pnpm-lock.yaml @@ -5,6 +5,15 @@ settings: excludeLinksFromLockfile: false dependencies: + '@algolia/cache-browser-local-storage': + specifier: ^4.22.1 + version: 4.22.1 + '@algolia/cache-common': + specifier: ^4.22.1 + version: 4.22.1 + '@algolia/cache-in-memory': + specifier: ^4.22.1 + version: 4.22.1 '@chakra-ui/react': specifier: ^2.4.2 version: 2.4.2(@emotion/react@11.0.0)(@emotion/styled@11.0.0)(@types/react@18.0.9)(framer-motion@6.0.0)(react-dom@18.2.0)(react@18.2.0) @@ -295,14 +304,30 @@ packages: dependencies: '@algolia/cache-common': 4.16.0 + /@algolia/cache-browser-local-storage@4.22.1: + resolution: {integrity: sha512-Sw6IAmOCvvP6QNgY9j+Hv09mvkvEIDKjYW8ow0UDDAxSXy664RBNQk3i/0nt7gvceOJ6jGmOTimaZoY1THmU7g==} + dependencies: + '@algolia/cache-common': 4.22.1 + dev: false + /@algolia/cache-common@4.16.0: resolution: {integrity: sha512-4iHjkSYQYw46pITrNQgXXhvUmcekI8INz1m+SzmqLX8jexSSy4Ky4zfGhZzhhhLHXUP3+x/PK/c0qPjxEvRwKQ==} + /@algolia/cache-common@4.22.1: + resolution: {integrity: sha512-TJMBKqZNKYB9TptRRjSUtevJeQVXRmg6rk9qgFKWvOy8jhCPdyNZV1nB3SKGufzvTVbomAukFR8guu/8NRKBTA==} + dev: false + /@algolia/cache-in-memory@4.16.0: resolution: {integrity: sha512-p7RYykvA6Ip6QENxrh99nOD77otVh1sJRivcgcVpnjoZb5sIN3t33eUY1DpB9QSBizcrW+qk19rNkdnZ43a+PQ==} dependencies: '@algolia/cache-common': 4.16.0 + /@algolia/cache-in-memory@4.22.1: + resolution: {integrity: sha512-ve+6Ac2LhwpufuWavM/aHjLoNz/Z/sYSgNIXsinGofWOysPilQZPUetqLj8vbvi+DHZZaYSEP9H5SRVXnpsNNw==} + dependencies: + '@algolia/cache-common': 4.22.1 + dev: false + /@algolia/client-account@4.16.0: resolution: {integrity: sha512-eydcfpdIyuWoKgUSz5iZ/L0wE/Wl7958kACkvTHLDNXvK/b8Z1zypoJavh6/km1ZNQmFpeYS2jrmq0kUSFn02w==} dependencies: