improve docs search performance by removing preflight (#19395)

* refactor: remove credentials to prevent cors preflight

* fix: minor bug fix to source load counting

* set no-verify-jwt for search endpoints

* perf: reduce debounce interval

* fix: throw returned error from edge function

* fix: better error catching
This commit is contained in:
Charis authored and GitHub committed 2023-12-04 16:04:06 -05:00
1 parent 91e234cf4a
commit 6ae34935df
2 files changed
+46 -32

No files matched your search

@@ -18,6 +18,8 @@ import { CommandGroup, CommandItem, CommandLabel, TextHighlighter } from './Comm
const NUMBER_SOURCES = 2
const FUNCTIONS_URL = '/functions/v1/'
const questions = [
'How do I get started with Supabase?',
'How do I run Supabase locally?',
@@ -239,25 +241,33 @@ const DocsSearch = () => {
const sources = ['search-fts', 'search-embeddings']
sources.forEach((source) => {
supabaseClient.functions
.invoke(source, { body: { query } })
.then(({ data: results, error }) => {
sourcesLoaded += 1
if (error) {
dispatch({
type: 'errored',
key: localKey,
sourcesLoaded,
message: error.message ?? '',
})
} else {
dispatch({
type: 'resultsReturned',
key: localKey,
sourcesLoaded,
results,
})
fetch(`${process.env.NEXT_PUBLIC_SUPABASE_URL}${FUNCTIONS_URL}${source}`, {
method: 'POST',
body: JSON.stringify({ query }),
})
.then((response) => response.json())
.then((results) => {
if (!Array.isArray(results)) {
throw Error("didn't get expected results array")
}
sourcesLoaded += 1
dispatch({
type: 'resultsReturned',
key: localKey,
sourcesLoaded,
results,
})
})
.catch((error) => {
sourcesLoaded += 1
dispatch({
type: 'errored',
key: localKey,
sourcesLoaded,
message: error.message ?? '',
})
})
.finally(() => {
if (sourcesLoaded === NUMBER_SOURCES) {
setIsLoading(false)
}
@@ -277,25 +287,23 @@ const DocsSearch = () => {
})
}
const debouncedSearch = useMemo(() => debounce(handleSearch, 1000), [handleSearch])
const debouncedSearch = useMemo(() => debounce(handleSearch, 300), [handleSearch])
useEffect(() => {
initialLoad.current = false
// search immediately if there is a search term on initial load
if (search) {
handleSearch(search)
}
}, [])
useEffect(() => {
if (!search) {
// Clear search results if user deletes query
// and cancel any pending debounced searches
if (initialLoad.current) {
// On first navigation into 'docs search' page, search immediately
if (search) {
handleSearch(search)
}
initialLoad.current = false
} else if (search) {
// Else if user is typing, debounce search
debouncedSearch(search)
} else {
// If user clears search, reset results
debouncedSearch.cancel()
key.current += 1
dispatch({ type: 'reset', key: key.current })
} else if (!initialLoad.current) {
debouncedSearch(search)
}
}, [search])
+6
View File
@@ -70,3 +70,9 @@ redirect_uri = ""
# Overrides the default auth provider URL. Used to support self-hosted gitlab, single-tenant Azure,
# or any other third-party OIDC providers.
url = ""
[functions.search-fts]
verify_jwt = false
[functions.search-embeddings]
verify_jwt = false