Skip to content

Commit

Permalink
✨ Add caching strategy and double debounce time #1987 (#2080)
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandolucchesi authored Jan 30, 2024
1 parent e73323e commit 423b34e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 5 deletions.
18 changes: 16 additions & 2 deletions web/lib/algolia.ts
Original file line number Diff line number Diff line change
@@ -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(),
],
}),
})
3 changes: 3 additions & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion web/pageComponents/search/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion web/pageComponents/search/SearchBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 25 additions & 0 deletions web/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 423b34e

Please sign in to comment.